5

I want to port this simple JAVA example...

AES Encryption/Decryption with Bouncycastle Example in J2ME

...to C# and have the two following 3 questions:

  1. As I understand, the JAVA example uses AESEngine for encryption/decryption operations. What is the difference between AESEngine and AESFastEngine and AESLightEngine? Unfortunately I don't understand the information given in the documentation: http://www.bouncycastle.org/docs/docs1.6/index.html

  2. I want to use a new encryption-key for every file I encrypt. Which block cipher modes of operation should I use: AES.CBC, AES.CFB, AES.ECB OR AES.OFB http://www.bouncycastle.org/docs/docs1.6/index.html

  3. Is my assumption correct that in my case I don't have to use an iv / salt (which means I have to use a static iv?) since I use AES.KeyGen128() for key generation and use it only once?
    http://www.bouncycastle.org/docs/docs1.6/index.html

Hope my questions do not cause too much confusion ;-) I but I appreciate every answer, clarification or feedback you can give me.

Mike

Community
  • 1
  • 1
Mike
  • 1,992
  • 4
  • 31
  • 42

1 Answers1

5
  1. My reading of the doc says that the AESEngine, FastEngine and LightEngine all take different tradeoffs of memory versus speed. You would have to test it yourself to determine if those tradeoffs are even relevant in your scenario.

  2. you will need to read up on the various AES modes. Different modes have different strengths and attributes, which may be more or less applicable or desirable depending on your scenario. So the answer to your question is "it depends."

  3. no. you will need an IV. As far as the salt, it is usually employed with the passphrase to generate the actual encryption key and the IV, often via PKBDF2. That is outside the realm of AES, but it is a typical usage.


Finally you didn't ask, but.... why are you porting that code to C#? .NET has AES encryption built-in. You don't need to port anything, you can just use the .NET base class library. Just ensure you use the same keysize and mode, and make sure your key+iv is the same on each side, and the .NET BCL AES classes will interoperate with the BouncyCastle stuff for J2ME.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Re modes. Do __not__ use ECB mode, it is insecure. CBC mode requires an IV, though you can usually get the system to generate one for you. CTR mode requires a nonce, which is similar to an IV but not exactly the same. – rossum Sep 02 '11 at 10:45
  • Thanks for the detailed answer. Since they are built in, do you think the .NET AES encryption methods are faster? – Mike Sep 12 '11 at 14:51
  • 4
    They're pretty fast, but ... is speed really a critical factor for you? Just measure it and see if it suffices. ps: ECB isn't always insecure and can be used as a basis for higher-level mechanisms. For example, a CTR mode is not included in the AES encrytion library that's built-in to .NET, but you can use ECB to build a CTR. See here for some comments on that: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aesmanaged(v=vs.90).aspx#2 – Cheeso Sep 12 '11 at 14:59
  • You can use CBC to build up CTR as well...I would avoid ECB just out of principle :) – Jeff Apr 09 '13 at 19:58
  • @Mike See this to compare the performance of the implementations https://github.com/sidshetye/BouncyBench – makerofthings7 Mar 19 '14 at 01:25