尝试用另一种语言解密时 AES 解密错误

Wrong AES decryption when trying to decrypt in another language

本文关键字:解密 错误 AES 语言 另一种      更新时间:2023-10-16

当我尝试在C++中加密并在 C# 中解密时,它给了我一个错误

输入数据不是完整的块

但这对我来说没有任何意义,因为如果我尝试用C++解密消息,与我进行加密的语言相同,它可以正常工作。

因此,C++部分的一些代码:

int main(int argc, char* argv[]) {
 std::string szEncryptionKey = "Sixteen byte key";
 std::string szEncryptionIV = "Sixteen byte key";
 std::string str = "I do not like green eggs and ham I do not like them Sam-I-Am.";
 std::string str_encrypted = encrypt(str, szEncryptionKey, szEncryptionIV);
 std::string str_decrypted = decrypt(str_encrypted, szEncryptionKey, szEncryptionIV);
 std::cout << "str encrypted: " << str_encrypted << std::endl;
 std::cout << "str decrypted: " << str_decrypted << std::endl;
return 0;
}

std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
  std::string str_out;
  CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
  CryptoPP::StringSource encryptor(str_in, true,
    new CryptoPP::StreamTransformationFilter(encryption,
        new CryptoPP::Base64Encoder(
            new CryptoPP::StringSink(str_out),
            BlockPaddingSchemeDef::NO_PADDING
        )
     )
  );
 return str_out;
}

我正在将加密和 base64 编码的字符串以及 KEY 和 IV 带到 C#:

        static void Main(string[] args)
    {
        byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");

            String msg2 = "cboiiqATFAU9KyK49BJMmkLLwiAyaP6yYjyM0oP09xbITLGaxRuLCsTYYzUKANydhO+JO7N00aVz0tmFTg==";
        byte[] msg = Convert.FromBase64String(msg2);
        DecryptStringFromBytes_Aes(msg, bytes, bytes);
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");
        // Declare the string used to hold
        // the decrypted text.
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Mode = CipherMode.CFB;
            aesAlg.Padding = PaddingMode.None;
            aesAlg.Key = Key;
            aesAlg.IV = IV;
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted = null;
            // Create an Aes object
            // with the specified key and IV.
            using (ICryptoTransform decrypt = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
            {
                byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);
               String s = Encoding.UTF8.GetString(decryptedText);
                Console.Write(s);
                return s;
            }
        }
    }

错误消息出现在这一行:

byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);

这一行:

byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");

这只是 UTF8 的一个猜测,但我不知道它是否是正确的编码。

谁能帮我解决这个问题?

输入数据不是块大小的精确倍数,AES 为 16 字节。 解码时msg2为 61 字节,这不是有效的 AES 加密长度,因此无效并产生错误消息:"输入数据不是完整的块".

AES是基于块的,因此如果要加密的数据不是块大小的精确倍数,则必须填充。与 AES 一起使用的一般填充方法是 PKCS#7,有时称为 PKCS#5。

CFB 模式需要填充,CFB8 模式不需要。