0

In our application third party tool is used to encrypt the file and we have to decrypt the file. we are responsible to supply keys and IVs. But we are unable to decrypt it back and getting below errors.

If I use openssl its working fine and I am able to decrypt the file successfully

All details are attached here, please help what I am missing.

public string Decrypt(string cipherText, string key, string IV)
{
    var rm = new RijndaelManaged();
    rm.BlockSize = 256;
    rm.KeySize = 256;
    rm.Mode = CipherMode.ECB;
    rm.Padding = PaddingMode.None;

    var encryptedValue = Encoding.ASCII.GetBytes(cipherText);
    var byteKey = ConvertStringToByte(key);
    var byteIV = ConvertStringToByte(IV);
    var DecryptorTransform = rm.CreateDecryptor(byteKey, byteIV); <-- **error is here**

code copied from here

Sample Keys

Key (64 chars) : d3dda598fc93a90d3de733fd3de733fdc1bb863de733fdc1bb86fbdc1bb86fb IV (32 chars) : d3dda598fc93a90d3de733fdc1bb86fb

Error Details

"message": "Specified key is not a valid size for this algorithm.",
"details": "CryptographicException: Specified key is not a valid size for this algorithm.\r\nSTACK TRACE:    at

System.Security.Cryptography.RijndaelManagedTransform.GenerateKeyExpansion(Byte[] rgbKey)\r\n at System.Security.Cryptography.RijndaelManagedTransform..ctor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 blockSize, Int32 feedbackSize, PaddingMode PaddingValue, RijndaelManagedTransformMode transformMode)\r\n at System.Security.Cryptography.RijndaelManaged.NewEncryptor(Byte[] rgbKey, CipherMode mode, Byte[] rgbIV, Int32 feedbackSize, RijndaelManagedTransformMode encryptMode)\r\n at System.Security.Cryptography.RijndaelManaged.CreateDecryptor(Byte[] rgbKey, Byte[] rgbIV)\r\n at Helix.Bdc.Services.AccessMaangement.SecretManager.Decrypt(String cipherText, String key, String IV)

user2463514
  • 273
  • 1
  • 4
  • 19
  • If I recall correctly, `RijndaelManaged` is obsolete. You should use [`AesManaged`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aesmanaged?view=netframework-4.8) – ikerbera Aug 02 '19 at 05:19
  • I tired with both AesManaged and RijndaelManaged but same error – user2463514 Aug 02 '19 at 12:16
  • Well, I can't know for sure without the code for `ConvertStringToByte`, but I've had a symilar problem converting the code [from php to C#](https://stackoverflow.com/a/54416378/8024781). My problem was that the string char count was being doubled when converting to `byte[]`, thus getting a key of invalid length. How many items do you have in `byteKey`? – ikerbera Aug 05 '19 at 05:08
  • I am using third party library now "Bouncy Castle" – user2463514 Aug 13 '19 at 17:07

0 Answers0