Linux下AES解密失败

在win下正常运行但在linux下报错Given final block not properly padded. Such issues can arise if a bad key 好像是因为win中生成的key是一样的,而在部分linux下会变成随机

一下是解密修改前后的对照

 1     public static byte[] decrypt(byte[] content) {
 2         String password="123456";
 3         try {
 4             KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
 5             //防止linux下 随机生成key 
 6             SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
 7             random.setSeed(password.getBytes("UTF-8"));
 8             kgen.init(128, random);
 9             //kgen.init(128, new SecureRandom(password.getBytes()));
10             SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
11             byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
12             SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
13             Cipher cipher = Cipher.getInstance("AES");// 创建密码器
14             cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
15             byte[] result = cipher.doFinal(content);  
16             return result; // 明文   
17 
18         } catch (NoSuchAlgorithmException e) {
19             e.printStackTrace();
20         } catch (NoSuchPaddingException e) {
21             e.printStackTrace();
22         } catch (InvalidKeyException e) {
23             e.printStackTrace();
24         } catch (IllegalBlockSizeException e) {
25             e.printStackTrace();
26         } catch (BadPaddingException e) {
27             e.printStackTrace();
28         } catch (UnsupportedEncodingException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32         return null;
33     }

下面是修改过的

 

 1     public static byte[] decrypt(byte[] content) {
 2         String password="123456";
 3         try {
 4             KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者
 5             //防止linux下 随机生成key 
 6             SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
 7             random.setSeed(password.getBytes("UTF-8"));
 8             kgen.init(128, random);
 9             //kgen.init(128, new SecureRandom(password.getBytes()));
10             SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
11             byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
12             SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
13             Cipher cipher = Cipher.getInstance("AES");// 创建密码器
14             cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
15             byte[] result = cipher.doFinal(content);  
16             return result; // 明文   
17 
18         } catch (NoSuchAlgorithmException e) {
19             e.printStackTrace();
20         } catch (NoSuchPaddingException e) {
21             e.printStackTrace();
22         } catch (InvalidKeyException e) {
23             e.printStackTrace();
24         } catch (IllegalBlockSizeException e) {
25             e.printStackTrace();
26         } catch (BadPaddingException e) {
27             e.printStackTrace();
28         } catch (UnsupportedEncodingException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         }
32         return null;
33     }

猜你喜欢

转载自www.cnblogs.com/garrettLsy/p/10376034.html
0条评论
添加一条新回复