我目前正在构建一个电子邮件应用程序,作为其中的一部分,我需要实现一些基本的电子邮件加密,以确保它们被安全地发送和接收任何帮助表示赞赏,因为我对android没有太多了解或java加密。
答案 0 :(得分:0)
使用以下功能:
private static final String ALGORITHM = "AES";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
public String encrypt(String valueToEnc) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
// String encryptedValue = new Base64.encoder();
return Base64.encodeToString(encValue, Base64.DEFAULT);
}
public String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
//byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decordedValue = Base64.decode(encryptedValue, Base64.DEFAULT);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}