# Encryption of data using JSR-177

From Nokia Developer Wiki

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.

18 Sep

2009

Article Review by Larry101(20090918)This concise article demonstrates how to perform encryption and decryption easily using Java ME (and the JSR-177 API in particular). The article demonstrates the use of the DES symmetric encryption algorithm. The String "THIS IS A SECRET MESSAGE" is encrypted using the key “SECRET!!” (which must be 8 bytes in length).

The article shows how to convert a message to a byte array, initialize a Cypher object using a key, and perform encryption and decryption.