久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java 中為 AES 生成隨機 IV

Generating random IV for AES in Java(在 Java 中為 AES 生成隨機 IV)
本文介紹了在 Java 中為 AES 生成隨機 IV的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在為 android 中的 PBE 實現和 AES 加密引擎,我找到了兩種方法來實現 IV 的創建,我想知道哪種方法更好更安全地獲取 IvParameterSpec:

I'm implementing and AES encryption engine for PBE in android, and I've found two ways to implement the creation of the IV and I would like to know which one is better and more secure for getting IvParameterSpec:

方法#1:

SecureRandom randomSecureRandom = SecureRandom.getInstance("SHA1PRNG");
byte[] iv = new byte[cipher.getBlockSize()];
randomSecureRandom.nextBytes(iv);

IvParameterSpec ivParams = new IvParameterSpec(iv);

方法#2:

AlgorithmParameters params = cipher.getParameters();
byte[] iv2 = params.getParameterSpec(IvParameterSpec.class).getIV();

ivParams = new IvParameterSpec(iv2);

推薦答案

我會使用方法#1,因為 Java API 為 Cipher.init() 只接受加密/解密模式和密鑰的API:

I'd use method #1, because the Java API specifies the following for the Cipher.init() API that just takes the encryption/decryption mode and key:

如果此密碼實例需要指定密鑰無法提供的任何算法參數或隨機值,則此密碼的底層實現應該生成所需的參數(使用其提供者 隨機值).

If this cipher instance needs any algorithm parameters or random values that the specified key can not provide, the underlying implementation of this cipher is supposed to generate the required parameters (using its provider or random values).

(強調我的).

所以不清楚選擇方法2時不同的提供者會做什么.查看 Android 源代碼,似乎至少某些版本(包括版本 21?)將不會創建隨機 IV - 隨機 IV 創建似乎已被注釋掉.

So it is not clear what different providers will do when method 2 is chosen. Looking at the Android source code, it seems that at least some versions (including version 21?) will not create a random IV - the random IV creation seems commented out.

方法 1 也更透明,而且 - 在我看來 - 對眼睛更容易.

Method 1 is also more transparent and it is - in my opinion - easier on the eyes.

請注意,通常最好使用 new SecureRandom() 并讓系統找出最好的 RNG.SHA1PRNG" 定義不明確,可能因實現而異,并且已知存在實現弱點,尤其是在 Android 上.

Note that it is generally better to use new SecureRandom() and let the system figure out which RNG is best. "SHA1PRNG" is not well defined, may differ across implementations and is known to have had implementation weaknesses, especially on Android.

所以最終結果應該是這樣的:

So the end result should be something like:

SecureRandom randomSecureRandom = new SecureRandom();
byte[] iv = new byte[cipher.getBlockSize()];
randomSecureRandom.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);

<小時>

請注意,GCM 模式最適合 12 字節 IV 而不是 16 字節 IV - AES 的塊大小.


Beware that GCM mode works best with a 12 byte IV instead of the 16 byte IV - the block size of AES.

這篇關于在 Java 中為 AES 生成隨機 IV的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 国产资源一区二区三区 | 蜜桃视频一区二区三区 | 国产女人与拘做视频免费 | 一区二区福利视频 | 国产资源网 | 美女拍拍拍网站 | 成人在线一区二区三区 | 亚洲一区二区三区高清 | www.久久久 | 日韩2020狼一二三 | 国产免费观看一区 | 99精品免费久久久久久久久日本 | 视频二区| 99热这里 | 美女日批免费视频 | 欧洲一区二区在线 | 国产精品国产精品 | 日韩精品久久一区二区三区 | 欧美成人精品激情在线观看 | 夜夜骚视频| 国产精品久久久久久久久久久久午夜片 | 美女视频一区 | 亚洲第1页| 黄网站涩免费蜜桃网站 | 午夜激情影院 | 西西裸体做爰视频 | 久久久av中文字幕 | 欧美一级免费看 | 欧美精品欧美精品系列 | 亚洲视频在线观看免费 | 成人黄视频在线观看 | 美女天天操 | 又黄又色 | 欧美成人性生活 | 欧美性久久 | 日韩久久在线 | 日本精品一区 | 中文字幕亚洲精品 | 欧美激情欧美激情在线五月 | 国产视频一区二区 | 精品一二 |