4

i want to Encrypt and Decrypt data in J2ME using AES Algorithm with bouncy castle can any one give me sample code for that

i want to use ECB with PKCS5Padding

Thanks in Advance.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Mihir Palkhiwala
  • 2,586
  • 3
  • 37
  • 47

1 Answers1

20

I'm sure there are examples out there but I haven't found them. Here are a few hints to help you get started. You need to learn how to connect the BC classes together. First, get the bouncycastle source code and be prepared to look at it when you have questions. It's actually very readable so don't be afraid to examine it when the documentation is poor. For example, many classes want an instance of a CipherParameters object, but it is rare for the documentation to specify any more detail. However, in the source code it will be obvious as to which implementing classes are expected.

Choose one of the AES engines, for example AESEngine, as the encryption engine. Next pick a mode; ECB is rarely correct, so for example if you pick CBC mode then create a CBCBlockCipher object from your AESEngine object. Next, use this object to create a PaddedBufferBlockCipher object. The default constructor uses PKCS7 padding which is identical to the PKCS5 padding you want. Now you need to create an object to hold the key and IV. This is the CipherParameters interface. You create the object in two steps. First, you create a KeyParameter object with your key. Next, you create a ParametersWithIV object with your KeyParameter object and your IV. This object is supplied to the init method of the PaddedBufferBlockCipher object and then your are ready to go.

EDIT

Here is small example:

private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
        throws Exception
{
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • hey thanks for your help, can you provide me some sample code for same?? – Mihir Palkhiwala Nov 23 '10 at 07:11
  • How to generate IV in J2me and what is the importance of IV?? – Mihir Palkhiwala Nov 25 '10 at 13:35
  • Sorry, I'm not going to provide you an encryption tutorial. – President James K. Polk Nov 25 '10 at 14:41
  • i understand algo and its content but only not getting what is the fundas of IV because as we are generating KEY of 128,192 or 256 they what is need of IV?? – Mihir Palkhiwala Nov 26 '10 at 06:45
  • 2
    Here is one explanation: [Initialization Vector](https://secure.wikimedia.org/wikipedia/en/wiki/Block_cipher_modes_of_operation#Initialization_vector_.28IV.29) – President James K. Polk Nov 26 '10 at 12:43
  • 1
    Even though late to the party, people like me come here from Googling. The Initialization Vector (iv) is usually filled random numbers that AES uses to help better encrypt. Just fill it with random numbers, and you may have to reuse the same vector to decrypt – Joe Plante Apr 03 '13 at 15:13
  • I tested your example with the following: byte[] enc = encrypt( "SECRET".getBytes(), key, iv ); String encrypted = new String( enc ); byte[] dec = decrypt( encrypted.getBytes(), key, iv ); but that throws org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption. any ideas why? – isapir Jun 29 '13 at 19:31
  • 1
    @Igal: `encrypted = new String( enc );` is not correct and will lose information. – President James K. Polk Jun 29 '13 at 21:54
  • ok, thanks. so what would be the correct way to convert the byte array into a string? – isapir Jun 30 '13 at 01:53
  • @Igal: Usually [base 64 encoding](http://stackoverflow.com/questions/13109588/base64-encoding-in-java) is the way to go. – President James K. Polk Jun 30 '13 at 01:55
  • If `AESEngine` is used, IV should be 16 bytes (128bits) long. The `key` must be 16,24,32 bytes (128,192,256 bits) – lepe Apr 18 '17 at 07:38
  • For anyone looking for RSA.. full example in #8: https://www.javatips.net/api/org.bouncycastle.crypto.engines.rsaengine – JAnton May 14 '20 at 09:38