9

I am trying to decrypt a string using some pretty standard algorithm.

public static string DecryptString(string cipherText)
{
    string keyString = string.Empty;

    // Check whether the environment variable exists.
    keyString = Environment.GetEnvironmentVariable("EncryptKey");

    if (keyString == null)
    {
        keyString = "E546C8DF278CD5931069B522E695D4F2";
    }

    var fullCipher = Convert.FromBase64String(cipherText);
    using (var aesAlg = Aes.Create())
    {
        byte[] iv = new byte[aesAlg.BlockSize / 8];
        var cipher = new byte[16];

        Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
        Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
        var key = Encoding.UTF8.GetBytes(keyString);

        string result;
        using (var decryptor = aesAlg.CreateDecryptor(key, iv))
        using (var msDecrypt = new MemoryStream(cipher))
        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        using (var srDecrypt = new StreamReader(csDecrypt))
        {
            result = srDecrypt.ReadToEnd();
        }
        return result;
    }
}

I keep getting the error :

System.Security.Cryptography.CryptographicException: Specified padding mode is not valid for this algorithm.

I have tried multiple ways like this

var iv = new byte[16];
var cipher = new byte[16];

Or

var iv = aesAlg.IV;

I still get an error at this point. What am I doing wrong ?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
w2olves
  • 2,229
  • 10
  • 33
  • 60
  • Possible duplicate of [Specified key is not a valid size for this algorithm](http://stackoverflow.com/questions/2919228/specified-key-is-not-a-valid-size-for-this-algorithm) – Eris May 01 '17 at 22:17
  • How was the message encrypted, what language and implementation such as PHP mcrypt – zaph Jul 14 '17 at 03:32
  • 2
    [@w2olves] have you solved that? – Ivan Zaruba Aug 05 '17 at 11:37
  • 1
    @Eris This is not a duplicate 1) This is AES, that's not. 2) If we define key length 15, then we got the key size exception, but with 16 we got other exception (this padding thing, what the question is about) – g.pickardou Sep 18 '17 at 09:10
  • @w2olves have you resolved this issue? – LP13 Mar 08 '18 at 20:56
  • @IvanZaruba I am still facing this issue. – w2olves May 02 '18 at 17:54
  • @w2olves can you show the EncryptString method? A possible reason for the exception to be fired could be if you concatenate IV + cipher in a wrong order – Ivan Zaruba May 03 '18 at 10:28
  • 1
    By changing the value for `cipher` and altering the second `Buffer.BlockCopy` statement as mentioned in this link solves this issue: https://stackoverflow.com/a/46541503/4745542 – Vikas C May 04 '18 at 03:58

2 Answers2

1

Two changes are required

var cipher = new byte[fullCipher.Length - iv.Length];

and

Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
Clayton Harbich
  • 505
  • 1
  • 7
  • 16
-1
public static string Decrypt(string cipherText)
  {
     string EncryptionKey  = string.Empty;

    // Check whether the environment variable exists.
    EncryptionKey = Environment.GetEnvironmentVariable("EncryptKey");

    if (EncryptionKey == null)
    {
        EncryptionKey = "E546C8DF278CD5931069B522E695D4F2";
    }
            byte[] cipherBytes;
            try
            {
                cipherBytes = Convert.FromBase64String(cipherText);
            }
            catch
            {
                return cipherText;
            }
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }

Note :- You need to provide same key which you provided for encryption the string.

Love Pandey
  • 330
  • 2
  • 9
  • This is a block of code that's copy-pasted all over the web and this site. Mention where you found it, and why it's a good choice to do what the OP wants. – CodeCaster Aug 30 '19 at 10:59
  • @CodeCaster I am using this code. i did not copy from anywhere. I add this code from multiple places. – Love Pandey Aug 30 '19 at 16:02