Friday, June 18, 2010

JCE Encryption – Data Encryption Standard (DES)



Here i demonstrate how do use the Java Cryptography Extension (JCE) to encrypt and decrypt a text in Data Encryption Standard (DES) mechanism.

1.) Create a DES Key.

    KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();

2.) Create a Cipher instance from Cipher class, we have to specify the following information and separated by a slashes (/).

1. Algorithm name
2. Mode (optional)
3. Padding scheme (optional)

    Cipher desCipher;
// Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

DES = Data Encryption Standard
ECB = Electronic Codebook mode
PKCS5Padding = PKCS #5-style padding

In this example, we create a DES (Data Encryption Standard) cipher in Electronic Codebook mode, with PKCS #5-style padding.

3.) Convert String into Byte[] array format.

    byte[] text = "No body can see me".getBytes();

4.) Make Cipher in encrypt mode, and encrypt it with Cipher.doFinal() method.

    desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] textEncrypted = desCipher.doFinal(text);

5.) Make Cipher in decrypt mode, and decrypt it with Cipher.doFinal() method as well.

    desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desCipher.doFinal(textEncrypted);

Full Example of how do use Java's JCE to encrypt and decrypt text in DES mechanism.

package com.mkyong.util;
 
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
 
public class JEncrytion
{
public static void main(String[] argv) {
 
try{
 
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
 
Cipher desCipher;
 
// Create the cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
 
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
 
//sensitive information
byte[] text = "No body can see me".getBytes();
 
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
 
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
 
System.out.println("Text Encryted : " + textEncrypted);
 
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
 
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
 
System.out.println("Text Decryted : " + new String(textDecrypted));
 
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
 
}
}

Output

Text [Byte Format] : [B@19b5393
Text : No body can see me
Text Encryted : [B@4e79f1
Text Decryted : No body can see me

No comments:

Post a Comment