Search code examples
c#.netperformanceencryptionbouncycastle

Which BouncyCastle API-supported Encryption algorithm is fastest for short string encryption in C# .NET and pretty safe?


I tried to search stackoverflow for an answer to this particular question but couldn't find a good answer.

The BouncyCastle API offers a ton of different encryption algorithms for .NET. In this case I have to select an encryption algorithm for the following use case:

  1. Encrypting several thousand short strings for storage in an unencrypted file, typical length 10-30 characters.

  2. Encryption needs to be only moderately secure, all strings will be encrypted with the same key but a different initialization vector. Things like authorization (like in AES-GCM) are not needed.

Which encryption algorithm is both fastest and straightforward to apply for encrypting and decrypting such a set of several thousand small strings? I will store the encrypted data of each string as BASE64 in the file.

Thank you for your advice!


Solution

  • That processor still has AES-NI, so using that through the AES.Create seems most logical. A higher end solution would be to create CTR mode out of ECB (and cache the key streams).

    Otherwise you are looking for a fast stream cipher in software. You could check if Salsa20 is working for you. Unfortunately that's the only eStream-compatible cipher that I can find in Bouncy Castle for C#.

    Note that you may want to look into multi-threading and I would certainly check if it is possible to use sequential nonce when using a stream cipher, as generating a 128 bit random value for each encryption seems wasteful.