AesCBC.java
3.5 KB
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
91
92
93
94
95
96
package com.framework.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.jfinal.kit.HttpKit;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AesCBC {
/*
* 已确认 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
*/
// public final static String sKey = "aaaaaaaaaaaaaaaa";
public final static String ivParameter = "0000000000000000";
private static AesCBC instance = null;
// private static
private AesCBC() {
}
public static AesCBC getInstance() {
if (instance == null)
instance = new AesCBC();
return instance;
}
public static String getKey() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
String formatDate = sdf.format(date);
Date d = new Date(System.currentTimeMillis());
String key = formatDate + "owenkasduf" + (new SimpleDateFormat("MM")).format(d);
return key;
}
// 加密
public String encrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
// cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));
return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。
} catch (Exception e) {
}
return null;
}
// 解密
public String decrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
// cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, encodingFormat);
return originalString;
} catch (Exception ex) {
return null;
}
}
public static void main(String[] args) throws Exception {
// 需要加密的字串
// String cSrc = "aqswed1234";
// System.out.println("加密前的字串是:" + cSrc);
// // 加密
// String enString = AesCBC.getInstance().encrypt(cSrc, "utf-8", getKey(), ivParameter);
// System.out.println("加密后的字串是:" + enString);
// BASE64Encoder encoder = new BASE64Encoder();
// List<NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("data", enString));
String params = "data=YCbPytqeduiEj0PGGIeF5Q4rgxJNd59JCtJ5duHrzN8bKvlc8wGhqVyCraamqwNCwb+eGhCBoZ3v\r\n" +
"6SD39qXHKZTDCh/69wufSze7D1b9KMQ=";
String result = HttpKit.post("http://zzcargo.com:8080/index.php?r=api/index", params);
System.out.println(result);
// 解密
// String DeString = AesCBC.getInstance().decrypt(result,"utf-8",sKey,ivParameter);
// System.out.println("解密后的字串是:" + DeString);
}
}