简体   繁体   English

C#字节数组长度不如python

[英]C# byte array length inferior to python

I'm trying to port some Python code to C# and in the code there is a part where it encrypts a string with AES with the following key:我正在尝试将一些 Python 代码移植到 C# 中,并且在代码中有一部分使用以下密钥使用 AES 加密字符串:

'\\xd7\\xdf\\xca2\\xd0Vhu\\xeb\\x06\\xa0\\xba\\n\\xa2\\x07O\\xc1\\x8b\\xcf\\x8f2&t\\xc0\\x92\\xc4\\xa5\\x0b>\\xb4\\xe7\\xbc' '\\xd7\\xdf\\xca2\\xd0Vhu\\xeb\\x06\\xa0\\xba\\n\\xa2\\x07O\\xc1\\x8b\\xcf\\x8f2&t\\xc0\\x92\\xc4\\xa5\\x0b>\\xb4\\xe7\\xbc'

This key is 32 bytes in Python, but the problem is that in C# it is 30 bytes when I convert that string ( Encoding.Default.GetBytes ), so I can't generate the AES key.这个密钥在 Python 中是 32 个字节,但问题是在 C# 中,当我转换该字符串( Encoding.Default.GetBytes )时它是 30 个字节,所以我无法生成 AES 密钥。

Why is the length of the key 32 in Python and the byte array length is 30 in C#?为什么Python中key的长度是32,C#中byte数组的长度是30?

Converting the key to bytes array:将键转换为字节数组:

byte[] key = Encoding.Default.GetBytes("\xd7\xdf\xca2\xd0Vhu\xeb\x06\xa0\xba\n\xa2\x07O\xc1\x8b\xcf\x8f2&t\xc0\x92\xc4\xa5\x0b>\xb4\xe7\xbc");

Getting invalid key size for algorithm exception (because it's 30 bytes and not 32):获取算法异常的无效密钥大小(因为它是 30 个字节而不是 32 个字节):

byte[] encrypted;
byte[] iv;
using (Aes aesAlg = Aes.Create())
{
    aesAlg.Key = key;
    aesAlg.GenerateIV();
    iv = aesAlg.IV;
    aesAlg.Mode = CipherMode.CBC;

    var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, iv);

    using (var msEncrypt = new MemoryStream())
    {
        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (var swEncrypt = new StreamWriter(csEncrypt))
            {
                swEncrypt.Write("test");
            }
            encrypted = msEncrypt.ToArray();
        }
    }
}

The byte string you have is in a sort of mixed encoding, with unicode code-points and ASCII representations of characters alongside each-other.您拥有的字节字符串是一种混合编码,具有 unicode 代码点和字符的 ASCII 表示并排在一起。 You can see this in a few places,你可以在几个地方看到这一点,

'\\xd7\\xdf\\xca 2 \\xd0 Vhu \\xeb\\x06\\xa0\\xba \\n \\xa2\\x07 O \\xc1\\x8b\\xcf\\x8f 2&t \\xc0\\x92\\xc4\\xa5\\x0b > \\xb4\\xe7\\xbc' '\\xd7\\xdf\\xca 2 \\xd0 Vhu \\xeb\\x06\\xa0\\xba \\n \\xa2\\x07 O \\xc1\\x8b\\xcf\\x8f 2&t \\xc0\\x92\\xc4\\xa5\\x0b > \\xb4\\xe7 \\xbc'

If you convert those over to codepoints, you end up with "\\xd7\\xdf\\xca\\x32\\xd0\\x56\\x68\\x75\\xeb\\x06\\xa0\\xba\\x0a\\xa2\\x07\\x4f\\xc1\\x8b\\xcf\\x8f\\x32\\x26\\x74\\xc0\\x92\\xc4\\xa5\\x0b\\x3e\\xb4\\xe7\\xbc" , and should have the correct bytes for your key.如果将它们转换为代码点,最终会得到"\\xd7\\xdf\\xca\\x32\\xd0\\x56\\x68\\x75\\xeb\\x06\\xa0\\xba\\x0a\\xa2\\x07\\x4f\\xc1\\x8b\\xcf\\x8f\\x32\\x26\\x74\\xc0\\x92\\xc4\\xa5\\x0b\\x3e\\xb4\\xe7\\xbc" ,并且应该为您的密钥提供正确的字节。

var val = "\xd7\xdf\xca\x32\xd0\x56\x68\x75\xeb\x06\xa0\xba\x0a\xa2\x07\x4f\xc1\x8b\xcf\x8f\x32\x26\x74\xc0\x92\xc4\xa5\x0b\x3e\xb4\xe7\xbc";
var count = Encoding.Default.GetByteCount(val);
Console.WriteLine(count); // 32 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM