导航:首页 > 编程语言 > java实现md5

java实现md5

发布时间:2022-09-01 06:55:08

java怎么把字符串进行md5加密

给你看源代码,我自己写的

public static String md5(String src){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] output = md.digest(src.getBytes());//加密处理
//将加密结果output利用Base64转换成字符串输出
String ret = Base64.encodeBase64String(output);

return ret;
}catch(Exception e){
throw new NoteException("密码加密失败",e);
}

}

public static void main(String[] args) {
System.out.println(md5("123456"));
}

㈡ java 中如何进行md5加密

JDK里面有一个java.security.MessageDigest类,这个类就是用来加密的。

加密代码如下:

Stringtoken=System.currentTimeMillis()+newRandom().nextInt()+"";
try{
MessageDigestmd=MessageDigest.getInstance("MD5");
byte[]md5=md.digest(token.getBytes());
}catch(Exceptione){
thrownewRuntimeException(e);
}

这个byte类型的数组就是使用MD5加密后的结果

㈢ 如何在java中实现md5加密和解密

package endecrypt;
02.
03.import java.io.UnsupportedEncodingException;
04.import java.security.MessageDigest;
05.import java.security.NoSuchAlgorithmException;
06.
07./**
08. * 采用MD5加密解密
09. * @author tfq
10. * @datetime 2011-10-13
11. */
12.public class MD5Util {
13.
14. /***
15. * MD5加码 生成32位md5码
16. */
17. public static String string2MD5(String inStr){
18. MessageDigest md5 = null;
19. try{
20. md5 = MessageDigest.getInstance("MD5");
21. }catch (Exception e){
22. System.out.println(e.toString());
23. e.printStackTrace();
24. return "";
25. }
26. char[] charArray = inStr.toCharArray();
27. byte[] byteArray = new byte[charArray.length];
28.
29. for (int i = 0; i < charArray.length; i++)
30. byteArray[i] = (byte) charArray[i];
31. byte[] md5Bytes = md5.digest(byteArray);
32. StringBuffer hexValue = new StringBuffer();
33. for (int i = 0; i < md5Bytes.length; i++){
34. int val = ((int) md5Bytes[i]) & 0xff;
35. if (val < 16)
36. hexValue.append("0");
37. hexValue.append(Integer.toHexString(val));
38. }
39. return hexValue.toString();
40.
41. }
42.
43. /**
44. * 加密解密算法 执行一次加密,两次解密
45. */
46. public static String convertMD5(String inStr){
47.
48. char[] a = inStr.toCharArray();
49. for (int i = 0; i < a.length; i++){
50. a[i] = (char) (a[i] ^ 't');
51. }
52. String s = new String(a);
53. return s;
54.
55. }

㈣ java如何实现md5加密算法

使用org.apache.catalina.util.MD5Encoder;这个类更强悍
如果简单试用的就是MD5.encode("加密内容")就OK了要导入apache的jar

㈤ 如何使用Java生成MD5代码

这是我以前做的一个小项目时用到md5写的


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//将用户密码进行md5加密 并返回加密后的32位十六进制密码

public class MD5Util {
public static String md5(String password) {

try {
// 获取md5对象
MessageDigest md = MessageDigest.getInstance("md5");
// 获取加密后的密码并返回十进制字节数组
byte[] bytes = md.digest(password.getBytes());

// 遍历数组得到每个十进制数并转换成十六进制

StringBuffer sb = new StringBuffer();
for (byte b : bytes) {

// 把每个数转成十六进制 存进字符中
sb.append(toHex(b));
}
String finish = sb.toString();
return finish;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

}

// 十进制转十六进制方法
private static String toHex(byte b) {

int target = 0;

if (b < 0) {
target = 255 + b;
} else {
target = b;
}

int first = target / 16;
int second = target % 16;

return Hex[first] + Hex[second];
}

static String[] Hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" };

/*public static void main(String[] args) {
String a = MD5Util.md5("1234");
System.out.println(a);
}*/
}

㈥ java 如何采用md5解密

md5只是消息摘要,不管多长的数据均得到512比特的摘要。
所以md5一般用于验证,原始消息被修改后,md5的消息摘要会有变化。
md5不是用于加密,也就不能解密,因为有无穷多的数据对应同一个md5消息摘要

㈦ java怎么实现md5字符串加密


importjava.security.MessageDigest;

publicclassMD5Util{

(byteb[]){
StringBufferresultSb=newStringBuffer();
for(inti=0;i<b.length;i++)
resultSb.append(byteToHexString(b[i]));

returnresultSb.toString();
}

(byteb){
intn=b;
if(n<0)
n+=256;
intd1=n/16;
intd2=n%16;
returnhexDigits[d1]+hexDigits[d2];
}

publicstaticStringMD5Encode(Stringorigin,Stringcharsetname){
StringresultString=null;
try{
resultString=newString(origin);
MessageDigestmd=MessageDigest.getInstance("MD5");
if(charsetname==null||"".equals(charsetname))
resultString=byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString=byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
}catch(Exceptionexception){
}
returnresultString;
}

[]={"0","1","2","3","4","5",
"6","7","8","9","a","b","c","d","e","f"};

publicstaticvoidmain(String[]args){
Strings="20160408dehui013691632869";
System.out.println(MD5Encode(s,null));
}

}

㈧ java如何算md5码

可以利用JDK自带的MD5来加密。

publicclassMD5Util{
publicfinalstaticStringMD5(Strings){
charhexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try{
byte[]btInput=s.getBytes();
//获得MD5摘要算法的MessageDigest对象
MessageDigestmdInst=MessageDigest.getInstance("MD5");
//使用指定的字节更新摘要
mdInst.update(btInput);
//获得密文
byte[]md=mdInst.digest();
//把密文转换成十六进制的字符串形式
intj=md.length;
charstr[]=newchar[j*2];
intk=0;
for(inti=0;i<j;i++){
bytebyte0=md[i];
str[k++]=hexDigits[byte0>>>4&0xf];
str[k++]=hexDigits[byte0&0xf];
}
returnnewString(str);
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
publicstaticvoidmain(String[]args){
System.out.println(MD5Util.MD5("20121221"));
System.out.println(MD5Util.MD5("加密"));
}
}

㈨ java MD5算法加密如何实现简单就是美!!!

var hexcase = 0;
var b64pad = "";
var chrsz = 8;

function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

function md5_vm_test()
{
return hex_md5("abc") == "";
}

function core_md5(x, len)
{
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;

var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;

for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;

a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);

a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);

}

function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
return core_md5(opad.concat(hash), 512 + 128);
}
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}

function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
function binl2b64(binarray)
{
var tab = "+/";
var str = "";
for(var i = 0; i < binarray.length * 4; i += 3)
{
var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++)
{
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}

㈩ java 怎样实现 64位的md5加密算法

直接引入“commons-codec-1.10.jar”这个java包,然后调用相应方法即可

比如我们可以写一个方法类,把常用的方法都写进去:

publicclassEncryptionUtil{
/**
*Base64encode
**/
(Stringdata){
returnBase64.encodeBase64String(data.getBytes());
}

/**
*Base64decode
*@
**/
(Stringdata){
returnnewString(Base64.decodeBase64(data.getBytes()),"utf-8");
}

/**
*md5
**/
publicstaticStringmd5Hex(Stringdata){
returnDigestUtils.md5Hex(data);
}

/**
*sha1
**/
publicstaticStringsha1Hex(Stringdata){
returnDigestUtils.sha1Hex(data);
}

/**
*sha256
**/
publicstaticStringsha256Hex(Stringdata){
returnDigestUtils.sha256Hex(data);
}

}


(PS:纯手打,望采纳)

阅读全文

与java实现md5相关的资料

热点内容
帝国神话组建云服务器 浏览:825
邓散木pdf 浏览:197
方舟怎么直连服务器图片教程 浏览:561
假相pdf 浏览:334
找对象找程序员怎么找 浏览:976
怎么投诉苹果商店app 浏览:470
华为手机如何看有多少个app 浏览:734
btr如何管理别的服务器 浏览:410
spwm软件算法 浏览:184
70多岁单身程序员 浏览:221
高考考前解压拓展训练 浏览:217
用纸做解压玩具不用浇水 浏览:584
谷轮压缩机序列号 浏览:736
牛顿插值法编程 浏览:366
php多用户留言系统 浏览:731
安卓和苹果如何切换流量 浏览:703
怎么知道dns服务器是多少 浏览:976
5995用什么简便算法脱式计算 浏览:918
电脑上如何上小米云服务器地址 浏览:921
手机资料解压密码 浏览:444