1

I'm developping a program which encrypts USB drives using RijndaelManaged method in C#, with the possibility of choosing between 128/192/256-bits key size. I assume that encryption should take more time with a 256-bits key, however even with a lot of files, the process takes the exact same time than with a 128-bits key. Do you think it's a programming mistake ?

I generate my key and iv from a password using this function :

private List<byte[]> GenerateKeys(string password, int strength)
{
    byte[] key;
    byte[] iv;

    List<byte[]> result = new List<byte[]>();

    Rfc2898DeriveBytes rfcDb = new Rfc2898DeriveBytes(password, System.Text.Encoding.UTF8.GetBytes(password));

    key = rfcDb.GetBytes(strength); // strength value can be 16, 24 or 32 depending
    iv = rfcDb.GetBytes(strength);  // on the user choice


    result.Add(key);
    result.Add(iv);

    return result;
}

I then use my RijndaelManaged method this way :

RijndaelManaged rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
rijndael.BlockSize = Strenght * 8;
rijndael.KeySize = Strenght * 8;
ICryptoTransform aesEncryptor = rijndael.CreateEncryptor(key, iv); // key and iv from the other method

Do you see anything wrong ?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 3
    If you are reading files, the time is likely to be dominated by Disk I/O, so differences in encoding time may be rendered insignificant. – Matthew Watson Feb 10 '15 at 13:26
  • @MatthewWatson : that's what i thought too, so I tried on my hard drive instead of a usb drive (which read/write speed is limited) and it was the same. I will investigate further. ArtjomB. : thanks for the precision. –  Feb 10 '15 at 13:39

1 Answers1

2

AES-256 is using 14 rounds and AES-128 is using 10 rounds. The key schedule of course has a larger input, but that should not matter much. The state size during encryption/decryption is the same (as the block size is the same). So you should expect at most a speed difference of about 40 percent or so.

For smaller files: Rfc2898DeriveBytes uses HMAC with SHA-1 and 1000 (!) iterations. That will be much slower than the AES encryption. Besides that you'll have disk seek time and operating system calls to worry about.

For larger files you may well be limited by disk I/O, especially for a dog slow medium such as a hard disk or USB-2 drives. Replacing a USB-2 thumbdrive with a HDD won't help you much (if any).

To test the AES speed, encrypt from memory (or even the same buffer over and over), use a RAM-drive or buy an SSD (in order of preference for testing, I would always suggest to buy an SSD).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263