Python解密后面留下了不需要的字符

时间:2013-06-20 19:09:39

标签: python google-app-engine aes pycrypto

所以这是我的解密功能:

def decrypt(value):
    key = b'1111111111111111'

    cipher = AES.new(key, AES.MODE_ECB)
    msg = cipher.decrypt(base64.b64decode(value))

    return msg

我认为我在这里遗漏了一些东西,因为当我给它一个字符串来解密时它也会添加一些奇怪的字符:

示例:hey\r\r\r\r\r\r\r\r\r\r\r\r\r

我做错了什么?

P.S。我正在使用谷歌应用引擎和此库from Crypto.Cipher import AES

编辑:

private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1','1','1','1','1' };

public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
     //   String encryptedValue = new BASE64Encoder().encode(encVal);

        byte[] decoded = Base64.encodeBase64(encVal);

        return (new String(decoded, "UTF-8") + "\n");
    }

private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }

3 个答案:

答案 0 :(得分:0)

刚刚做什么

return msg.strip()

将摆脱所有\ r \ n的

答案 1 :(得分:0)

我的错,我没有找到足够的答案

以下是答案的链接:Encrypt & Decrypt using PyCrypto AES 256

非常感谢

答案 2 :(得分:0)

由于Java默认为"PKCS#5Padding",因此可以非常安全地假设您必须处理这些填充字节(一个到块大小 - 在这种情况下,16个字节总是填充)。 Java "PKCS#5Padding"填充模式实际上是PKCS#7填充,因为PKCS#5仅指定8字节块的填充(例如DES模式加密)。

所以这意味着你必须寻找PKCS#5填充或PKCS#7填充。快速搜索显示this answer