Java 正在寻找Android的加密/解密AES示例

Java 正在寻找Android的加密/解密AES示例,java,android,encryption,aes,Java,Android,Encryption,Aes,由于我是加密新手,尤其是在Java/Android中,我正在努力寻找工作良好的教程和代码,以便我可以从中学习,但不会影响结果 如本网站所示: 我找不到给我带来问题的BASE64Encoder类,它似乎在包a sun.utils中,但我可以找到Base64类,但我无法调整代码使其适合我 在这方面也是如此 加密是在位图图像中完成的我无法在普通文本字符串中实现相同的技术 有人会在Android中提供一个简单的AES加密/解密示例,演示如何使用密钥、消息、加密和解密吗 我找不到BASE64Encode

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

如本网站所示:

我找不到给我带来问题的
BASE64Encoder
类,它似乎在包a sun.utils中,但我可以找到
Base64
类,但我无法调整代码使其适合我

在这方面也是如此

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

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

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

您可能没有为Base64使用正确的库。您提到的
sun.utils
,其中您发送的链接正在使用:

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

由于Java8,您可以使用
Java.util.Base64
,如Oracle文档所述。它支持基本编码、URL编码和MIME编码。您可以在中找到一些示例。

加密和解密过程在以下情况下运行良好: 我替换了从以下位置获得的字节数组:

位图
按照中的说明,通过消息字符串的
字节数组


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

我在我的项目中使用了这个

'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或SH256。 希望这有帮助

编辑: 在我的情况下,我必须加密登录密码并通过网络将其发送到服务器

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

因此,在后端,我用密钥解密了安全密码,并且在两侧使用相同的AES(Android和后端)。

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

byte[] b = baos.toByteArray();

请注意,您必须以某种方式存储密钥,因为在某些情况下必须进行解密

存储在设备上不是一个好主意,因为你把钥匙留在了门上。您可以在服务器上存储或使用密码短语(由用户修复或询问)。我给你最后一个选择的真实样本

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;
}
私有静态字符串getPassphraseSize16(字符串键){
if(TextUtils.isEmpty(键)){
返回null;
}
char controlChar='\u0014';
字符串key16=key+controlChar;
if(键16.length()<16){
while(键16.length()<16){
键16+=键+控制字符;
}
}
如果(键16.length()>16){
key16=key16.substring(key16.length()-16,key16.length());
}
返回键16;
}
公共静态字节[]编码(字节[]消息,字符串密码){
字符串passphrase16=getPassphraseSize16(passphrase);
SecretKeySpec secretKey=newsecretkeyspec(passphrase16.getBytes(),“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.ENCRYPT_模式,secretKey);
字节[]encodedText=cipher.doFinal(消息);
返回编码文本;
}
公共静态字节[]解码(字节[]编码消息,字符串键){
字符串passphrase16=getPassphraseSize16(键);
SecretKeySpec secretKey=newsecretkeyspec(passphrase16.getBytes(),“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.DECRYPT_模式,secretKey);
字节[]decodedText=cipher.doFinal(encodedMessage);
返回解码文本;
}

此示例与

中提供的类似。此示例用于登录还是其他?此示例用于在资源中脱机隐藏一些数据,这些数据受严格版权保护且需求量大。此问题与主题无关,因为它要求提供一个示例。但是,它似乎也是重复的。我尝试过它,但当我尝试解密使用AES encryption online 1加密的数据时,它显示填充错误异常。我使用2加密了字符串。我使用您提到的库加密了相同的字符串。3.加密数据是不同的。4.android studio显示javax.crypto.BadPaddingException:error:1e06b065:Cipher functions:EVP_DECRYPT final_ex:BAD_DECRYPT DECRYPT DECRYPT DECRYPT DECRYPT DECRYPT exception when decrypting让我用我在案例中尝试过的方法编辑我的答案。这是一个不推荐的存储库很高兴你提到它,你能在这里更新最新版本吗?
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;
}