java aes 中文_从Java到Python的AES加密

我试图对python和java中的一些字符串进行加密和解密,反之亦然,但是当我尝试用java加密和在python中使用相同的密钥解密时,我遇到了问题。在

这是我用来解密java输出的python算法from hashlib import pbkdf2_hmac

from Crypto.Cipher import AES

salt = bytes({0xC7, 0xB5, 0x99, 0xF4, 0x4B, 0x7C, 0x81, 0x77})

ITERATION_COUNT = 65536

KEY_LENGTH = 256

IV_LENGTH = 16

password = b'EKc8KyQj6H0pUe7WoGyU8MezojRvaDb9Uz1Ggu5fZ+c='

key = pbkdf2_hmac(

hash_name='sha1',

password=password,

salt=salt,

iterations=65536,

dklen=32

)

plain="F7a45f2a2o7fi+mTn0DYAnkAOfkr1sW33S2kHBXGJV8eMU680qVz2a7MflE9ap8ztWaXIpHJmuYS98yPSnmQ+NZeLD5PQYU+nHwKarXFm+x7bJpF13e7SRmMIkHJuHl7KSKlrxz7fQjWj2gMooYNArmArGi/MiIOBZG9wT7qrYyUA/1twMlusw4EfqJdfo7dtro9XGlja8Ya1ZyuwFq/7sS+ni+Yd80mOAwmtkwUj9rKxJ51juVVek1n2SVO6a6phQmiy2K3fqMmc1xAKQj2trZaWT5MHAjjs6amNeZ1+1w/lQTLAn1pTYUdKOhYHgLDh63p1a3bVTXqslDvBkDFsY2PaXPWnzEqTF8vXiHOS542lWNKhPQusngRWugGai6C1goySV3w6UYNfoq6BfUzqRLYfkQb+qUS5RmTwEyb9AB4lsFfrskCB2mHklZTR+aqadE3Z7rrLkPZRUETbhq8LsDvxNJpr1ZDCKhIle79Dkqn6ZpvD6wiyokQaErhAVMVjXbppEJGv69SJHyLwyc+61JY42i05T57FsQWt30+bZ5rmC1fXV+sB8KvGjCDPGUT"

iv=plain[:IV_LENGTH]

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

print(cipher.decrypt(plain))

输出应该类似于:

^{pr2}$

我正在尝试在encrypt&decrypt中成功地将java代码转换为python代码import java.security.SecureRandom;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.PBEKeySpec;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class EncDec {

private static final byte[] SALT = { (byte) 0xC7, (byte) 0xB5, (byte) 0x99,

(byte) 0xF4, (byte) 0x4B, (byte) 0x7C, (byte) 0x81, (byte) 0x77 };

private static final int ITERATION_COUNT = 65536;

private static final int KEY_LENGTH = 256;

private static final int IV_LENGTH = 16;

private Cipher eCipher;

private Cipher dCipher;

private byte[] encrypt;

private byte[] iv;

public EncDec(String passPhrase) {

try {

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT,ITERATION_COUNT, KEY_LENGTH);

SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);

SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(),"AES");

eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

iv = eCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();

dCipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(iv));

} catch (Exception ex) {

ex.printStackTrace();

}

}

public EncDec(String passPhrase, String encryptedString) {

try {

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT,ITERATION_COUNT, KEY_LENGTH);

SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);

SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(),"AES");

encrypt = Base64.decodeBase64(encryptedString);

eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] iv = extractIV();

dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

dCipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(iv));

} catch (Exception ex) {

ex.printStackTrace();

}

}

public String encrypt(String encrypt) {

String encStr = null;

try {

byte[] bytes = encrypt.getBytes("UTF8");

byte[] encrypted = encrypt(bytes);

byte[] cipherText = new byte[encrypted.length + iv.length];

System.arraycopy(iv, 0, cipherText, 0, iv.length);

System.arraycopy(encrypted, 0, cipherText, iv.length,encrypted.length);

encStr = new String(Base64.encodeBase64(cipherText));

} catch (Exception ex) {

ex.printStackTrace();

}

return encStr;

}

public byte[] encrypt(byte[] plain) throws Exception {

return eCipher.doFinal(plain);

}

private byte[] extractIV() {

byte[] iv = new byte[IV_LENGTH];

System.arraycopy(encrypt, 0, iv, 0, iv.length);

return iv;

}

public String decrypt() {

String decStr = null;

try {

byte[] bytes = extractCipherText();

byte[] decrypted = decrypt(bytes);

decStr = new String(decrypted, "UTF8");

} catch (Exception ex) {

ex.printStackTrace();

}

return decStr;

}

private byte[] extractCipherText() {

byte[] ciphertext = new byte[encrypt.length - IV_LENGTH];

System.arraycopy(encrypt, 16, ciphertext, 0, ciphertext.length);

return ciphertext;

}

public byte[] decrypt(byte[] encrypt) throws Exception {

return dCipher.doFinal(encrypt);

}

但我得到了错误的数据:b'\x8c\xb7m\xfc\x8b\x04\xc9s\xbbp\xe4\xd1\xafw\xb2A\x08\x01\xdb)\xd4qB\xe2\xef.........'

我也不明白什么是SALT变量,它是用来做什么的?另外,IV参数的计算是如何工作的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值