本文介紹了使用 JCE/JCA 從主密鑰派生秘密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
有人能指出我正確的方向嗎?
Can some point me in the right direction?
我想使用 JCE/JCA 從主密鑰派生新密鑰,我該如何實現?
I'd like to use JCE/JCA to derive a new key from a master secret key, How can I achieve this?
問候.
推薦答案
JCA 提供標準的基于密碼的密鑰派生函數,如 PKCS#5 v2.0 和 RFC 2898.該算法從主密鑰(密碼)中創建一些隨機材料,以生成適合給定密碼的密鑰.
The JCA provides standard password-based key derivation functions like PBKDF2 defined in PKCS#5 v2.0 and RFC 2898. This algorithm creates some random material from a master secret (a password) in order to generate a key suitable for a given cipher.
public byte[] deriveKey(String password, byte[] salt, int keyLen) {
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec specs = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLen);
SecretKey key = kf.generateSecret(specs);
return key.getEncoded();
}
public byte[] encrypt(String password, byte[] plaintext) {
byte[] salt = new byte[64];
Random rnd = new Random();
rnd.nextByte(salt);
byte[] data = deriveKey(password, salt, 192);
SecretKey desKey = SecretKeyFactory.getInstance("DESede").generateSecret(new DESedeKeySpec(data));
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, desKey);
return cipher.doFinal(plaintext);
}
這篇關于使用 JCE/JCA 從主密鑰派生秘密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!