55import java .security .NoSuchAlgorithmException ;
66import java .security .SecureRandom ;
77import java .util .Arrays ;
8+ import java .util .Base64 ;
89
910import javax .crypto .Cipher ;
1011import javax .crypto .KeyGenerator ;
1112import javax .crypto .SecretKey ;
13+ import javax .crypto .spec .IvParameterSpec ;
1214import javax .crypto .spec .SecretKeySpec ;
1315
1416/*
1921
2022public class AES128Coder {
2123 private static final String KEY_TYPE = "AES" ;
22- private static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding" ;
24+ private static final String AES = "AES" ;
25+ private static final String AES_ECB = "AES/ECB/PKCS5Padding" ;
26+ private static final String AES_CBC = "AES/CBC/PKCS5Padding" ;
27+ private static final String ALGORITHM = AES_ECB ;
2328
24- // private static final String CIPHER_ALGORITHM_ECB = "AES";
29+ // "AES/ECB/PKCS5Padding";
30+
31+ // private static final String CIPHER_ALGORITHM_ECB = "AES";
2532
2633 private byte [] key = null ;
2734
@@ -46,15 +53,31 @@ public AES128Coder(byte[] key) {
4653 } else if (key .length > 16 ) {
4754 key = Arrays .copyOf (key , 16 );
4855 }
49- System .out .println ("key=" + Arrays .toString (key ));
56+ System .out .println ("key=" + Arrays .toString (key ));
5057 this .key = key ;
5158 }
5259
5360 public static void main (String [] args ) {
61+ // System.err.println("providers:" +
62+ // Arrays.toString(Security.getProviders()));
63+ // System.err.println("Cipher:" +
64+ // DebugUtil.sortToString(Security.getAlgorithms("Cipher")));
65+ // System.err.println("Signature:" +
66+ // DebugUtil.sortToString(Security.getAlgorithms("Signature")));
67+ // System.err.println("MessageDigest:" +
68+ // DebugUtil.sortToString(Security.getAlgorithms("MessageDigest")));
69+ // System.err.println("KeyStore:" +
70+ // DebugUtil.sortToString(Security.getAlgorithms("KeyStore")));
71+ // System.err.println("Mac:" +
72+ // DebugUtil.sortToString(Security.getAlgorithms("Mac")));
73+ // Provider jceProvider = Security.getProvider("SunJCE");
74+ // System.err.println("JCE::" +
75+ // DebugUtil.sortToString(jceProvider.getServices()));
76+ System .out .println ("======================================" );
5477 // String src = "guessyourheart";
5578 // System.out.println("原文是:" + src);
5679 // byte[] key = "guessyourheart".getBytes(Charset.forName("UTF-8"));
57- String src = "123455 " ;
80+ String src = "1234567812345678 " ;
5881 System .out .println ("原文是:" + src );
5982 byte [] key = "12345678" .getBytes (Charset .forName ("UTF-8" ));
6083 AES128Coder coder = new AES128Coder (key );
@@ -74,13 +97,12 @@ public static void main(String[] args) {
7497 }
7598
7699 public String encrypt (byte [] src ) {
77- byte [] bytes = this .aesEncrypt (src );
78- System .out .println ("bytes[]=" + Arrays .toString (bytes ));
79- return CommUtil .encBase64Url (bytes );
100+ // System.out.println("bytes[]=" + ByteUtil.toHexString(bytes));
101+ return Base64 .getMimeEncoder ().encodeToString (this .aesEncrypt (src ));
80102 }
81103
82104 public byte [] decrypt (String src ) {
83- return this .aesDecrypt (CommUtil . decBase64Url (src ));
105+ return this .aesDecrypt (Base64 . getMimeDecoder (). decode (src ));
84106 }
85107
86108 /********** public functions ends ******************/
@@ -110,31 +132,56 @@ private Key toKey(byte[] key) throws Exception {
110132 return new SecretKeySpec (key , KEY_TYPE );
111133 }
112134
113- private byte [] aesDecrypt (byte [] cipherText ) {
135+ private byte [] aesDecrypt (byte [] cipherBytes ) {
114136 byte [] key = this .key ;
115137 byte [] plainText = null ;
116138 try {
117139 Key k = this .toKey (key );
118- Cipher cipher = Cipher .getInstance (CIPHER_ALGORITHM_ECB );
119- cipher .init (Cipher .DECRYPT_MODE , k );
120- plainText = cipher .doFinal (cipherText );
140+ // System.out.println(
141+ // "decrypt key:" + k.getAlgorithm() + "--" + k.getFormat() + "--" +
142+ // Arrays.toString(k.getEncoded()));
143+ Cipher cipher = Cipher .getInstance (ALGORITHM );
144+ if (AES_CBC .equals (ALGORITHM )) {
145+ IvParameterSpec iv = new IvParameterSpec (new byte [16 ]);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
146+ cipher .init (Cipher .DECRYPT_MODE , k , iv );
147+ } else {
148+ cipher .init (Cipher .DECRYPT_MODE , k );
149+ }
150+ // System.out.println("cipher:" + cipher.getAlgorithm() +
151+ // "--blockSize:" + cipher.getBlockSize()
152+ // + "--outputsize:" + cipher.getOutputSize(cipherBytes.length) +
153+ // "--provider:" + cipher.getProvider());
154+ plainText = cipher .doFinal (cipherBytes );
121155 } catch (Exception e ) {
122- e . printStackTrace ( );
156+ throw new IllegalStateException ( e );
123157 }
124158
125159 return plainText ;
126160 }
127161
128- private byte [] aesEncrypt (byte [] plainText ) {
162+ private byte [] aesEncrypt (byte [] plainBytes ) {
129163 byte [] key = this .key ;
130164 byte [] cipherText = null ;
131165 try {
132166 Key k = this .toKey (key );
133- Cipher cipher = Cipher .getInstance (CIPHER_ALGORITHM_ECB );
134- cipher .init (Cipher .ENCRYPT_MODE , k );
135- cipherText = cipher .doFinal (plainText );
167+ // System.out.println(
168+ // "encrypt key:" + k.getAlgorithm() + "--" + k.getFormat() + "--" +
169+ // Arrays.toString(k.getEncoded()));
170+ Cipher cipher = Cipher .getInstance (ALGORITHM );
171+
172+ if (AES_CBC .equals (ALGORITHM )) {
173+ IvParameterSpec iv = new IvParameterSpec (new byte [16 ]);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
174+ cipher .init (Cipher .ENCRYPT_MODE , k , iv );
175+ } else {
176+ cipher .init (Cipher .ENCRYPT_MODE , k );
177+ }
178+ // System.out.println("cipher:" + cipher.getAlgorithm() +
179+ // "--blockSize:" + cipher.getBlockSize()
180+ // + "--outputsize:" + cipher.getOutputSize(plainBytes.length) +
181+ // "--provider:" + cipher.getProvider());
182+ cipherText = cipher .doFinal (plainBytes );
136183 } catch (Exception e ) {
137- e . printStackTrace ( );
184+ throw new IllegalStateException ( e );
138185 }
139186
140187 return cipherText ;
0 commit comments