0

I am making an application in blackberry. In that application i want to encrypt a string using AES algorithm. Is it possible to use AES algorithm in Blackberry? Is there any API for that? Thanks in advance,

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Dany
  • 2,034
  • 8
  • 34
  • 54

3 Answers3

3

try this -

useremail= CryptAes.AESEncryption(username_.getBytes());

CryptAes class is given below -

public class CryptAes {
// First create the AES key based on the bytes in secretKey using  keyLength bits as the length
static AESKey keydec = new AESKey("A3$1E*81234567891111111111111111".getBytes() );
static AESKey keyenc = new AESKey("A3$1E*81234567891111111111111111".getBytes() );
static AESKey keyenc128 = new AESKey("A3Q1EF8123456789".getBytes());
static AESKey keydec128 = new AESKey("A3Q1EF8123456789".getBytes());

private static byte[] iv = { 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c,
    0x0d, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };

public static byte[] plainText= new byte[10000];

public static String AESEncryption(byte[] plainText) throws CryptoException, IOException {
      AESEncryptorEngine engine = new AESEncryptorEngine( keyenc128 );
      CBCEncryptorEngine cengine=new CBCEncryptorEngine(engine, new InitializationVector(iv));
      PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      BlockEncryptor encryptor = new BlockEncryptor( fengine, output );

      encryptor.write(plainText);
      encryptor.close();
      byte[] encryptedData = output.toByteArray(); output.close();
      String st=new String(encryptedData);

      byte[] base64 = Base64OutputStream.encode(encryptedData, 0, encryptedData.length, false, false);

        //Base64Coder.encodeString(Byte.toString(plainText));
        String str = new String(base64);

   return str;
}


// sampleAESDecryption
public static String AESDecryption(byte[] cipherText, int dataLength ) throws CryptoException, IOException {

    // Create the input stream based on the ciphertext
    ByteArrayInputStream in = new ByteArrayInputStream( cipherText, 0, dataLength );

    // Now create the block decryptor and pass in a new instance
    // of an AES decryptor engine with the specified block length
    BlockDecryptor cryptoStream = new BlockDecryptor(new AESDecryptorEngine( keydec128 ), in );

    byte[] T= new byte[dataLength];
    // Read the decrypted text from the AES decryptor stream and
    // return the actual length read

    int length= cryptoStream.read( T );
  String str= new String(T);

  int i=str.indexOf("</msg>");
  str=str.substring(0,i+6);
  return str;
}
}
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
  • see this is my approach, very simple : http://stackoverflow.com/questions/25862812/aes-encryption-blackberry ... But i am getting lang.error – Noman Sep 16 '14 at 07:33
2

Look into AESEncryptorEngine and AESDecryptorEngine (with a help of Google).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

Alternatively you may consider to use bouncy castle for j2me, as suggested here.

Community
  • 1
  • 1
rosco
  • 939
  • 9
  • 16