python能解密java的_使用AES-CFB在python中加密并在Java中解密

我的问题是,我无法在Java中正确解密。尽管使用了正确的密钥和IV,解密后我仍然得到垃圾字符。我在Java中没有任何编译/运行时错误或异常,因此我相信我使用正确的参数进行解密。

Python加密代码-

from Crypto.Cipher import AES

import base64

key = '0123456789012345'

iv = 'RandomInitVector'

raw = 'samplePlainText'

cipher = AES.new(key,AES.MODE_CFB,iv)

encrypted = base64.b64encode(iv + cipher.encrypt(raw))

Java解密代码-

private static String KEY = "0123456789012345";

public static String decrypt(String encrypted_encoded_string) throws NoSuchAlgorithmException, NoSuchPaddingException,

InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

String plain_text = "";

try{

byte[] encrypted_decoded_bytes = Base64.getDecoder().decode(encrypted_encoded_string);

String encrypted_decoded_string = new String(encrypted_decoded_bytes);

String iv_string = encrypted_decoded_string.substring(0,16); //IV is retrieved correctly.

IvParameterSpec iv = new IvParameterSpec(iv_string.getBytes());

SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");

cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

plain_text = new String(cipher.doFinal(encrypted_decoded_bytes));//Returns garbage characters

return plain_text;

} catch (Exception e) {

System.err.println("Caught Exception: " + e.getMessage());

}

return plain_text;

}

有什么明显的我想念的东西吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值