I'm making a system that has some data in it, and I want it to take this data (in XML format) and save it as an encrypted string in a txt file, and later on when the software opens again, decrypt the file and read it as normal. I already have all the code to convert the xml to a string, I already have the code to save it, I just need some help with the encryption/decryption code?
Note: I did find some code to encrypt/decrypt, but it seems that I can't split the code into 2 methods.
Here is my attempt:
public class AesEncrDec
{
public static String encrypt(String Data)
{
byte[] byteCipherText = null;
try {
String plainData=Data,cipherText,decryptedText;
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecureRandom rnd = new SecureRandom();
SecretKey secretKey = keyGen.generateKey();
IvParameterSpec iv;
iv = new IvParameterSpec(rnd.generateSeed(16));
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
byte[] byteDataToEncrypt = plainData.getBytes();
byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
cipherText = new BASE64Encoder().encode(byteCipherText);
return new String(byteCipherText);
} catch (InvalidKeyException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
}
return new String(byteCipherText);
}
public static String dencrypt(String Data)
{
byte[] byteDecryptedText = null;
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
IvParameterSpec iv;
SecureRandom rnd = new SecureRandom();
iv = new IvParameterSpec(rnd.generateSeed(16));
Cipher aesCipher = Cipher.getInstance("AES");
SecretKey secretKey = keyGen.generateKey();
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
byteDecryptedText = aesCipher.doFinal(Data.getBytes());
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(AesEncrDec.class.getName()).log(Level.SEVERE, null, ex);
}
return new String(byteDecryptedText);
}
}
EDIT : In responce to @Libin here are the errors
Mar 24, 2014 6:27:42 PM PrefsReadAndWrite.AesEncrDec decrypt
SEVERE: null
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2121)
at PrefsReadAndWrite.AesEncrDec.decrypt(AesEncrDec.java:61)
at PrefsReadAndWrite.AesEncrDec.decryptedString(AesEncrDec.java:104)
at smarthouse.SmartHouse.main(SmartHouse.java:12)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:554)
at PrefsReadAndWrite.AesEncrDec.decryptedString(AesEncrDec.java:105)
at smarthouse.SmartHouse.main(SmartHouse.java:12)
Java Result: 1