10

我正在尝试使用 C# 加密字符串并使用 Python 对其进行解密。加密/解密部分按预期工作(即我能够解密我最初加密的字符串)。但是 Python 返回的字符串开头有 2 个额外字节,每个字符用空格分隔。

**Original string** (before encryption -- encrypted using C#) = "Something you want to keep private with AES"

**Decrypted string** (using Python) = "��S o m e t h i n g  y o u   w a n t   t o   k e e p   p r i v a t e   w i t h  A E S"

为什么我在字符串的开头得到这两个额外的字节?为什么解密字符串中的所有这些空格?知道为什么吗?

谢谢!

使用 C# 加密

public static string Encrypt<T>(string value, string password, string salt)
         where T : SymmetricAlgorithm, new()
{
    DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

    SymmetricAlgorithm algorithm = new T();

    byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
    byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);

    ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

    using (MemoryStream buffer = new MemoryStream())
    {
        using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
        {
            using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
            {
                writer.Write(value);
            }
        }

        return Convert.ToBase64String(buffer.ToArray());
    }
}


string plain = "Something you want to keep private with AES";
string encrypted = CipherUtility.Encrypt<AesManaged>(plain, "password", "salt");

使用 Python + pycrypto 解密

import base64, sys
import Crypto.Cipher.AES

password = base64.b64decode('PSCIQGfoZidjEuWtJAdn1JGYzKDonk9YblI0uv96O8s=') # See rgbKey
salt = base64.b64decode('ehjtnMiGhNhoxRuUzfBOXw==') # See rgbIV
aes = Crypto.Cipher.AES.new(password, Crypto.Cipher.AES.MODE_CBC, salt)
text = base64.b64decode('QpHn/fnraLswwI2Znt1xTaBzRtDqO4V5QI78jLOlVsbvaIs0yXMUlqJhQtK+su2hYn28G2vNyLkj0zLOs+RIjElCSqJv1aK/Yu8uY07oAeStqRt4u/DVUzoWlxdrlF0u')

print aes.decrypt(text)
4

2 回答 2

12

使用 UTF-16 编码将字符串编码为字节。前两个字节是 BOM。然后每个字符被编码为两个字节。

从文档中Encoding.Unicode

使用 little endian 字节顺序获取 UTF-16 格式的编码。

要获取原始字符串,您需要将其从 UTF-16 字节解码回 Unicode 字符串。

print aes.decrypt(text).decode('utf-16')
于 2012-05-12T17:25:40.260 回答
0
 def decrypted(self) -> str:
    _pwd = base64.b64decode(self._key)
    _salt = base64.b64decode(self._salt)
    _aes = Crypto.Cipher.AES.new(_pwd, Crypto.Cipher.AES.MODE_CBC, _salt)
    _text = base64.b64decode(self._encrypted_str)
    _decode = _aes.decrypt(_text).decode('utf-16')
    _regex = '[a-zA-Z0-9 +-,\/ ]+'
    return re.findall(_regex, _decode)

正在使用正则表达式

于 2019-05-28T15:19:23.350 回答