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

1 packagecom.test;2 importjavax.crypto.Cipher;3 importjavax.crypto.spec.IvParameterSpec;4 importjavax.crypto.spec.SecretKeySpec;5 importjava.util.Base64.Encoder;6 importjava.util.Base64;7 importjava.util.Base64.Decoder;8

9

10 /**

11 * AES/CBC/PKCS5Padding 对称加密12 *@authorjia13 *14 */

15 public classAEStest {16

17 private static final String ALGORITHM = "AES/CBC/PKCS5Padding";18 /**

19 * 数据加密20 *@paramsrcData21 *@paramkey22 *@paramiv23 *@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, newIvParameterSpec(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 returnencodeBase64String;40 }41

42 /**

43 * 数据解密44 *@paramencDataStr45 *@paramkey46 *@paramiv47 *@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, newIvParameterSpec(iv));59 decbbdt =cipher.doFinal(encData);60 } catch(Exception e) {61 e.printStackTrace();62 }63 return newString(decbbdt);64 }65

66

67 public staticString str2HexStr(String str) {68 char[] chars = "0123456789ABCDEF".toCharArray();69 StringBuilder sb = new StringBuilder("");70 byte[] bs =str.getBytes();71 intbit;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 returnsb.toString().trim();80 }81

82 /**

83 * 16进制直接转换成为字符串(无需Unicode解码)84 *@paramhexStr85 *@return

86 */

87 public staticString hexStr2Str(String hexStr) {88 String str = "0123456789ABCDEF";89 char[] hexs =hexStr.toCharArray();90 byte[] bytes = new byte[hexStr.length() / 2];91 intn;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 newString(bytes);98 }99

100 public static void main(String[] args) throwsException {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("加密后: "+newString(encrypt));110 String hex =AEStest.str2HexStr(encrypt);111 System.out.println("加密后转16进制: "+newString(hex));112 String tohex =AEStest.hexStr2Str(hex);113 System.out.println("16进制转加密后: "+newString(tohex));114

115

116 String decrypt =AEStest.decrypt(tohex, s, iv.getBytes());117 System.out.println("解密后: "+decrypt);118 }119 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值