java与python兼容_JAVA中AES对称加密和解密以及与Python兼容

1 importjava.io.IOException;2 importjava.io.UnsupportedEncodingException;3 importjava.security.InvalidAlgorithmParameterException;4 importjava.security.InvalidKeyException;5 importjava.security.NoSuchAlgorithmException;6

7 importjavax.crypto.BadPaddingException;8 importjavax.crypto.Cipher;9 importjavax.crypto.IllegalBlockSizeException;10 importjavax.crypto.NoSuchPaddingException;11 importjavax.crypto.spec.IvParameterSpec;12 importjavax.crypto.spec.SecretKeySpec;13

14 importorg.apache.commons.lang.StringUtils;15

16 importsun.misc.BASE64Decoder;17 importsun.misc.BASE64Encoder;18

19 /**

20 *

Title: TestAES3

21 *

Description:

22 *

Copyright: Copyright (c) 2012 All rights reserved.

23 *

Company: Neusoft Corporation, China, Ltd.

24 *@authorchendch25 *26 */

27 public classTestAES328 {29 private String ALGO = "AES";30 private String ALGO_MODE = "AES/CBC/NoPadding";31 private String akey = "keyskeyskeyskeys";32 private String aiv = "keyskeyskeyskeys";33 private String padding = " ";34 private int blockSize = 16;35

36 /**

37 *@paramcontent38 *@returnpassword39 *@throwsNoSuchAlgorithmException40 *@throwsNoSuchPaddingException41 *@throwsUnsupportedEncodingException42 *@throwsInvalidAlgorithmParameterException43 *@throwsInvalidKeyException44 *@throwsBadPaddingException45 *@throwsIllegalBlockSizeException46 */

47 publicString encrypt(String content)48 throwsNoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,49 BadPaddingException50 {51 String encryptContent =content;52 if (encryptContent.length()

58 Cipher cipher =Cipher.getInstance(ALGO_MODE);59 SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);60 IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));61 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);62 byte[] byteEncode = encryptContent.getBytes("utf-8");63 byte[] byteAES =cipher.doFinal(byteEncode);64

65 String AESEncode = new String(newBASE64Encoder().encode(byteAES));66

67 returnAESEncode;68 }69

70 /**

71 *@paramcontent72 *@returncontent73 *@throwsNoSuchAlgorithmException74 *@throwsNoSuchPaddingException75 *@throwsInvalidKeyException76 *@throwsInvalidAlgorithmParameterException77 *@throwsIllegalBlockSizeException78 *@throwsBadPaddingException79 *@throwsIOException80 */

81 publicString decrypt(String content)82 throwsNoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,83 BadPaddingException, IOException84 {85 byte[] byte_content = newBASE64Decoder().decodeBuffer(content);86

87 Cipher cipher =Cipher.getInstance(ALGO_MODE);88 SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);89 IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));90

91 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);92

93 byte[] byte_decode =cipher.doFinal(byte_content);94 String AES_decode = new String(byte_decode, "utf-8");95 returnAES_decode.trim();96 }97

98 /**

99 *@paramargs100 *@throwsNoSuchAlgorithmException101 *@throwsNoSuchPaddingException102 *@throwsBadPaddingException103 *@throwsIllegalBlockSizeException104 *@throwsInvalidAlgorithmParameterException105 *@throwsInvalidKeyException106 *@throwsIOException107 */

108 public static voidmain(String[] args)109 throwsNoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,110 BadPaddingException, IOException111 {112 TestAES3 testAES3 = newTestAES3();113 String content = "chenduoc";114 System.out.println("加密前:" +content);115

116 String encryptContent =testAES3.encrypt(content);117 System.out.println("加密后:" +encryptContent);118

119 String decryptContent =testAES3.decrypt(encryptContent);120 System.out.println("解密后:" +decryptContent);121 }122 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值