Md5

Example

JAVA示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cn;
import java.net.URLEncoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class MD5Util {
/***
* md5加密
* @param md5
* @return
*/
public static String MD5(String md5)
{
try
{
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(md5.getBytes());
StringBuilder sb = new StringBuilder();
for (byte anArray : array) {
    sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
catch (java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
return null;
}
}
/**
* 密码编码
* @param content
* @param key
* @return
*/
public static String encrypts(String content, String key)
{
if(key == null || key.length() != 16)
{
System.err.println("AES key 的长度必须是16位!");
return null;
}
try
{
Cipher cipher = Cipher.getInstance("AES/ECB/NOPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = content.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0)
{
    plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
byte[] encrypted = cipher.doFinal(plaintext);
if (encrypted == null){
    return null;
}
char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[encrypted.length * 2];
for (int j = 0; j < encrypted.length; j++) {
    int v = encrypted[j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
/**
* 输入中文和特殊字符加密
* @param str
* @return
*/
public static String encode(String str) {
String strUTF8 = null;
try {
strUTF8 = URLEncoder.encode(str, "UTF-8");
System.out.println(strUTF8);
} catch (Exception e) {
e.printStackTrace();
}
return strUTF8;
}
}

© 2016-2021 北京拓课网络科技有限公司 版权所有  京ICP备17018423号-1 京公网安备11010502043461号