Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Encryption of data using JSR-177
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;
byte deMsg = new byte;
//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.