Namespaces

Variants
Actions

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

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

Example

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 03:27.
41 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.

×