simple aes encryption and decryption

984 views
Skip to first unread message

Sam Ace

unread,
Jul 26, 2014, 5:12:34 PM7/26/14
to codenameone...@googlegroups.com
hello i want to encrypt and decrypt strings using bouncy castle api.. i have seen so many examples online but its either it isnt supported or having slight errors or discussion is so long u dont understand d whole thing anymore... can anyone show me how use AES to encrypt and decrypt a simple string???

Shai Almog

unread,
Jul 27, 2014, 2:16:43 AM7/27/14
to codenameone...@googlegroups.com
Hi,
there are some discussions in the forum on this. Use the J2ME aes samples as reference.

Sam Ace

unread,
Jul 27, 2014, 5:22:03 AM7/27/14
to codenameone...@googlegroups.com
hi shai.. i did that and all ova d net.. i got this eample
http://stackoverflow.com/questions/4243650/aes-encryption-decryption-with-bouncycastle-example-in-j2me?rq=1
 crypting works fine but i am having issues decrypting crypted string

Sam Ace

unread,
Jul 27, 2014, 7:06:32 AM7/27/14
to codenameone...@googlegroups.com
here is my code:
import com.codename1.util.Base64;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;

/**
 *
 * @author SAMUEL
 */
public class Tester {
    static  String strEnc = "Hi This is my String";
    final static String strPassword = "password12345678";
 
    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);
}


public static void main(String [] args) throws Exception{
  
    byte[] enc= encrypt(strEnc.getBytes(),"password12345678".getBytes(), "password12345678".getBytes());
    String encrypted =   Base64.encode(enc);
    System.out.println("Encrypted is:"+encrypted);
     byte[] dec= decrypt(encrypted.getBytes(),"password12345678".getBytes() , "password12345678".getBytes());       
    System.out.println("Decrypted file is:"+dec);
   
  
      }

  
}
     but this is netbeans output:
Encrypted is:sw0SrUIKe0DmS7sRd9+XMgtYg+BUiAfiOsdMw/Lo2RA=
Exception in thread "main" org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
    at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(PaddedBufferedBlockCipher.java:281)
    at com.mycompany.myapp.Tester.cipherData(Tester.java:28)
    at com.mycompany.myapp.Tester.decrypt(Tester.java:40)
    at com.mycompany.myapp.Tester.main(Tester.java:57)


.. encryption is sucessful but decryption isnt.. how can i decrypt back to actual string?

Shai Almog

unread,
Jul 27, 2014, 11:05:20 AM7/27/14
to codenameone...@googlegroups.com
Maybe this fails because you base64 the data and don't do the reverse when decrypting.

Sam Ace

unread,
Jul 27, 2014, 11:53:55 AM7/27/14
to codenameone...@googlegroups.com
i found the solution to this : it was cos i didnt specify the encoding.. here is the right way go about it


   byte[] enc= encrypt(strEnc.getBytes(),"password12345678".getBytes(), "password12345678".getBytes());
    String encbase =  Base64.encode(enc);
    String encrypted =new String(encbase.getBytes(), "UTF-8");
   
    System.out.println("Encrypted is:"+encbase);
    byte[] decbase = Base64.decode(encrypted.getBytes());
     byte[] dec= decrypt(decbase,"password12345678".getBytes() , "password12345678".getBytes());       
    System.out.println("Decrypted file is:"+new String(dec, "UTF-8"));
    ... anyway thanks shai

c.jav...@gmail.com

unread,
Feb 16, 2015, 8:10:48 AM2/16/15
to codenameone...@googlegroups.com
Shai, please how can one do Encryption and Decryption in Codename one? any sample code please?

Shai Almog

unread,
Feb 16, 2015, 12:14:43 PM2/16/15
to codenameone...@googlegroups.com, c.jav...@gmail.com
What's wrong with the code above?

fidod...@gmail.com

unread,
Dec 29, 2016, 8:14:19 PM12/29/16
to CodenameOne Discussions
Hello Sam, do you have a .jar file, I need the library for use this example on my gemalto modem, but I don´t found the jar file. thank you.

Shai Almog

unread,
Dec 30, 2016, 12:14:13 AM12/30/16
to CodenameOne Discussions, fidod...@gmail.com
Reply all
Reply to author
Forward
0 new messages