[英]Android - Use Fingerprint scanner and Cipher to encrypt and decrypt multiple strings
在用戶使用指紋掃描儀進行身份驗證后,我需要結束對不同字符串和相關解密的加密。
按照這個項目( 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.
如何做到這一點的正確方法是什么?
謝謝
*******************更新:*****************************
為了幫助其他人解決這個問題,我使用了這個庫並像魅力一樣工作:
您遇到了問題,因為您使用 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 執行初始化以保證一些隨機性。
為了幫助其他人解決這個問題,我使用了這個像魅力一樣工作的庫:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.