analytics

Saturday, April 9, 2011

Data Encryption Standard (DES)

Data Encryption Standard


The Data Encryption Standard (DES) is a block cipher that uses shared secret encryption. It was selected by the National Bureau of Standards as an official Federal Information Processing Standard (FIPS) for the United States in 1976 and which has subsequently enjoyed widespread use internationally. It is based on a symmetric-key algorithm that uses a 56-bit key. The algorithm was initially controversial because of classified design elements, a relatively short key length, and suspicions about a National Security Agency (NSA) backdoor. DES consequently came under intense academic scrutiny which motivated the modern understanding of block ciphers and their cryptanalysis.


DES is now considered to be insecure for many applications. This is chiefly due to the 56-bit key size being too small; in January, 1999, distributed.net and the Electronic Frontier Foundation collaborated to publicly break a DES key in 22 hours and 15 minutes. There are also some analytical results which demonstrate theoretical weaknesses in the cipher, although they are infeasible to mount in practice. The algorithm is believed to be practically secure in the form of Triple DES, although there are theoretical attacks. In recent years, the cipher has been superseded by the Advanced Encryption Standard (AES). Furthermore, DES has been withdrawn as a standard by the National Institute of Standards and Technology (formerly the National Bureau of Standards).

In this article, I show you how do use Java Cryptography Extension (JCE) to encrypt or decrypt a text in Data Encryption Standard (DES) mechanism.

1. DES Key


Create a DES Key.

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

2. Cipher Info

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

• Algorithm name
• Mode (optional)
• Padding scheme (optional)

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

Note

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

In this case, you created a DES (Data Encryption Standard) cipher in Electronic Codebook mode, with PKCS #5-style padding.

3. Convert It

Convert String into Byte[] array format.

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

4. Encrypt It

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

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

5. Decrypt It

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);

6. Full Example


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

package com.roy.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);

// Encode bytes to base64 to get a string
// new sun.misc.BASE64Encoder().encode(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));

// Decode using utf-8
// 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

 
 
Encrypting a File or Stream with DES


This example implements a class for encrypting and decrypting files or streams using DES. The class is created with a key and can be used repeatedly to encrypt and decrypt streams using that key.

public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;

DesEncrypter(SecretKey key) {
// Create an 8-byte initialization vector
byte[] iv = new byte[]{
(byte)0x8E, 0x12, 0x39, (byte)0x9C,
0x07, 0x72, 0x6F, 0x5A
};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

try {
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

// CBC requires an initialization vector
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

} catch (java.security.InvalidAlgorithmParameterException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {

}
}

// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out) {
try {

// Bytes written to out will be encrypted
out = new CipherOutputStream(out, ecipher);

// Read in the cleartext bytes and write to out to encrypt
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}

out.close();
} catch (java.io.IOException e) {

}
}

public void decrypt(InputStream in, OutputStream out) {

try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}

out.close();
} catch (java.io.IOException e) {
}
}
}



Here's an example that uses the class:

try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();

// Create encrypter/decrypter class
DesEncrypter encrypter = new DesEncrypter(key);

// Encrypt
encrypter.encrypt(new FileInputStream("cleartext1"),
new FileOutputStream("ciphertext"));

// Decrypt
encrypter.decrypt(new FileInputStream("ciphertext"),
new FileOutputStream("cleartext2"));
} catch (Exception e) {
}




Encrypting an Object with DES

This example demonstrates how to encrypt a serializable object.

try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();

// Prepare the encrypter
Cipher ecipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);

// Seal (encrypt) the object
SealedObject so = new SealedObject(new MySecretClass(), ecipher);

// Get the algorithm used to seal the object
String algoName = so.getAlgorithm(); // DES

// Prepare the decrypter
Cipher dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);

// Unseal (decrypt) the class
MySecretClass o = (MySecretClass)so.getObject(dcipher);
} catch (java.io.IOException e) {
} catch (ClassNotFoundException e) {
} catch (javax.crypto.IllegalBlockSizeException e) {
} catch (javax.crypto.BadPaddingException e) {
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
}


public class MySecretClass implements java.io.Serializable {
String s = "the secret";
}

2 comments: