正在为 Android 寻找 encrypt/decrypt AES 示例

Looking for an encrypt/decrypt AES example for Android

由于我是加密新手,尤其是 Java/Android,我正在努力寻找可以正常工作的教程和代码,以便我可以从中学习,但是结果。

如本网站所示:https://www.owasp.org/index.php/Using_the_Java_Cryptographic_Extensions

我找不到用 BASE64Encoder class 打我的问题,它似乎在包 a sun.utils 中,但我可以找到 Base64 class 但我无法调整代码使其适用于我。

同样在这个

android encryption/decryption with AES

加密是在Bitmap Image中完成的我无法在普通文本字符串中实现相同的技术。

有人会在 Android 中提供一个简单的 AES encryption/decryption 示例来展示如何使用密钥、消息、加密和解密吗?

I couldn't find the problem that hit me with BASE64Encoder class, which seems to be inside package a sun.utils but i can find Base64 class but i could not tweak code so that it could work for me.

您可能没有使用正确的 Base64 库。您提到 sun.utils,您发送的 link 使用的是:

import org.apache.commons.codec.binary.Base64;

Java 8 开始,您可以使用 java.util.Base64,详见 Oracle 文档 here. It supports Basic encoding, URL encoding and MIME encoding. You can find some examples in this tutorial

加密和解密过程在以下情况下运行良好: 我替换了从 :

获得的字节数组

Bitmap byte array of message string 按照

中的指示

android encryption/decryption with AES

所以即使我的问题没有完全解决,这个问题也应该标记为已回答。

我已经将它用于我的项目。

'com.scottyab:aescrypt:0.0.1'(ref: [enter link description here][1]

加密:

String encryptedMsg = AESCrypt.encrypt(password, message);

解密:

String messageAfterDecrypt = AESCrypt.decrypt(password, encryptedMsg);

如果您更关心安全性,请使用 SHA1 或 SHA256。 希望这有帮助。

编辑: 就我而言,我必须加密登录密码并通过网络将其发送到服务器。

String userPassword = password.getText().toString();
try {
        encryptedMsg = AESCrypt.encrypt(userPassword, Config.secretlogin);
   } catch (GeneralSecurityException e) {
        e.printStackTrace();
   }

所以在后端,我已经用密钥解密了安全密码,并且在双方我都使用相同的 AES(Android 和后端)。

第二个示例应该适用于文本字符串。替换

byte[] b = baos.toByteArray();

byte[] b = yourString.getBytes();

请注意,您必须以某种方式存储密钥,因为在某些时候必须解密

存储在设备上不是一个好主意,因为您要把钥匙留在门上。您可以存储在您的服务器上或使用密码(固定或询问用户)。我给你最后一个选项的真实示例

private static String getPassphraseSize16(String key) {
    if (TextUtils.isEmpty(key)) {
        return null;
    }
    char controlChar = '\u0014';
    String key16 = key + controlChar;
    if (key16.length() < 16) {
        while (key16.length() < 16) {
            key16 += key + controlChar;
        }
    }
    if (key16.length() > 16) {
        key16 = key16.substring(key16.length() - 16, key16.length());
    }
    return key16;
}


public static byte[] encodeAES(byte[] message, String passphrase) {
    String passphrase16 = getPassphraseSize16(passphrase);
    SecretKeySpec secretKey = new SecretKeySpec(passphrase16.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encodedText = cipher.doFinal(message);

    return encodedText;
}


public static byte[] decodeAES(byte[] encodedMessage, String key) {
    String passphrase16 = getPassphraseSize16(key);
    SecretKeySpec secretKey = new SecretKeySpec(passphrase16.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] decodedText = cipher.doFinal(encodedMessage);

    return decodedText;
}

此示例类似于 https://github.com/scottyab/AESCrypt-Android

中提供的示例