【问题标题】:Android AES 256-bit Encrypt dataAndroid AES 256 位加密数据
【发布时间】:2016-11-01 08:21:29
【问题描述】:

所以我看到了很多例子,做了很多谷歌搜索,并查看了 Stack Overflow 上的例子......我需要帮助。我有一个 Android 应用程序,我将用户名和密码存储在设备上,我需要对它们进行 AES 256 加密。通过查看示例,这是我目前所拥有的:

public class Security {
    Cipher ecipher;
    Cipher dcipher;

    // 8-byte Salt
    byte[] salt = {
        (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
        (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
    };

    // Iteration count
    int iterationCount = 19;

    public Security (String passPhrase) {
        try {
            // Create the key
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance(
                "PBEWithSHAAndAES").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // Prepare the parameter to the ciphers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            // Create the ciphers
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (Exception e) { 
            e.printStackTrace(); 
        }
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return Base64.encodeToString(enc, Base64.DEFAULT);
        } catch (Exception e) { 
            e.printStackTrace();
            return null;
        }
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = Base64.decode(str, Base64.DEFAULT);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

我正在尝试使其基于密码,因此用户将在第一次使用与服务器通信所需的用户名和密码创建一个帐户,并创建一个将用作这些凭据的密钥的 PIN存储在数据库中。

我主要关心的是这看起来安全吗?我知道固定盐不好,我该如何解决?

我知道有十亿个关于这个的问题,但我希望有人站出来说“这很安全”或“这不安全,改变这个”

谢谢!


编辑:

所以这是我到目前为止的代码,它似乎正在工作......

public class Security {

    Cipher ecipher;
    Cipher dcipher;
    byte[] salt = new byte[8];
    int iterationCount = 200;

    public Security(String passPhrase) {
        try {
            // generate a random salt
            SecureRandom random = new SecureRandom();
            random.nextBytes(salt);

            // Create the key
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance(
                "PBEWithSHA256And256BitAES-CBC-BC").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // Prepare the parameter to the ciphers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            // Create the ciphers
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return Base64.encodeToString(enc, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = Base64.decode(str, Base64.DEFAULT);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int getIterationCount() {
        return iterationCount;
    }

    public String getSalt() {
        return Base64.encodeToString(salt, Base64.DEFAULT);
    }
}

我用这段代码来测试它:

 Security s = new Security(pinBox.getText().toString());
            String encrypted = s.encrypt(passwordBox.getText().toString());
            String decrypted = s.decrypt(encrypted);
            builder.setMessage("pin: " + pinBox.getText().toString() + "\n" +
                    "password: " + passwordBox.getText().toString() + "\n" +
                    "encrypted: " + encrypted + "\n" +
                    "decrypted: " + decrypted + "\n" +
                    "salt: " + s.getSalt());

所以我不需要担心初始化向量?或者专门硬编码一个密码算法?

再次感谢!

【问题讨论】:

标签: android encryption aes


【解决方案1】:

编辑:虽然下面的代码是正确的,但您所做的基本上是相同的,IV 是从密码中派生的,因此您不必单独存储它。

您的代码是否按预期工作?对于实际的加密/解密,您可能希望使用 AES,很可能在 CBC 模式下。然后你需要一个静脉注射,所以它变成这样:

ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[IV_LENGTH];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
byte[] enc = ecipher.doFinal(utf8);

它是否安全取决于您使用它的目的。 salt 的目的是让暴力破解密码更加困难:如果它是随机的,则攻击者不能使用预先生成的密码表(密码-> 密钥)。如果您不太担心这种攻击,则可以将其固定。如果您决定使其随机化,只需将其与加密数据一起存储即可。与 IV 相同。

【讨论】:

  • 我假设我会先加密 salt 和 iv,然后再使用加密的用户名和密码存储它们?如果是这样,我将如何解密?我没有盐和 iv 来设置解密器,所以也许我误解了?
  • 盐本身不是秘密,不需要加密。 IV 也不是。
  • 好的。那讲得通。谢谢你帮助我理解。我将编辑我的代码,然后将其发回。我想确保我得到最好的解决方案。你能解释一下迭代次数吗?如果是随机的有关系吗?我想我已经读过它,如果它更高,代码需要更长的时间来执行,但我假设更高更好?我猜这是在返回最终值之前运行算法迭代计数次的影响?
  • 迭代次数顾名思义:执行相同操作的次数。这个想法是,通过使单个推导变慢,蛮力攻击(成千上万的推导)变得不切实际。推荐值通常约为(几)千,但在移动设备上可能会很慢。尝试并找到一个适合您的数字,而不会太慢。阅读 RFC 了解一些细节和理由,它很短:tools.ietf.org/html/rfc2898
  • 该链接非常有用。我将进行更改并对其进行测试,然后发回我遇到的任何内容。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多