简体   繁体   中英

Got Error: “Specified block size is not valid for this algorithm” while initialize AesCryptoProvider

I am trying to use larger block size for the AES Encryption:

private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPublicKey)
{
    using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
    {
        // Create instance of AesManaged for
        // symetric encryption of the data.
        aes.KeySize = 256;

        // Allocating 64K
        aes.BlockSize = 8 * 1024 * 64; 
    }
}

and hitting the following exception:

System.Security.Cryptography.CryptographicException was unhandled
Message=Specified block size is not valid for this algorithm.
Source=mscorlib StackTrace: at System.Security.Cryptography.SymmetricAlgorithm.set_BlockSize(Int32 value) at ConsoleApplication4.Program.EncryptFile(String inFile, RSACryptoServiceProvider rsaPublicKey) in C:\\Projects\\ConsoleApplication4\\Program.cs:line 117

I sure am missing somehting ovbious, any clue?

It appears that AES only supports block size equal to 128. It appears that you can get around this by processing your data as multiple blocks.

source

Only certain block sizes are supported by each algorithm.

See SymmetricAlgorithm.LegalBlockSizes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM