package jijo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import org.apache.log4j.Logger;
public class Encrypter {
static Cipher ecipher = null;
static Cipher dcipher = null;
// private static final byte[] SHARED_KEY = hexstringToByteArray("BAC464B197083EE626273DC4C9EBD8AE82E0897E3D8388EE06CB3EF7BCFFF458");
private static Logger logger = Logger.getLogger(Encrypter.class);
public Encrypter(String path) {
try {
Key key = getSecretKeyFromFile(path);
ecipher = Cipher.getInstance("AES");
dcipher = Cipher.getInstance("AES");
/*byte[] iv = new byte[16];
System.arraycopy(SHARED_KEY, 0, iv, 0, 16);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
//String myAES = getSecretKey().toString();
//System.out.println("hello key is"+myAES.toString());
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);*/
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();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
encrypt object
*/
public Object encrypt(EncVO encVO) {
SealedObject so = null;
try {
so = new SealedObject(encVO, ecipher);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return so;
}
public static void generateKeyFile() {
KeyGenerator kg = null;
ObjectOutputStream out = null;
try {
kg = KeyGenerator.getInstance("AES");
kg.init(128);
// 128 is the keysize. Fixed for AES
// BASE64Decoder bd = new sun.misc.BASE64Decoder();
// SecretKeyFactory desFactory = SecretKeyFactory.getInstance("AES");
// DESKeySpec keyspec = new DESKeySpec(bd.decodeBuffer("JIJO"));
// Key key = new SecretKeySpec(SHARED_KEY, "AES");
// SecretKey key1 = desFactory.generateSecret(keyspec);
SecretKey key = kg.generateKey();
out = new ObjectOutputStream(new FileOutputStream("C:/key/aes.key")); //Storage of AES Key to File
// out.writeObject(key);
out.writeObject(key);
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static Key getSecretKeyFromFile(String keyFile) throws Exception
{
ObjectInputStream in = null;
Key key = null;
try{
if(keyFile != null){
in = new ObjectInputStream(new FileInputStream(keyFile));
key = (Key) in.readObject();
in.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
return key;
}
/**
decrypt object
*/
public EncVO decrypt(Object obj) {
EncVO encVO = null;
try {
SealedObject sealedObject = (SealedObject)obj;
encVO = (EncVO)sealedObject.getObject(dcipher);
} catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return encVO;
}
public static void main(String args[]){
try {
EncVO encVO = new EncVO();
encVO.setStr("jijo");
generateKeyFile(); //generate key file
Encrypter encrypter = new Encrypter("C:/key/aes.key");
Object obj = encrypter.encrypt(encVO); //encrypt
EncVO encVO2 = encrypter.decrypt(obj); //decrypt
System.out.println(encVO2.getStr());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/*****************************/
/**
*
*/
package jijo;
import java.io.Serializable;
/**
* @author jijo
*
*/
public class EncVO implements Serializable {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment