久久久久久久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)
主站蜘蛛池模板: 国产成人精品午夜视频免费 | 国产一区二区免费 | 一区二区三区免费 | 久久亚洲精品国产精品紫薇 | 中文字幕一区二区三区四区五区 | cao在线| 欧美一区二区三区 | 免费黄视频网站 | 欧美精品在线看 | 久久久国产一区二区三区 | 中文字幕视频一区 | 成人夜晚看av | 亚洲视频一区在线观看 | 日韩欧美亚洲综合 | 国产精品乱码一区二区三区 | 免费中文字幕 | 中文字幕中文字幕 | 国产精品亚洲一区 | 亚洲导航深夜福利涩涩屋 | 一级毛片网| 日韩欧美国产精品一区二区三区 | 欧美成人一区二免费视频软件 | 亚洲男人的天堂网站 | 超碰超碰 | 黄色免费av | 中文字幕亚洲视频 | 久久天天躁狠狠躁夜夜躁2014 | av手机在线免费观看 | 欧美一级免费看 | 久久久久国产一区二区三区四区 | 亚洲第一天堂无码专区 | 久久久久久久久久久久久9999 | 操操日| 久久久久久久夜 | av黄色片在线观看 | 不卡视频一区二区三区 | 夜夜骑首页 | 超碰最新在线 | 精品久久久久久久久久久久久久久久久 | 日韩欧美国产一区二区三区 | 操操日 |