使用AES/CBC/NoPadding将C转换为Python

2024-09-24 00:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将这个C代码转换成Python(2.7)。问题是解密的结果与python代码不符。IV和钥匙是正确的。在

我找到了很多关于Python和C的主题,但是我没有找到答案。在

C加密:

class Tracer
{
    private static readonly int BlockBitSize = 128;
    private static readonly int KeyBitSize = 256;

    internal static byte[] In(byte[] plainBytes, byte[] uid)
    {
        using (var sha = new SHA512Managed())
        {
            var hash = sha.ComputeHash(uid);
            return In(plainBytes, hash.Skip(32).Take(32).ToArray(), hash.Take(16).ToArray());
        }
    }

    internal static byte[] In(byte[] plainBytes, byte[] key, byte[] iv)
    {
        if (key == null || key.Length != KeyBitSize / 8)
            throw new ArgumentException(String.Format("Key needs to be {0} bit!", KeyBitSize), "key");
        if (iv == null || iv.Length != BlockBitSize / 8)
            throw new ArgumentException(String.Format("IV needs to be {0} bit!", BlockBitSize), "iv");

        using (AesManaged aes = new AesManaged())
        {
            aes.KeySize = KeyBitSize;
            aes.BlockSize = BlockBitSize;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;

            using (ICryptoTransform encrypter = aes.CreateEncryptor(key, iv))
                using (MemoryStream cipherStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                        cryptoStream.FlushFinalBlock();
                    }
                    return cipherStream.ToArray();
                }
        }
    }

    internal static byte[] Out(byte[] cipherBytes, byte[] uid)
    {
        using (var sha = new SHA512Managed())
        {
            var hash = sha.ComputeHash(uid);
            return Out(cipherBytes, hash.Skip(32).Take(32).ToArray(), hash.Take(16).ToArray());
        }
    }

    internal static byte[] Out(byte[] cipherBytes, byte[] key, byte[] iv)
    {
        if (key == null || key.Length != KeyBitSize / 8)
            throw new ArgumentException(String.Format("Key needs to be {0} bit!", KeyBitSize), "key");
        if (iv == null || iv.Length != BlockBitSize / 8)
            throw new ArgumentException(String.Format("IV needs to be {0} bit!", BlockBitSize), "iv");

        using (AesManaged aes = new AesManaged())
        {
            aes.KeySize = KeyBitSize;
            aes.BlockSize = BlockBitSize;
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;

            using (ICryptoTransform decrypter = aes.CreateDecryptor(key, iv))
                using (MemoryStream plainStream = new MemoryStream())
                {
                    using (var decrypterStream = new CryptoStream(plainStream, decrypter, CryptoStreamMode.Write))
                        using (var binaryWriter = new BinaryWriter(decrypterStream))
                        {
                            //Decrypt Cipher Text from Message
                            binaryWriter.Write(cipherBytes, 0, cipherBytes.Length);
                        }
                    //Return Plain Text
                    return plainStream.ToArray();
                }
        }
    }
}

Python解密

^{pr2}$

注:对不起我的英语

谢谢你的帮助!在

编辑:Python中的AES解密返回64个字符,这是错误的。原始明文是32。在

更新了Python代码EDIT2。解密函数现在返回32个字符,但仍然出错


Tags: keynewvarstatichashbytelengthaes
1条回答
网友
1楼 · 发布于 2024-09-24 00:31:00

用于生成密钥的摘要和iv是用正确的数据生成的,但使用的是字符串。相反地,C#用一系列数据生成摘要。多亏了BitArray Python library,我解决了我的问题:

新的Python代码:

def AESdecrypt(ciphertext, UID):

    from Crypto.Cipher import AES

    UIDBytes = BitArray(hex=UID)
    digest = hashlib.sha512(UIDBytes.bytes).hexdigest()

    iv = BitArray(hex=digest[:32])

    key = BitArray(hex=digest[64:128])

    cipherSpec = AES.new(key.bytes, AES.MODE_CBC, iv.bytes)
    plaintextWithoutPadding = cipherSpec.decrypt(ciphertext[1])

相关问题 更多 >