I want to encrypt data in android to save them in shared preferences or send them to the server.
This is a class which I wrote for this, but I don't know any other ways to make it more perfect and secure.
Please tell me your ideas!
public class Cryptogram {
private static byte[] iv;
private static IvParameterSpec ivspec;
public static byte[] Encrypt(String txt, SecretKey key) {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(txt.getBytes("UTF-8"));
iv = cipher.getIV();
ivspec = new IvParameterSpec(iv);
return cipherText;
} catch (Exception e) {
return e.toString().getBytes(Charset.forName("UTF-8"));
}
}
public static String Decrypt(byte[] txt, SecretKey key) {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", Security.getProvider("BC"));
cipher.init(Cipher.DECRYPT_MODE, key, ivspec);
String decryptString = new String(cipher.doFinal(txt), "UTF-8");
return decryptString;
} catch (Exception e) {
return e.toString();
}
}
}
Full project source here.