@@ -31,7 +31,7 @@ public void testStringEncrypt() throws Exception {
3131 new SecureRandom ().nextBytes (var2 );
3232 new SecretKeySpec (var2 , "AES" );
3333
34- SecretKey key = AESUtil .generateKey (128 );
34+ SecretKey key = AESUtil .randomKey (128 );
3535 final String base64 = AESUtil .generateIvByte ();
3636
3737 // 固定iv密钥
@@ -49,10 +49,11 @@ public void testStringEncrypt() throws Exception {
4949 Assert .assertEquals (input , plainText );
5050 }
5151
52+
5253 @ Test
5354 public void testGivenFile_whenEncrypt_thenSuccess () throws Exception {
5455 // given
55- SecretKey key = AESUtil .generateKey (128 );
56+ SecretKey key = AESUtil .randomKey (128 );
5657 String algorithm = "AES/CBC/PKCS5Padding" ;
5758 IvParameterSpec ivParameterSpec = AESUtil .generateIv ();
5859 Path originPath = Paths .get ("src/test/resources/origin.txt" );
@@ -79,7 +80,7 @@ public void testGivenFile_whenEncrypt_thenSuccess() throws Exception {
7980 public void givenObject_whenEncrypt_thenSuccess () throws Exception {
8081 // given
8182 Student student = new Student ("Baeldung" , 20 );
82- SecretKey key = AESUtil .generateKey (128 );
83+ SecretKey key = AESUtil .randomKey (128 );
8384 IvParameterSpec ivParameterSpec = AESUtil .generateIv ();
8485 String algorithm = "AES/CBC/PKCS5Padding" ;
8586
@@ -116,23 +117,35 @@ public void givenPassword_whenEncrypt_thenSuccess() throws Exception {
116117
117118 @ Test
118119 public void testConfigWay () throws Exception {
119- String plainText = "www.baeldung.com" ;
120+ String plainText = "" + System . currentTimeMillis () ;
120121 String password = "baeldung" ;
121122 String salt = "12345678" ;
122- final String iv = AESUtil .generateIvByte ();
123- log .info ("iv={}" , iv );
124- final String result = encryptPass (plainText , password , salt , iv );
125- final String origin = decryptPass (result , password , salt , iv );
123+ // final String iv = AESUtil.generateIvByte();
124+ String iv = "D+OER4Pzbc+CtaXeVKlTEQ==" ;
125+ log .info ("iv [{}] salt [{}]" , iv , salt );
126+ // 即使固定了iv 初始化向量,在跨系统传输仍需要双方约定三个 变量比较麻烦
127+ final String result = AESUtil .encryptPass (plainText , password , salt , iv );
128+ final String origin = AESUtil .decryptPass (result , password , salt , iv );
129+ log .info ("{} {}" , result , origin );
126130
127131 Assert .assertEquals (origin , plainText );
128-
129132 }
130133
131- public String encryptPass (String text , String passwd , String salt , String iv ) throws Exception {
132- return AESUtil .encryptPasswordBased (text , AESUtil .getKeyFromPassword (passwd , salt ), AESUtil .generateIv (iv ));
134+ @ Test
135+ public void testSimple () throws Exception {
136+ try {
137+ String key = "b423eb489f47dbe933f7e761946ec216" ;
138+ SecretKeySpec secretKey = AESUtil .generateKey (key );
139+
140+ String plainText = "Hello, AES Encryption!" ;
141+ String encryptedText = AESUtil .encrypt (plainText , secretKey );
142+ System .out .println ("加密后的密文 (Base64): " + encryptedText );
143+
144+ String decryptedText = AESUtil .decrypt (encryptedText , secretKey );
145+ System .out .println ("解密后的明文: " + decryptedText );
146+ } catch (Exception e ) {
147+ log .error ("" , e );
148+ }
133149 }
134150
135- public String decryptPass (String text , String passwd , String salt , String iv ) throws Exception {
136- return AESUtil .decryptPasswordBased (text , AESUtil .getKeyFromPassword (passwd , salt ), AESUtil .generateIv (iv ));
137- }
138151}
0 commit comments