Android - 使用Fingerprint scanner和Cipher加密解密多个字符串

标签 android

在用户使用指纹扫描仪进行身份验证后,我需要结束加密不同的字符串和相关的解密。

按照这个项目 ( https://github.com/StylingAndroid/UserIdentity/tree/Part1 ) 并更改了“tryEncrypt”方法,如下所示:

  private boolean tryEncrypt(Cipher cipher) {
    try {
        cipher.doFinal(SECRET_BYTES);
        String one = "augusto";
        String two = "test@gmail.com";
        String three = "3333333331";
        byte[] oneEnc = cipher.doFinal(one.getBytes());
        byte[] twoEnc = cipher.doFinal(one.getBytes());
        byte[] threeEnc = cipher.doFinal(one.getBytes());
        Log.d("test", "oneEnc: " + Base64.encodeToString(oneEnc,0));
        Log.d("test", "twoEnc: " + Base64.encodeToString(twoEnc,0));
        Log.d("test", "threeEnc: " + Base64.encodeToString(threeEnc,0));

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

我收到这个错误:

java.lang.IllegalStateException: IV has already been used. Reusing IV in encryption mode violates security best practices.

正确的做法是什么?

谢谢

*********************更新:************************** ***

为了帮助其他人解决这个问题,我使用了这个库并像魅力一样工作:

https://github.com/Mauin/RxFingerprint

最佳答案

您遇到问题是因为您正在使用 Cipher 的单个实例进行多次加密 (dofinal)。您正在使用单个向量初始化 (IV)。

查看有关如何初始化密码的选项。

SecureRandom r = new SecureRandom();
byte[] ivBytes = new byte[16];
r.nextBytes(ivBytes);

cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(ivBytes));

如您所见,您需要指定初始化向量。初始化向量不能重复,保证加密有效。

在您的场景中,您可能需要执行新的初始化。

*Ps: 也可以在没有 IvParameterSpec 的情况下使用 Cipher 初始化。在这种情况下,该类将为您生成一个。但是,我相信您需要对每个 DoFinal 执行初始化以保证一定的随机性。

关于Android - 使用Fingerprint scanner和Cipher加密解密多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37522370/

相关文章:

java - Android NDK 捕获关键事件

android - AlertDialog 未显示

Android AudioTrack 和 AudioRecord 奇怪的行为

android - 如何使用 NFC 将 android 应用程序数据存储在 sim 卡上?

java - 更改在colors.xml中设置的styles.xml中的颜色

JAVASCRIPT:动态加载manifest.json

android - Phonegap/Converting 移动网站

android - 在 flutter 中热重载后更新更改?

Android位图imageview内存泄漏

java - 如何将android volley gson请求解析为对象列表/集合