久久久久久久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)
主站蜘蛛池模板: 亚洲电影第三页 | 一级aaaaaa毛片免费同男同女 | 99精品视频一区二区三区 | 成人免费视频久久 | 日韩精品一区二区三区视频播放 | 成人在线免费视频 | 精品亚洲一区二区三区 | 亚洲一区在线播放 | 亚洲自拍偷拍av | 成人做爰69片免费观看 | 欧美日韩成人在线 | 国产亚洲高清视频 | 欧美日韩精品专区 | 亚洲欧美一区二区三区1000 | 欧美一区中文字幕 | 精品视频一区二区三区在线观看 | 中文字幕 在线观看 | 国产欧美在线一区二区 | 国产成人免费在线 | 欧美一区2区三区3区公司 | 91久久久久久 | 在线黄色影院 | 色永久 | 国产一级在线视频 | 中文在线а√在线8 | 黄色一级毛片 | 91久久精品日日躁夜夜躁欧美 | 久久99精品久久久久久国产越南 | 亚洲欧美精品国产一级在线 | 久久er99热精品一区二区 | 久久欧美高清二区三区 | 91欧美精品成人综合在线观看 | 精品国产乱码久久久久久88av | 国产亚洲欧美在线 | 特黄毛片视频 | 久久久久无码国产精品一区 | 欧美色成人 | 区一区二在线观看 | 欧美日韩在线一区二区 | 亚洲精品免费看 | 草草草影院|