Android:在电子邮件应用程序中实施基本加密
Android: implementing basic encryption into email application
我目前正在构建一个电子邮件应用程序,作为其中的一部分,我需要实施一些基本的电子邮件加密,以确保它们被安全地发送和接收任何帮助都非常感谢,因为我对它的了解不多android 或 java 加密。
使用这些函数:
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;
}
我目前正在构建一个电子邮件应用程序,作为其中的一部分,我需要实施一些基本的电子邮件加密,以确保它们被安全地发送和接收任何帮助都非常感谢,因为我对它的了解不多android 或 java 加密。
使用这些函数:
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;
}