在用户使用指纹扫描程序进行身份验证后,我需要结束加密不同的字符串和相关的解密。
关注此项目(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.
如何做到这一点的正确方法是什么?
由于
*******************更新:********** ***
为了帮助其他人解决这个问题,我使用了这个库并像魅力一样工作:
答案 0 :(得分:4)
您遇到问题,因为您正在使用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的情况下使用密码初始化。在这种情况下,该类将为您生成一个。但是,我认为您需要为每个DoFinal执行初始化以保证一些随机性。
答案 1 :(得分:0)
为了帮助其他人解决这个问题,我使用了这个像魅力一样工作的库: