尝试使用其他语言进行解密时,AES解密错误

时间:2018-03-08 14:06:43

标签: c# c++ encryption aes

当我尝试在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的猜测,但我不知道它是否是正确的编码。

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

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

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

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

相关问题