简体   繁体   English

使用C#进行AES解密

[英]AES Decryption with C#

I use Crypto++ in C++ to encrypt a string. 我在C ++中使用Crypto ++来加密字符串。 Now I want to decrypt this ciphertext with C# but it doesn't work. 现在,我想用C#解密此密文,但是它不起作用。

My C++ Code to encrypted the string is as follow: 我的用于加密字符串的C ++代码如下:

string encrypt(string data)
{
  // Key and IV setup
  std::string key = "0123456789abcdef";
  std::string iv = "aaaaaaaaaaaaaaaa";

  std::string plaintext = data;
  std::string ciphertext;

  // Create Cipher Text
  CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
  CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (byte *)iv.c_str());

  CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
  stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length() + 1);
  stfEncryptor.MessageEnd();

  return ciphertext;
}

To decrypt the code in C++ I can use this code 要解密C ++中的代码,我可以使用此代码

CryptoPP::AES::Decryption aesDecryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte *)iv.c_str() );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( result_string.c_str() ), result_string.size() );
stfDecryptor.MessageEnd()

But I need to decrypt the code in a C# application and therefor I use this methode: 但是我需要解密C#应用程序中的代码,因此我使用以下方法:

static string Decrypt(byte[] cipherText)
{
  if (cipherText == null || cipherText.Length <= 0)
    throw new ArgumentNullException("cipherText");

  byte[] Key = GetBytes(ConfigurationManager.AppSettings["aes_key"]);
  byte[] IV = GetBytes(ConfigurationManager.AppSettings["aes_iv"]);

  // Declare the string used to hold the decrypted text.
  string plaintext = null;

  // Create an RijndaelManaged object with the specified key and IV.
  using (RijndaelManaged rijAlg = new RijndaelManaged())
  {
    rijAlg.Key = Key;
    rijAlg.IV = IV;

    // Create a decrytor to perform the stream transform.
    ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

    // Create the streams used for decryption.
    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
    {
      using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
      {
        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
        {
          // Read the decrypted bytes from the decrypting stream
          // and place them in a string.
          plaintext = srDecrypt.ReadToEnd();
        }
      }
    }
  }

  return plaintext;
}

static byte[] GetBytes(string str)
{
  byte[] bytes = new byte[str.Length * sizeof(char)];
  System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
  return bytes;
}

If I try to decrypt the ciphertext, I get the message, that the specified initialization vector does not match the block size for this algorithm. 如果尝试解密密文,则会收到消息,指出指定的初始化向量与该算法的块大小不匹配。

I do not know why this doesn't work and I hope that someone can help me. 我不知道为什么这行不通,我希望有人能帮助我。

Try setting your IV like this: 尝试这样设置IV:

rijAlg.IV = new byte[]{ 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97 }; 

which ensures the IV has exactly 128 bits (and all a 's, as in your c++ sample). 这确保了IV具有正好128位(和所有a的,作为C ++样品中)。

If this works, make sure that the IV is read correctly from the configuration. 如果可行,请确保从配置中正确读取了IV。 The value of ConfigurationManager.AppSettings["aes_iv"] is probably either empty, or does not have a length of 16. ConfigurationManager.AppSettings["aes_iv"]值可能为空,或者长度为16。

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

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