在Python中解密的AES / CBC / PKCS5Padding加密出错了

时间:2017-07-04 13:12:12

标签: java python aes padding pkcs#5

我试图用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加密方法填充了TWICE。

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

这是我的代码。

(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 version

#!/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

1 个答案:

答案 0 :(得分:-1)

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

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

而不是;

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