I am using AES/ECB/PKCS5Padding in java.
But BlackBerry requires onle AES encryptor engine and PKCS5Formatter engine.
ECB cipher mode is default in blackberry.
So can you please guide me to achieve AES/ECB/PKCS5Padding in Blackberry.
/*
* BasicCryptoDeveloperLab.java
*
*/
package com.rim.samples.crypto;
import java.io.;
import net.rim.device.api.crypto.;
import net.rim.device.api.util.*;
public class BasicCryptoDeveloperLab
{
public static void main( String[] args )
{
try {
// Create the data that you want to encrypt.
String message = "Welcome to the RIM Crypto API.";
byte[] data = message.getBytes();
byte[] keyData = "Create AES Key".getBytes();
// Encrypt the data using
byte[] ciphertext = encrypt( keyData, data );
// Decrypt the data.
byte[] plaintext = decrypt( keyData, ciphertext );
String message2 = new String( plaintext );
if( message.equals( message2 )) {
// The encryption/decryption operation worked as expected.
System.out.println( "Congratulations! You just encrypted and decrypted data." );
} else {
System.out.println( "Oops. The decrypted message should equal the original.
Check your code." );
}
} catch( CryptoException e ) {
System.out.println("An unexpected exception occurred.
Please verify your work or ask for help.");
} catch( IOException e ) {
System.out.println("An unexpected exception occurred.
Please verify your work or ask for help.");
}
}
private static byte[] encrypt( byte[] keyData, byte[] data )
throws CryptoException, IOException
{
// Create the AES key to use for encrypting the data.
// This will create an AES key using as much of the keyData
// as possible.
AESKey key = new AESKey( keyData );
AESEncryptorEngine engine = new AESEncryptorEngine( key );
PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
ByteArrayOutputStream output = new ByteArrayOutputStream();
BlockEncryptor encryptor = new BlockEncryptor( fengine, output );
encryptor.write( data );
encryptor.close();
output.close();
return output.toByteArray();
}
private static byte[] decrypt( byte[] keyData, byte[] ciphertext )
throws CryptoException, IOException
{
AESKey key = new AESKey( keyData );
AESDecryptorEngine engine = new AESDecryptorEngine( key );
PKCS5UnformatterEngine uengine = new PKCS5UnformatterEngine( engine );
ByteArrayInputStream input = new ByteArrayInputStream( ciphertext );
BlockDecryptor decryptor = new BlockDecryptor( uengine, input );
byte[] temp = new byte[ 100 ];
DataBuffer buffer = new DataBuffer();
for( ;; ) {
int bytesRead = decryptor.read( temp );
buffer.write( temp, 0, bytesRead );
if( bytesRead < 100 ) {
// We ran out of data.
break;
}
}
byte[] plaintextAndHash = buffer.getArray();
return plaintextAndHash ;
}
}
Thanks
Deepak