2121 */
2222public class AES extends EncryptAndDecrypt {
2323
24+ private String encryptType = "AES/CBC/PKCS5Padding" ;
25+ private String algorithmParam = "abcdefgh12345678" ;
26+
27+ private void execute (String data ) throws InvalidKeyException ,
28+ IllegalBlockSizeException , BadPaddingException ,
29+ UnsupportedEncodingException , NoSuchAlgorithmException ,
30+ NoSuchPaddingException , InvalidAlgorithmParameterException {
31+ SecretKey secretKey = createSecretKey ("AES" , "abcdefgh_1234567" );
32+ byte [] secretData = encrypt (encryptType , secretKey , data ,
33+ algorithmParam );
34+ log ("使用%s加密后的数据:" , encryptType );
35+ log (Base64 .encodeBase64String (secretData ));
36+
37+ String srcStr = decrypt (encryptType , secretKey , secretData ,
38+ algorithmParam );
39+ log ("解密后的数据:\n %s" , srcStr );
40+ }
41+
2442 public static void main (String [] args ) throws UnsupportedEncodingException ,
2543 InvalidKeyException , IllegalBlockSizeException ,
2644 BadPaddingException , NoSuchAlgorithmException ,
@@ -29,15 +47,7 @@ public static void main(String[] args) throws UnsupportedEncodingException,
2947 log ("待加密的数据:\n %s" , data );
3048
3149 AES aes = new AES ();
32- String encryptType = "AES/CBC/PKCS5Padding" ;
33- String algorithmParam = "abcdefgh12345678" ;
34- SecretKey secretKey = aes .createSecretKey ("AES" , "abcdefgh_1234567" );
35- byte [] secretData = aes .encrypt (encryptType , secretKey , data , algorithmParam );
36- log ("使用%s加密后的数据:" , encryptType );
37- log (Base64 .encodeBase64String (secretData ));
38-
39- String srcStr = aes .decrypt (encryptType , secretKey , secretData , algorithmParam );
40- log ("解密后的数据:\n %s" , srcStr );
50+ aes .execute (data );
4151 }
4252
4353}
0 commit comments