Encryption AES 256 Input Plaintext Length Issue

moto_geek

From my security team, they want me to use AES256 key strength and CBC mode. My code only works when I enter a input plaintext of 32 letters in length now after changing to 256 CBC and block size to 128.

If I enter "This is a test" (not 32 characters long), I receive:

System.Security.Cryptography.CryptographicException: The input data is not a complete block.

If I enter: " ABCDEFGHIJKLMNOPQRSTUVWXYZ000000", works!

What code do I need to make this work with "This is a test" as input.

Code Below:

public byte[] EncryptStringToByte(string plainText, byte[] key, byte[]  vector)
{               
byte[] encrypted;                
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
    aes.BlockSize = 128;
    aes.KeySize = 256;
    aes.Mode = CipherMode.CBC;
    aes.Padding = PaddingMode.None;
    aes.Key = key;
    aes.IV = vector;

    // Create a decrytor to perform the stream transform.
    ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

    // Create the streams used for encryption. 
    using (MemoryStream msEncrypt = new MemoryStream())
    {
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {

                //Write all data to the stream.
                swEncrypt.Write(plainText);
            }
            encrypted = msEncrypt.ToArray();
        }
    }
    }
// Return the encrypted bytes from the memory stream. 
return encrypted;
}
Artjom B.

AES is a block cipher, so it only works on plaintexts that have exactly the size of one block. A mode of operation like CBC enables you to encrypt plaintexts that are a multiple of the block size. To encrypt plaintexts of arbitrary length a padding mode must be used.

A common mode used for block ciphers is PKCS#5/PKCS#7:

aes.Padding = PaddingMode.PKCS7;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Issue with AES 256-Bit Encryption Key Size in C#

From Dev

Java AES 256 encryption

From Dev

AES Encryption And WCF Issue

From Dev

AES 256 encryption PHP with Padding

From Dev

AES Encryption 256 ECB Mode

From Dev

AES 256 Encryption is not matching with result

From Dev

OpenSSL AES-256-CBC Encryption Error, "wrong final block length" Ruby

From Dev

AES 256 bit encryption - java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

From Dev

AES-CTR double encryption reverses the ciphertext to plaintext

From Dev

How to encrypt plaintext with AES-256 CBC in PHP using OpenSSL?

From Dev

CryptoJS AES CBC 256 decrypt adds additional byte in the middle of plaintext

From Dev

PHP AES encryption charset issue

From Dev

Does this use 256-bit AES encryption?

From Dev

AES 256 Encryption/Decryption without IV

From Dev

AES 256 encryption in C++ and Qt 5

From Dev

AES 256 encryption - Qt equivalent for Java

From Dev

Replicate Oracle AES 256 encryption to C#

From Dev

implementing AES256 encryption into IOS

From Dev

AES 256 encryption in C++ and Qt 5

From Dev

How to use 256 bit aes encryption in android

From Dev

AES 256 encryption mysql vs php method

From Dev

AES 256 bit encryption with 256 block size in iOS

From Dev

AES 128 bit & AES 256 bit encryption decryption

From Dev

AES encryption in Java using any length password

From Dev

AES, 128 and 256 Invalid Key Length

From Dev

AES256 Libgcrypt Invalid Key Length

From Dev

AES encryption error: The input data is not a complete block?

From Dev

Bouncy Castle AES Encryption - providing input in blocks

From Dev

AES encryption error: The input data is not a complete block?

Related Related

HotTag

Archive