Encryption of data using JSR-177

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: shaii (30 Mar 2008)
Last edited: hamishwillee (25 Jul 2013)


A quick example on how to encrypt and decrypt important data with JSR-177 support on MIDP.

byte[] msg = "THIS IS A SECRET MESSAGE".getBytes();
byte[] enMsg = new byte[10000];
byte[] deMsg = new byte[10000];
//create new cipher using DES algorithm
Cipher c = Cipher.getInstance("DES");
//our raw byte[] key - please note that since we use DES algorithm,
//the key must be 8 bytes long
byte[] b = "SECRET!!".getBytes();
//init the cipher to encrypt the data
c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(b,0,b.length,"DES"));
int numBytes = c.doFinal(msg, 0, msg.length, enMsg, 0);
//init the cipher to decrypt the data
c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(b,0,b.length,"DES"));
numBytes = c.doFinal(enMsg, 0, numBytes, deMsg, 0);
String s = new String(deMsg,0,numBytes);

at the end of this code, object s should be equal to "THIS IS A SECRET MESSAGE".

In the above example we used symmetric (algorithm) encryption which means it's the same amount of time needed to encrypt or decrypt unlike a-symmetric algorithm such as RSA.

This page was last modified on 25 July 2013, at 06:27.
58 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.