java开发之MD5加密算法的实现
程序员文章站
2023-12-05 21:46:16
先看看代码再说:复制代码 代码如下:package com.b510.note; import java.math.biginteger; import...
先看看代码再说:
package com.b510.note;
import java.math.biginteger;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
/**
* md5加密
*
* @author hongten
*
*/
public class md5 {
public static void main(string[] args) {
system.out.println(md5.getmd5("123456"));
}
/**
* 用md5编码后的码值
*
* @param sinput
* 明码
* @return md5加密后的密码
*/
public static string getmd5(string sinput) {
string algorithm = "";
if (sinput == null) {
return "null";
}
try {
algorithm = system.getproperty("md5.algorithm", "md5");
} catch (securityexception se) {
}
messagedigest md = null;
try {
md = messagedigest.getinstance(algorithm);
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
}
byte buffer[] = sinput.getbytes();
for (int count = 0; count < sinput.length(); count++) {
md.update(buffer, 0, count);
}
byte bdigest[] = md.digest();
biginteger bi = new biginteger(bdigest);
return (bi.tostring(16));
}
}
运行效果:
1 186d636867f51c667893c1b1b3d96dc3
我们可以根据自己的需要,来得到自己想要的md5加密后的密文;
1 //这里的数字是可以根据需要而进行改变的
2 return (bi.tostring(16));
复制代码 代码如下:
package com.b510.note;
import java.math.biginteger;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
/**
* md5加密
*
* @author hongten
*
*/
public class md5 {
public static void main(string[] args) {
system.out.println(md5.getmd5("123456"));
}
/**
* 用md5编码后的码值
*
* @param sinput
* 明码
* @return md5加密后的密码
*/
public static string getmd5(string sinput) {
string algorithm = "";
if (sinput == null) {
return "null";
}
try {
algorithm = system.getproperty("md5.algorithm", "md5");
} catch (securityexception se) {
}
messagedigest md = null;
try {
md = messagedigest.getinstance(algorithm);
} catch (nosuchalgorithmexception e) {
e.printstacktrace();
}
byte buffer[] = sinput.getbytes();
for (int count = 0; count < sinput.length(); count++) {
md.update(buffer, 0, count);
}
byte bdigest[] = md.digest();
biginteger bi = new biginteger(bdigest);
return (bi.tostring(16));
}
}
运行效果:
复制代码 代码如下:
1 186d636867f51c667893c1b1b3d96dc3
我们可以根据自己的需要,来得到自己想要的md5加密后的密文;
复制代码 代码如下:
1 //这里的数字是可以根据需要而进行改变的
2 return (bi.tostring(16));
上一篇: C#实现顺序队列和链队列的代码实例