gpt4 book ai didi

java - java中的AES/CBC/PKCS5Padding加密在python中解密出错

转载 作者:行者123 更新时间:2023-12-01 06:22:26 30 4
gpt4 key购买 nike

我正在尝试用 Java 加密数据文件,然后用 Python 解密。

但是解密的数据文件总是有一些填充字节仍然像这样

my normal content^@^@^@^@^@

我实际上在 python 代码中执行了 UNPAD 操作( func decrypt_file() )

当我删除 UNPAD 操作时,我得到了这个:

my normal content^@^@^@^@^@^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P^P

所以,看来Java加密方法做了两次填充。

我很困惑,被困在这里。有人可以帮忙吗?非常感谢!

这是我的代码。

(1)java版本

import java.io.File;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
private static final String key = "wesurexZ5!Hcurfit";
private static final String ivs = "zK2hzBvP%FRJ5%lD";
public static byte[] encrypt(byte[] strInBytes) throws Exception {
SecretKeySpec skeySpec = getKey(key);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
return cipher.doFinal(strInBytes);
}

public static byte[] decrypt(byte[] strIn) throws Exception {
SecretKeySpec skeySpec = getKey(key);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] decrypted = cipher.doFinal(strIn);
return decrypted;
}

private static SecretKeySpec getKey(String strKey) throws Exception {
byte[] arrBTmp = strKey.getBytes();
byte[] arrB = new byte[16];
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
return skeySpec;
}
}

(2)python版本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

reload(sys)
sys.setdefaultencoding("utf-8")

from Crypto.Cipher import AES


class AESTool(object):

def __init__(self, iv, key):
self.iv = iv.decode("utf8")
self.key = key
bs = AES.block_size
self.pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
self.unpad = lambda s: s[0:-ord(s[-1])]

def encrypt(self, plain_text):
try:
encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
encrypted_text = encryptor.encrypt(self.pad(plain_text))
except Exception as ex:
raise Exception(ex.message)

return encrypted_text

def decrypt(self, decrypted_text):
try:
decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
plain_text = decryptor.decrypt(decrypted_text)
except Exception as ex:
raise Exception(ex.message)

return self.unpad(plain_text)

def encrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
encryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
try:
with open(from_full_path, 'rb') as infile:
with open(to_full_path, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)

if len(chunk) == 0:
break

if len(chunk) < chunksize:
outfile.write(encryptor.encrypt(self.pad(chunk)))
break

outfile.write(encryptor.encrypt(chunk))
except Exception as ex:
return -1

return 0

def decrypt_file(self, from_full_path, to_full_path, chunksize=64*1024):
decryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
try:
with open(from_full_path, 'rb') as infile:
with open(to_full_path, 'wb') as outfile:
prev_chunk = None
while True:
chunk = infile.read(chunksize)

if len(chunk) == 0 and prev_chunk:
outfile.write(self.unpad(decryptor.decrypt(prev_chunk)))
break

if prev_chunk:
outfile.write(decryptor.decrypt(prev_chunk))

if len(chunk) < chunksize:
outfile.write(self.unpad(decryptor.decrypt(chunk)))
break

prev_chunk = chunk
except Exception as ex:
return -1

return 0

最佳答案

您知道当您尝试使用默认模式进行 enc/dec 时是否会发生这种情况吗?我很想知道,因为如果是这样的话,那么我们就会遇到同样的问题。我的意思是默认使用:

Cipher cipher = Cipher.getInstance("AES");

而不是;

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

关于java - java中的AES/CBC/PKCS5Padding加密在python中解密出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907014/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com