Vishnu February 2016

the input data is not a complete block while decrypting the cipher Text

can anyone help me where is the problem? Here is the Decrypt function code.

    public  static string DecryptStringAES(string cipherText)
    {
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("plainText");
        String key = "0102030405060708";
        String iv = "1020304050607080";
        if (key == null || key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (iv == null || iv.Length <= 0)
            throw new ArgumentNullException("IV");
        byte[] bytearraytodecrypt = Encoding.ASCII.GetBytes(cipherText);


        AesCryptoServiceProvider keydecrypt = new AesCryptoServiceProvider();
        keydecrypt.BlockSize = 128;
        keydecrypt.KeySize = 128;
        keydecrypt.Key = System.Text.Encoding.UTF8.GetBytes(key);
        keydecrypt.IV = System.Text.Encoding.UTF8.GetBytes(iv);
        keydecrypt.Padding = PaddingMode.PKCS7;
        keydecrypt.Mode = CipherMode.CBC;

        ICryptoTransform crypto1 = keydecrypt.CreateDecryptor(keydecrypt.Key, keydecrypt.IV);

        byte[] returnbytearray = crypto1.TransformFinalBlock(bytearraytodecrypt, 0, bytearraytodecrypt.Length);
        crypto1.Dispose();
        return Convert.ToBase64String(returnbytearray);
    }

Answers


zaph February 2016

Not sure what the question is.

AES in CBC mode is a block cipher, that is the input and output is in block sized chunks.

If the input to encryption might not always be a multiple of the block size padding can be added (PKCS#7 padding in the question code) to the data to be encrypted to make it so. The output will be a multiple of the block size.

Because of this the question does not make sense, the encrypted data will always be a multiole of the block size if PKCS#7 padding is used (as in the code).

On decryption the decrypted output will be a multiple of the block size and then the padding can be removed.

By specifying PKCS#7 padding int he APIU call the padding/de-padding will happen in the API.

There are other modes such as CTR where the output is the same length as the input, they are in general "streaming" modes and have their own complications such as never using the same nonce with the same key.


Vishnu February 2016

hi Here is the code after the modifications and it works fine. The changes I made to the previous code is converting the cipher text from Base64 string to the bytes format and at last while returning the String convert it to the System.Text.Encoding.UTF8.GetString format. public static string DecryptStringAES(string cipherText) { if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("plainText"); String key = "0102030405060708"; String iv = "1020304050607080"; if (key == null || key.Length <= 0) throw new ArgumentNullException("Key"); if (iv == null || iv.Length <= 0) throw new ArgumentNullException("IV"); byte[] bytearraytodecrypt = Convert.FromBase64String(cipherText);

    AesCryptoServiceProvider keydecrypt = new AesCryptoServiceProvider();
    keydecrypt.BlockSize = 128;
    keydecrypt.KeySize = 128;
    keydecrypt.Key = System.Text.Encoding.UTF8.GetBytes(key);
    keydecrypt.IV = System.Text.Encoding.UTF8.GetBytes(iv);
    keydecrypt.Padding = PaddingMode.PKCS7;
    keydecrypt.Mode = CipherMode.CBC;

    ICryptoTransform crypto1 = keydecrypt.CreateDecryptor(keydecrypt.Key, keydecrypt.IV);

    byte[] returnbytearray = crypto1.TransformFinalBlock(bytearraytodecrypt, 0, bytearraytodecrypt.Length);
    crypto1.Dispose();

return System.Text.Encoding.UTF8.GetString(returnbytearray); }

Post Status

Asked in February 2016
Viewed 1,801 times
Voted 5
Answered 2 times

Search




Leave an answer