import javax.crypto.Cipher;import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public static void main(String[] args) {
// TODO code application logic here
try
{
String plainData = "This is a secret text";
String cipherText;
String decryptedText;
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
cipherText = encrypt(plainData, secretKey);
System.out.println("Plain Text :"+plainData);
System.out.println("cypher text : "+cipherText);
decryptedText = decrypt(cipherText, secretKey);
System.out.println("decrypted text : "+decryptedText);
} catch (Exception e)
{
e.printStackTrace();
}
}
public static String encrypt(String plainData, SecretKey secretKey) throws Exception
{
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] byteDataToEncrypt = plainData.getBytes();
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
return new BASE64Encoder().encode(byteCipherText);
}
public static String decrypt(String cipherData, SecretKey secretKey) throws Exception
{
byte[] data = new BASE64Decoder().decodeBuffer(cipherData);
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plainData = aesCipher.doFinal(data);
return new String(plainData);
}
No comments:
Post a Comment