I amgetting System.Security.Cryptography.CryptographicException: 'Length of the data to decrypt is invalid.'

460 Views Asked by At

The encryption works fine but I get the exception while decrypting the cipher text. The cipher text is generated via encrypting 'Test' Cipher text: u1jeSfKVfSRfSieLX01/uQ==

    string encrypt(string plainText)
    {
        AesManaged aesCipher = new AesManaged();
        aesCipher.KeySize = 128;
        aesCipher.BlockSize = 128;
        aesCipher.Mode = CipherMode.CBC;
        aesCipher.Padding = PaddingMode.PKCS7;
        aesCipher.Key = generatedKey();

        byte[] iv = new byte[16];
        // 15 1B 0F 03 56 3A 66 6D E5 E1 1D 83 12 21 B4 8E
        iv[0] = 0x15;
        iv[1] = 0x1B;
        iv[2] = 0x0F;
        iv[3] = 0x03;
        iv[4] = 0x56;
        iv[5] = 0x3A;
        iv[6] = 0x66;
        iv[7] = 0x6D;
        iv[8] = 0xE5;
        iv[9] = 0xE1;
        iv[10] = 0x1D;
        iv[11] = 0x83;
        iv[12] = 0x12;
        iv[13] = 0x21;
        iv[14] = 0xB4;
        iv[15] = 0x8E;

        aesCipher.IV = iv;

        byte[] b = System.Text.Encoding.UTF8.GetBytes(plainText);
        ICryptoTransform encryptTransform = aesCipher.CreateEncryptor();
        byte[] ctext = encryptTransform.TransformFinalBlock(b, 0, b.Length);

        System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));
        System.Console.WriteLine("Cipher text: " + Convert.ToBase64String(ctext));
        return Convert.ToBase64String(ctext);
    }
    string decrypt(String CipherText)
    {
        AesManaged aesCipher = new AesManaged();
        aesCipher.KeySize = 128;
        aesCipher.BlockSize = 128;

        aesCipher.Mode = CipherMode.CBC;
        aesCipher.Padding = PaddingMode.PKCS7;
        byte[] key = generatedKey();
        aesCipher.Key = key;

        byte[] iv = new byte[16];
        // 15 1B 0F 03 56 3A 66 6D E5 E1 1D 83 12 21 B4 8E
        iv[0] = 0x15;
        iv[1] = 0x1B;
        iv[2] = 0x0F;
        iv[3] = 0x03;
        iv[4] = 0x56;
        iv[5] = 0x3A;
        iv[6] = 0x66;
        iv[7] = 0x6D;
        iv[8] = 0xE5;
        iv[9] = 0xE1;
        iv[10] = 0x1D;
        iv[11] = 0x83;
        iv[12] = 0x12;
        iv[13] = 0x21;
        iv[14] = 0xB4;
        iv[15] = 0x8E;

        aesCipher.IV = iv;
        System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));
        // aesCipher.IV = new Ini

        ICryptoTransform decryptTransform = aesCipher.CreateDecryptor(aesCipher.key, aesCipher.IV);
        byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, CipherText.Length);
        return System.Text.Encoding.UTF8.GetString(plainText);
    }
    byte[] generatedKey()
    {
        // byte[] salt = new byte[] { 172, 137, 25, 56, 156, 100, 136, 211, 84, 67, 96, 10, 24, 111, 112, 137, 3 };
        byte[] salt = new byte[17];
        // AC 89 19 38 9C 64 88 D3 54 43 60 0A 18 6F 70 89 03
        salt[0] = 0xAC;
        salt[1] = 0x89;
        salt[2] = 0x19;
        salt[3] = 0x38;
        salt[4] = 0x9C;
        salt[5] = 0x64;
        salt[6] = 0x88;
        salt[7] = 0xD3;
        salt[8] = 0x54;
        salt[9] = 0x43;
        salt[10] = 0x60;
        salt[11] = 0x0A;
        salt[12] = 0x18;
        salt[13] = 0x6F;
        salt[14] = 0x70;
        salt[15] = 0x89;
        salt[16] = 0x03;


        int iterations = 1024;
        var rfc2898 = new System.Security.Cryptography.Rfc2898DeriveBytes("!CarIT.123#2017", salt, iterations);
        byte[] key = rfc2898.GetBytes(16);
        String keyB64 = Convert.ToBase64String(key);
        System.Console.WriteLine("Key: " + keyB64);
        return key;
    }

I also tried using byte[] plainText = decryptTransform.TransformFinalBlock(Convert.FromBase64String(CipherText), 0, CipherText.Length); instead of byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, CipherText.Length); But I get error "Value was invalid" on the same line.

1

There are 1 best solutions below

1
Divesh Bari On

My bad,

byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, CipherText.Length);

should be like

byte[] plainText = decryptTransform.TransformFinalBlock(Encoding.ASCII.GetBytes(CipherText), 0, Encoding.ASCII.GetBytes(CipherText).Length);

Now it works fine, I have passed bytes as first paramter in TransformFinalBlock() and then get the length of plain text in third parameter which was wrong.