How to loop faster through Dataset while using AES 256 algorithm c#.net? -
in c# in looping through 50k records. while looping calling aes 256 algorithm encrypt/decrypt number filed, after encrypting assigning encrypt/decrypt text same field. 50k records taking approximately 40 min. constraint performance. here code
public string encrypt(string plaintext) { try { _saltsize = 32; encrypt_key = "200911381f7899d2482ab61fe8d15684469b17fc690"; if (string.isnullorempty(plaintext)) { throw new argumentnullexception("plaintext"); } if (string.isnullorempty(encrypt_key)) { throw new argumentnullexception("password_to_encrypt_key"); } var keyderivationfunction = new rfc2898derivebytes(encrypt_key, _saltsize); byte[] saltbytes = keyderivationfunction.salt; byte[] keybytes = keyderivationfunction.getbytes(32); byte[] ivbytes = keyderivationfunction.getbytes(16); using (var aesmanaged = new aesmanaged()) { aesmanaged.keysize = 256; using (var encryptor = aesmanaged.createencryptor(keybytes, ivbytes)) { memorystream memorystream = null; cryptostream cryptostream = null; return writememorystream(plaintext, ref saltbytes, encryptor, ref memorystream, ref cryptostream); } } } catch (exception) { throw; } }
my guess similar this:
public static byte[] encryptrfc(byte[] plaintext, string password, byte[] salt) { var keygen = new rfc2898derivebytes(password, salt); var key = keygen.getbytes(32); var iv = keygen.getbytes(16); var cipher = new rijndaelmanaged { key = key, iv = iv }; byte[] ciphertext; using (var encryptor = cipher.createencryptor()) { using (var ms = new memorystream()) { using (var cs = new cryptostream(ms, encryptor, cryptostreammode.write)) { cs.write(plaintext, 0, plaintext.length); cs.flushfinalblock(); ciphertext = ms.toarray(); } } } return ciphertext; }
the problem here initialization of rfc2898derivebytes
, subsequent calls getbytes
takes considerable amount of time when generating first key. (see this blog post more information.)
what should lift out rfc2898derivebytes
method:
public static byte[] encryptrfc(string plaintext, byte[] key, byte[] iv) { var cipher = new rijndaelmanaged { key = key, iv = iv }; ... return ciphertext; }
and call encryption function key
, iv
... var keygen = new rfc2898derivebytes(password, salt); var key = keygen.getbytes(32); ... foreach (var line in alldatatoencryptcollectionofstrings) { ... var iv = keygen.getbytes(16); var encrypteddata = encryptrfc(plaintext, key, iv); ... }
this reduce execution time 1/4 of original time. if need run faster use same key , iv encryptions:
... var keygen = new rfc2898derivebytes(password, salt); var key = keygen.getbytes(32); var iv = keygen.getbytes(16); ... foreach (var line in alldatatoencryptcollectionofstrings) { ... var encrypteddata = encryptrfc(plaintext, key, iv); ... }
the last piece of code run in fraction of time (50 000 calls encryptrfc takes, on computer, 300ms).
mind though level of security decreases when using same iv multiple encryptions.
but guesswork since haven't provided code attempts.
Comments
Post a Comment