java - 不安全的密码加密模式,如何解决?

标签 java android firebase-authentication

<分区>

我正在加密登录 firebase 的密码,它运行良好,但我在 google play 控制台收到警告,提示您的应用程序包含不安全的加密模式 怎么能我摆脱它 ??

我正在 android studio 上尝试。

public static class AESCrypt
{
    private static final String ALGORITHM = "AES";
    private static final String KEY = "1Hbfh667adfDEJ78";

    public static String encrypt(String value) throws Exception
    {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte [] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8"));
        String encryptedValue64 = Base64.encodeToString(encryptedByteValue, Base64.DEFAULT);
        return encryptedValue64;

    }

    public static String decrypt(String value) throws Exception
    {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(AESCrypt.ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT);
        byte [] decryptedByteValue = cipher.doFinal(decryptedValue64);
        String decryptedValue = new String(decryptedByteValue,"utf-8");
        return decryptedValue;

    }

    private static Key generateKey() throws Exception
    {
        Key key = new SecretKeySpec(AESCrypt.KEY.getBytes(),AESCrypt.ALGORITHM);
        return key;
    }

最佳答案

主要问题是您使用了不完整的密码和硬编码的加密 key 。如果您使用 Find Security Bugs 分析您的来源你得到 CIPHER_INTEGRITYHARD_CODE_KEY警告:

The cipher does not provide data integrity [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 25] CIPHER_INTEGRITY
The cipher does not provide data integrity [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 15] CIPHER_INTEGRITY
Hard coded cryptographic key found [com.lloyds.keystorage.AESCrypt] At AESCrypt.java:[line 35] HARD_CODE_KEY

解决方案是使用包含基于哈希的消息验证代码 (HMAC) 的密码来签署数据:

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

并将 key 存储在单独的配置文件或 keystore 中。

下面是完全重构后的整个类:

import android.util.Base64
import static java.nio.charset.StandardCharsets.UTF_8;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESCrypt {
  private static final String TRANSFORMATION = "AES/GCM/NoPadding";

  public static String encrypt(String value) throws Exception {
    Key key = generateKey();
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedByteValue = cipher.doFinal(value.getBytes(UTF_8));
    return Base64.encodeToString(encryptedByteValue, Base64.DEFAULT);
  }

  public static String decrypt(String value) throws Exception {
    Key key = generateKey();
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT);
    byte[] decryptedByteValue = cipher.doFinal(decryptedValue64);
    return new String(decryptedByteValue, UTF_8);
  }

  private static Key generateKey() {
    return new SecretKeySpec(Configuration.getKey().getBytes(UTF_8), TRANSFORMATION);
  }
}

关于java - 不安全的密码加密模式,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026804/

相关文章:

java - Alpha channel 模糊

java - 使用 JPA 2 持久性处理数据异常

java - 如何设置 Spring 应用程序的日志文件名并记录到 tomcat/logs 文件夹?

ios - 使用 Carthage 集成 Firebase GoogleSignIn

javascript - firebase.auth.signInWithCustomToken 不是函数node.js

ios - 如何通过 Firebase Auth 获取有关 Apple Sign In 用户真实性的数据?

java - 执行操作后如何更改 fxml 文件?

javascript - touchend 事件在 Android 上不起作用

java - ".android"文件夹和 "debug.keystore"文件丢失

android - 删除 TableLayout 中 TableRow 之间的任意间隙