I have this code where I am trying to encode and decode and string in java, but am getting compile errors, here is the code with the errors commented in the code:
public static String encrypt(String plainText, SecretKey secretKey)
throws Exception {
byte[] plainTextByte = plainText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedByte = cipher.doFinal(plainTextByte);
Encoder encoder = Base64.getEncoder(); //ERROR "cannot resolve method"
String encryptedText = encoder.encodeToString(encryptedByte);
return encryptedText;
}
public static String decrypt(String encryptedText, SecretKey secretKey)
throws Exception {
Decoder decoder = Base64.getDecoder(); //ERROR "cannot resolve method"
byte[] encryptedTextByte = (byte[]) decoder.decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
return decryptedText;
}
Thanks for the help in advance
getBytes("UTF-8")should this app at some time run on a non-UTF-8 platform, like Windows.