Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
funkym
Posts: 39
Registered: ‎04-01-2008
My Device: Not Specified
Accepted Solution

AESEncryptorEngine and Unicode Characters

Hi,

 

I am using the AESEncryptorEngine and AESDecryptorEngine to encrypt and decrypt normal text strings that have been entered by the user in an ActiveAutoTextEditField. The problem is: users who enter non-English characters like Japanese or Chinese can encrypt their messages, but after decryption the characters appear as rubbish or sometimes they are just question marks like "?????". It seems line breaks come back properly. 

 

My encryption and decryption methods work fine for Latin characters but not if it gets more complicated. Perhaps someone knows what I am missing here? :smileyhappy: 

 

This is the encryption method: 

 

 

public static String encrypt(String keyString, String plainText) { String encryptedText = null; byte[] keyData = keyString.getBytes(); try { AESKey key = new AESKey( keyData ); // Now, we want to encrypt the data. // First, create the encryptor engine that we use for the actual // encrypting of the data. AESEncryptorEngine engine = new AESEncryptorEngine( key ); PKCS5FormatterEngine formatterEngine = new PKCS5FormatterEngine(engine); // Use the byte array output stream to catch the encrypted information. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BlockEncryptor encryptor = new BlockEncryptor(formatterEngine, outputStream); // Encrypt the actual data. encryptor.write(plainText.getBytes()); // Close the stream. encryptor.close(); byte[] encryptedData = outputStream.toByteArray(); encryptedText = new String(encryptedData); } catch (Exception e) { System.err.println(e.toString()); } return encryptedText; }

 

This is the decryption method:

 

 

 

public static String decrypt(String keyString, String encryptedText) { String plainText = null; byte[] keyData = keyString.getBytes(); try { //remove prefix which indicates this is encrypted text encryptedText = encryptedText.substring(ENCRYPTION_PREFIX.length()); AESKey key = new AESKey( keyData ); // Now, create the decryptor engine. AESDecryptorEngine engine = new AESDecryptorEngine( key ); // Create the unformatter engine that will remove any of the padding bytes. PKCS5UnformatterEngine unformatterEngine = new PKCS5UnformatterEngine(engine); // Set up an input stream to hand the encrypted data into the block decryptor. ByteArrayInputStream inputStream = new ByteArrayInputStream(encryptedText.getBytes()); // Create the block decryptor passing in the unformatter engine and the encrypted data. BlockDecryptor decryptor = new BlockDecryptor(unformatterEngine, inputStream); 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; } } inputStream.close(); plainText = new String(buffer.getArray()); } catch (Exception e) { System.err.println(e.toString()); } return plainText; }

 

 

 

 

 

P.S.: I know that for loop in the decryption method is not really nice, but it was the only one that was working for me. :smileyhappy:

Please use plain text.
Developer
Developer
gtj
Posts: 319
Registered: ‎07-20-2008
My Device: Not Specified

Re: AESEncryptorEngine and Unicode Characters

When you do your string/byte conversions, specify an encoding...

 

plainText.getBytes("UTF-16");

 

plainText = new String(buffer.getArray(), "UTF-16"); 

 

Please use plain text.
Developer
funkym
Posts: 39
Registered: ‎04-01-2008
My Device: Not Specified

Re: AESEncryptorEngine and Unicode Characters

Thanks man! That's awesome ... it was exactly what I needed! Though I used UTF-8 to save some (50%) space it works fine.

 

 

Just out of curiosity: I thought all strings in Java are unicode? Didn't know we need to specificly give them the encoding. 

 

Please use plain text.
Developer
Developer
gtj
Posts: 319
Registered: ‎07-20-2008
My Device: Not Specified

Re: AESEncryptorEngine and Unicode Characters

Strings are Unicode but when you convert them to byte arrays, you lose the encoding unless you explicitly specify which encoding scheme to use.

 

Please use plain text.
New Contributor
amontilla
Posts: 2
Registered: ‎02-16-2011
My Device: Blackberry Bold 2 - 9700
My Carrier: Movistar

Re: AESEncryptorEngine and Unicode Characters

Hi funkym, thanks for sharing your code! It looks very useful for me, I have only one question, in the following line:

 

encryptedText = encryptedText.substring(ENCRYPTION_PREFIX.length());
Where do you take "ENCRYPTION_PREFIX" constant from? i can't find in the API nor any post in the web, only yours.
Thanks in advance.

 

Please use plain text.
New Contributor
dandollas
Posts: 9
Registered: ‎02-13-2012
My Device: Tour 9630
My Carrier: MTN Nigeria

Re: AESEncryptorEngine and Unicode Characters

I have a similar case...i am working with files...a video file to be specific. It decrypts the file but the file is unuseable or corrupt...pls what do i do??

Please use plain text.