|
13 | 13 | import javax.crypto.spec.IvParameterSpec; |
14 | 14 | import javax.crypto.spec.SecretKeySpec; |
15 | 15 |
|
16 | | -import org.apache.commons.codec.binary.Base64; |
17 | | - |
18 | | -import static cn.aofeng.demo.util.LogUtil.log;; |
19 | | - |
20 | 16 | /** |
21 | 17 | * 加密与解密。 |
22 | 18 | * |
|
25 | 21 | public class EncryptAndDecrypt { |
26 | 22 |
|
27 | 23 | private final static String CHARSET = "utf8"; |
28 | | - |
29 | | - private SecretKey createSecretKey(String encryptType, String keyStr) |
| 24 | + |
| 25 | + /** |
| 26 | + * 创建安全密钥。 |
| 27 | + * |
| 28 | + * @param encryptType |
| 29 | + * 加密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
| 30 | + * @param keyStr |
| 31 | + * 密钥明文 |
| 32 | + * @return 安全密钥 |
| 33 | + * @throws UnsupportedEncodingException |
| 34 | + * 不支持指定的字符集编码 |
| 35 | + */ |
| 36 | + public SecretKey createSecretKey(String encryptType, String keyStr) |
30 | 37 | throws UnsupportedEncodingException { |
31 | 38 | byte[] secretKeyData = keyStr.getBytes(CHARSET); |
32 | 39 | SecretKeySpec sks = new SecretKeySpec(secretKeyData, encryptType); |
33 | | - |
| 40 | + |
34 | 41 | return sks; |
35 | 42 | } |
36 | | - |
37 | | - private byte[] encrypt(String encryptType, SecretKey secretKey, String srcData) |
38 | | - throws InvalidKeyException, IllegalBlockSizeException, |
39 | | - BadPaddingException, UnsupportedEncodingException, |
40 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
41 | | - InvalidAlgorithmParameterException { |
| 43 | + |
| 44 | + /** |
| 45 | + * 加密数据。 |
| 46 | + * |
| 47 | + * @param encryptType 加密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
| 48 | + * @param secretKey 密钥 |
| 49 | + * @param srcData 待加密的数据 |
| 50 | + * @return 数据加密后的字节数组 |
| 51 | + * @see #encrypt(String, SecretKey, String, String) |
| 52 | + */ |
| 53 | + public byte[] encrypt(String encryptType, SecretKey secretKey, |
| 54 | + String srcData) throws InvalidKeyException, |
| 55 | + IllegalBlockSizeException, BadPaddingException, |
| 56 | + UnsupportedEncodingException, NoSuchAlgorithmException, |
| 57 | + NoSuchPaddingException, InvalidAlgorithmParameterException { |
42 | 58 | return encrypt(encryptType, secretKey, srcData, null); |
43 | 59 | } |
44 | | - |
45 | | - private byte[] encrypt(String encryptType, SecretKey secretKey, String srcData, String algorithmParam) |
46 | | - throws InvalidKeyException, IllegalBlockSizeException, |
47 | | - BadPaddingException, UnsupportedEncodingException, |
48 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
49 | | - InvalidAlgorithmParameterException { |
| 60 | + |
| 61 | + public byte[] encrypt(String encryptType, SecretKey secretKey, |
| 62 | + String srcData, String algorithmParam) throws InvalidKeyException, |
| 63 | + IllegalBlockSizeException, BadPaddingException, |
| 64 | + UnsupportedEncodingException, NoSuchAlgorithmException, |
| 65 | + NoSuchPaddingException, InvalidAlgorithmParameterException { |
50 | 66 | Cipher encrpyt = Cipher.getInstance(encryptType); |
51 | 67 | if (null == algorithmParam) { |
52 | 68 | encrpyt.init(Cipher.ENCRYPT_MODE, secretKey); |
53 | 69 | } else { |
54 | | - IvParameterSpec iv = new IvParameterSpec(algorithmParam.getBytes(CHARSET)); |
| 70 | + IvParameterSpec iv = new IvParameterSpec( |
| 71 | + algorithmParam.getBytes(CHARSET)); |
55 | 72 | encrpyt.init(Cipher.ENCRYPT_MODE, secretKey, iv); |
56 | 73 | } |
57 | 74 | byte[] secretData = encrpyt.doFinal(srcData.getBytes(CHARSET)); |
58 | | - |
| 75 | + |
59 | 76 | return secretData; |
60 | 77 | } |
61 | | - |
62 | | - private String decrypt(String decryptType, SecretKey secretKey, byte[] secretData) |
63 | | - throws InvalidKeyException, IllegalBlockSizeException, |
64 | | - BadPaddingException, UnsupportedEncodingException, |
65 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
66 | | - InvalidAlgorithmParameterException { |
| 78 | + |
| 79 | + /** |
| 80 | + * 解密数据。 |
| 81 | + * |
| 82 | + * @param decryptType 解密方式,如:AES,Blowfish。详情查看<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher">Java Cryptography Architecture Standard Algorithm Name Documentation</a> |
| 83 | + * @param secretKey 密钥 |
| 84 | + * @param secretData 待解密的数据 |
| 85 | + * @return 解密后的数据 |
| 86 | + * @see #decrypt(String, SecretKey, byte[], String) |
| 87 | + */ |
| 88 | + public String decrypt(String decryptType, SecretKey secretKey, |
| 89 | + byte[] secretData) throws InvalidKeyException, |
| 90 | + IllegalBlockSizeException, BadPaddingException, |
| 91 | + UnsupportedEncodingException, NoSuchAlgorithmException, |
| 92 | + NoSuchPaddingException, InvalidAlgorithmParameterException { |
67 | 93 | return decrypt(decryptType, secretKey, secretData, null); |
68 | 94 | } |
69 | | - |
70 | | - private String decrypt(String decryptType, SecretKey secretKey, byte[] secretData, String algorithmParam) |
| 95 | + |
| 96 | + public String decrypt(String decryptType, SecretKey secretKey, |
| 97 | + byte[] secretData, String algorithmParam) |
71 | 98 | throws InvalidKeyException, IllegalBlockSizeException, |
72 | 99 | BadPaddingException, UnsupportedEncodingException, |
73 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
| 100 | + NoSuchAlgorithmException, NoSuchPaddingException, |
74 | 101 | InvalidAlgorithmParameterException { |
75 | 102 | Cipher decrypt = Cipher.getInstance(decryptType); |
76 | 103 | if (null == algorithmParam) { |
77 | 104 | decrypt.init(Cipher.DECRYPT_MODE, secretKey); |
78 | 105 | } else { |
79 | | - IvParameterSpec iv = new IvParameterSpec(algorithmParam.getBytes(CHARSET)); |
| 106 | + IvParameterSpec iv = new IvParameterSpec( |
| 107 | + algorithmParam.getBytes(CHARSET)); |
80 | 108 | decrypt.init(Cipher.DECRYPT_MODE, secretKey, iv); |
81 | 109 | } |
82 | | - byte[] srcData = decrypt.doFinal(secretData); |
| 110 | + byte[] srcData = decrypt.doFinal(secretData); |
83 | 111 | String srcStr = new String(srcData, CHARSET); |
84 | | - |
| 112 | + |
85 | 113 | return srcStr; |
86 | 114 | } |
87 | | - |
88 | | - private void blowfishPerformence(String data) |
89 | | - throws UnsupportedEncodingException, InvalidKeyException, |
90 | | - IllegalBlockSizeException, BadPaddingException, |
91 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
92 | | - InvalidAlgorithmParameterException { |
93 | | - SecretKey secretKey; |
94 | | - String type = "Blowfish"; |
95 | | - secretKey = createSecretKey(type, "abcdefgh_1234567"); |
96 | | - long startTime = System.currentTimeMillis(); |
97 | | - for (int j = 0; j < 100000; j++) { |
98 | | - encrypt(type, secretKey, data+j); |
99 | | - } |
100 | | - long endTime = System.currentTimeMillis(); |
101 | | - long usedTime = endTime - startTime; |
102 | | - log("使用%s进行%d次加密消耗时间%d毫秒", type, 100000, usedTime); |
103 | | - } |
104 | | - |
105 | | - private void aesPerformence(String data) |
106 | | - throws UnsupportedEncodingException, InvalidKeyException, |
107 | | - IllegalBlockSizeException, BadPaddingException, |
108 | | - NoSuchAlgorithmException, NoSuchPaddingException, |
109 | | - InvalidAlgorithmParameterException { |
110 | | - SecretKey secretKey; |
111 | | - String type = "AES/CBC/PKCS5Padding"; |
112 | | - secretKey = createSecretKey("AES", "abcdefgh_1234567"); |
113 | | - long startTime = System.currentTimeMillis(); |
114 | | - for (int j = 0; j < 100000; j++) { |
115 | | - encrypt(type, secretKey, data+j, "abcdefgh12345678"); |
116 | | - } |
117 | | - long endTime = System.currentTimeMillis(); |
118 | | - long usedTime = endTime - startTime; |
119 | | - log("使用%s进行%d次加密消耗时间%d毫秒", type, 100000, usedTime); |
120 | | - } |
121 | | - |
122 | | - public static void main(String[] args) throws NoSuchAlgorithmException, |
123 | | - NoSuchPaddingException, UnsupportedEncodingException, |
124 | | - InvalidKeyException, IllegalBlockSizeException, |
125 | | - BadPaddingException, InvalidAlgorithmParameterException { |
126 | | - String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-"; |
127 | | - log("待加密的数据:\n%s", data); |
128 | | - |
129 | | - String encryptType = "Blowfish"; |
130 | | - EncryptAndDecrypt bf = new EncryptAndDecrypt(); |
131 | | - SecretKey secretKey = bf.createSecretKey(encryptType, "abcdefgh_1234567"); |
132 | | - byte[] secretData = bf.encrypt(encryptType, secretKey, data); |
133 | | - log("使用%s加密后的数据:", encryptType); |
134 | | - log(Base64.encodeBase64String(secretData)); |
135 | | - |
136 | | - String srcStr = bf.decrypt(encryptType, secretKey, secretData); |
137 | | - log("解密后的数据:\n%s", srcStr); |
138 | | - |
139 | | - // ==========性能测试========== |
140 | | - // AES |
141 | | - bf.aesPerformence(data); |
142 | | - |
143 | | - // Blowfish |
144 | | - bf.blowfishPerformence(data); |
145 | | - } |
146 | 115 |
|
147 | 116 | } |
0 commit comments