久久久久久久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)
主站蜘蛛池模板: 伊人久久艹 | 日韩欧美在线播放 | 美女久久久 | 日韩免费在线观看 | 国产精品成人免费视频 | 亚洲精品欧美 | 欧美日韩国 | 欧美国产日韩在线 | 国产一级一片免费播放放a 男男成人高潮片免费网站 精品视频在线观看 | 亚洲精品乱码久久久久久蜜桃91 | 欧美三级韩国三级日本三斤在线观看 | 久婷婷| 天天艹夜夜艹 | 久草福利在线观看 | 亚洲精品视频在线播放 | 黄a视频 | 色片在线观看 | 久久久久国产一区二区三区 | 96看片| 高潮毛片无遮挡免费看 | 亚洲三级在线 | 97人人干| 欧美日韩亚洲综合 | 欧洲精品| 中文字幕在线免费观看 | 一级片久久 | 免费一区二区三区 | 欧洲一区二区 | 伊人春色在线 | 日本中文字幕网站 | www.国产在线观看 | 免费的黄色小视频 | 国产中文字幕在线观看 | 色香蕉网 | 国产激情网站 | 久久小视频 | 四色永久访问 | 亚洲一区中文 | 欧美成人精品一区二区三区在线看 | 欧美日韩国产在线观看 | 色婷婷国产 |