BASE64编码器是内部API,可能会在将来的版本中删除
我试图解决这个问题,但我从来没有找到一个适合我的解决方案。问题是我对BASE64Encoder感到担忧。有没有其他方法可以在没有BASE64编码器的情况下做到这一点?
代码:
public static String Encrypt(String Data) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal); //Here is the problem
return encryptedValue;
}
public static String Decrypt(String encryptedData) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); //Another problem
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception
{
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}