Thursday, June 11, 2009

Encryption and Decryption of string using Java

This article enlightens us with the code with the encryption and decryption of a string using java code.



Here we are using BASE64 encoder for encryption.


Here the trick is we are generating the key to encrypt and are saving the key into a serializable file (KeyWrite.ser). While decrypting the string, we will use the same key and decrypt it.


Without going into much detail, moving into the code.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EnDeCryption{
SecretKey globalKey;
static String encrypted;
EnDeCryption(){

}

private String doEnCryption(String string){
try {
globalKey = KeyGenerator.getInstance("DES").generateKey();
try {
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("KeyWrite.ser"));
out.writeObject(globalKey);
out.close();
} catch (IOException e) {
e.printStackTrace();
}

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

// Encrypt
System.out.println("To be Encrypted String in doEncryption:: "+string);
encrypted = encrypter.encrypt(string);
System.out.println("Encrypted String in doEncryption:: "+encrypted);
return encrypted;
} catch (Exception e) {
e.printStackTrace();
return "";
}

}
private void doDeCryption(String string){
try {
File file = new File("KeyWrite.ser");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
// Deserialize the object
SecretKey serSecretKey = (SecretKey) in.readObject();
in.close();

// Create encrypter/decrypter class
DesEncrypter encrypter = new DesEncrypter(serSecretKey);
// Decrypt
System.out.println("To be Decrypted String in doDecryption:: "+string);
String decrypted = encrypter.decrypt(string);
System.out.println("Decrypted String in doDecryption:: "+decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]){
EnDeCryption enDeCryption = new EnDeCryption();
String encryptedString = enDeCryption.doEnCryption("StringToBeEncrypted");
enDeCryption.doDeCryption(encryptedString);
}
}




class DesEncrypter {
Cipher ecipher;
Cipher dcipher;

DesEncrypter(SecretKey key) {
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

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

public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");

// Encrypt
byte[] enc = ecipher.doFinal(utf8);

// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
return "";
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
return "";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
} catch (java.io.IOException e) {
e.printStackTrace();
return "";
}
}

public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

// Decrypt
byte[] utf8 = dcipher.doFinal(dec);

// Decode using utf-8
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
return "";
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
return "";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
} catch (java.io.IOException e) {
e.printStackTrace();
return "";
}
}
}



Hope you all will find it useful;

No comments:

Total Pageviews