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

标签 c# python encryption aes

我正在尝试将此 C# 代码转换为 Python (2.7)。问题是python代码解密的结果是错误的。 IV 和 key 正确。

我发现很多主题都讨论 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解密

def AESdecrypt(ciphertext, UID):

    from Crypto.Cipher import AES

    digest = hashlib.sha512(UID).hexdigest()

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

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

    block40Str = BitArray(hex=ciphertext[1].encode('hex'))

    cipherSpec = AES.new(key.bytes, AES.MODE_CBC, iv.bytes)
    plaintextWithPadding = cipherSpec.decrypt(block40Str.bytes)

注意:抱歉我的英语

感谢您的帮助!

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

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

最佳答案

用于生成 key 的摘要和 iv 是使用正确的数据生成的,但采用字符串形式。相反,C# 使用数据的 ByteArray 生成摘要。感谢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])

关于c# - 使用 AES/CBC/NoPadding 将 C# 转换为 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27801126/

相关文章:

C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?

python - 如何在不耗尽内存的情况下迭代 Django 中的大表?

python - Django : Use multiple CSS file in one html

ubuntu - 无法在 s3cmd 上使用加密选项

iphone - 基于客户端-服务器的 iPhone 应用程序中的通信安全

c# - 如何使 x 轴忽略 x 值的顺序?

c# - 与#include Someplace.Models 等效的 aspx/ascx View 页面是什么?

c# - PictureBox Refresh导致上面的图层闪烁

python - 将 CSV 文件转换为 Excel 后,整数将存储为字符串 - 如何将它们转换回来?

java - 如何在 Java 中使用 Skipjack (skip32) 随机化数据库中的连续整数