java实现AES/CBC/pack5padding加解密算法

Posted testinga

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现AES/CBC/pack5padding加解密算法相关的知识,希望对你有一定的参考价值。

    最近要测试一个借口,借口的传值参数是使用AES加密,偏移量为0000000000000000,秘钥:12345678901234567890123456789012,加密后内容转成16进制发送,用网上的代码一直没实现,最后发送是因为jre的两个jar包需要升级,支持key大于16位。

两个jar包:US_export_policy.jar、local_policy.jar,下载地址:https://files.cnblogs.com/files/tech-test/security.zip

AES加密工具网址:http://tool.chacuo.net/cryptaes

16进制与字符串互转:http://www.bejson.com/convert/ox2str/

代码如下:

  1 package com.test;
  2 import javax.crypto.Cipher;
  3 import javax.crypto.spec.IvParameterSpec;
  4 import javax.crypto.spec.SecretKeySpec;
  5 import java.util.Base64.Encoder;
  6 import java.util.Base64;
  7 import java.util.Base64.Decoder;
  8 
  9 
 10 /**
 11  * AES/CBC/PKCS5Padding 对称加密
 12  * @author jia
 13  *
 14  */
 15 public class AEStest {
 16 
 17      private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
 18         /**
 19          * 数据加密
 20          * @param srcData
 21          * @param key
 22          * @param iv
 23          * @return
 24          */
 25         public static String encrypt(String srcData,byte[] key,byte[] iv) 
 26         {
 27             SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
 28             Cipher cipher;
 29             String encodeBase64String = null;
 30             try {
 31                 cipher = Cipher.getInstance(ALGORITHM);
 32                 cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
 33                 byte[] encData = cipher.doFinal(srcData.getBytes());
 34                 encodeBase64String = Base64.getEncoder().encodeToString(encData);
 35                 //encodeBase64String = Base64.encodeBase64String(encData);
 36             } catch (Exception e) {
 37                 e.printStackTrace();
 38             }
 39             return encodeBase64String;
 40         }
 41 
 42         /**
 43          * 数据解密
 44          * @param encDataStr
 45          * @param key
 46          * @param iv
 47          * @return
 48          */
 49         public static String decrypt(String encDataStr,byte[] key,byte[] iv) 
 50         {
 51             byte[] encData = Base64.getDecoder().decode(encDataStr);
 52 //            byte[] encData = Base64.decodeBase64(encDataStr);
 53             SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
 54             Cipher cipher;
 55             byte[] decbbdt = null;
 56             try {
 57                 cipher = Cipher.getInstance(ALGORITHM);
 58                 cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
 59                 decbbdt = cipher.doFinal(encData);
 60             } catch (Exception e) {
 61                 e.printStackTrace();
 62             } 
 63             return new String(decbbdt);
 64         }
 65         
 66         
 67         public static String str2HexStr(String str) {
 68             char[] chars = "0123456789ABCDEF".toCharArray();
 69             StringBuilder sb = new StringBuilder("");
 70             byte[] bs = str.getBytes();
 71             int bit;
 72             for (int i = 0; i < bs.length; i++) {
 73                 bit = (bs[i] & 0x0f0) >> 4;
 74                 sb.append(chars[bit]);
 75                 bit = bs[i] & 0x0f;
 76                 sb.append(chars[bit]);
 77                 // sb.append(\' \');
 78             }
 79             return sb.toString().trim();
 80         }
 81 
 82         /**
 83          * 16进制直接转换成为字符串(无需Unicode解码)
 84          * @param hexStr
 85          * @return
 86          */
 87         public static String hexStr2Str(String hexStr) {
 88             String str = "0123456789ABCDEF";
 89             char[] hexs = hexStr.toCharArray();
 90             byte[] bytes = new byte[hexStr.length() / 2];
 91             int n;
 92             for (int i = 0; i < bytes.length; i++) {
 93                 n = str.indexOf(hexs[2 * i]) * 16;
 94                 n += str.indexOf(hexs[2 * i + 1]);
 95                 bytes[i] = (byte) (n & 0xff);
 96             }
 97             return new String(bytes);
 98         }
 99 
100     public static void main(String[] args) throws Exception {
101         String str = "12345678901234567890123456789012";
102         byte[] s = str.getBytes();
103         String s2 = "12345678ilkljklkjv";
104        
105         
106         String iv = "0000000000000000";
107         System.out.println("加密前: "+s2);
108         String encrypt = AEStest.encrypt(s2, s, iv.getBytes());
109         System.out.println("加密后: "+new String(encrypt));
110         String hex = AEStest.str2HexStr(encrypt);
111         System.out.println("加密后转16进制: "+new String(hex));
112         String tohex = AEStest.hexStr2Str(hex);
113         System.out.println("16进制转加密后: "+new String(tohex));
114         
115         
116         String decrypt = AEStest.decrypt(tohex, s, iv.getBytes());
117         System.out.println("解密后: "+decrypt);
118     }
119 }

 

以上是关于java实现AES/CBC/pack5padding加解密算法的主要内容,如果未能解决你的问题,请参考以下文章

如何用Java实现URLEncode

如何用Java实现URLEncode

java如何实现链表

java 如何实现多线程

接口与具体实现(Java)

用java实现