我真的不知道您的用例是否從安全角度提出建議。但是,要直接解決用戶提供的輸入生成可重複密鑰的問題,可以使用基於密碼的密鑰推導,其中我們將用戶提供的輸入視爲密碼(編輯爲一個完整示例):
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import sun.security.provider.SecureRandom;
public class DerivationExample {
private static SecretKey makeKeyFromUserInput(String userInput, byte[] salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(userInput.toCharArray(), salt, 1024, 256);
byte[] derivedKey = factory.generateSecret(keySpec).getEncoded();
return new SecretKeySpec(derivedKey, "AES");
}
public static void main(String[] args) throws Exception {
String userInput = "foo";
// PBKDF2 standard recommends at least 64-bit salt
// Note: you want to randomly generate this elsewhere and keep it constant
byte[] salt = new byte[8];
new SecureRandom().engineNextBytes(salt);
SecretKey derivedKey = makeKeyFromUserInput(userInput, salt);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, derivedKey, new IvParameterSpec(
new byte[16]));
String plaintext = "Hello, World!";
byte[] cipherText = cipher.doFinal(plaintext.getBytes());
// Derive key again to demonstrate it is the same
SecretKey derivedKey2 = makeKeyFromUserInput(userInput, salt);
cipher.init(Cipher.DECRYPT_MODE, derivedKey2, new IvParameterSpec(
new byte[16]));
byte[] plainText = cipher.doFinal(cipherText);
// Prints "Hello, World!"
System.out.println(new String(plainText));
}
}
如果鹽保持不變,所得到的鍵將是可再現的。在需要產生相同密鑰的任何其他設備之間共享鹽。
注意:您需要安裝不受限制的策略文件(請參閱this page的底部)才能正常工作。
請注意,由「安全」位組成的安全系統在整體上看並不保證安全。我們只看到了一小部分你的要求,因此我們的建議應該採取一撮鹽(沒有雙關語)。要獲得更完整的答案,我們需要了解您嘗試保護的端到端流程。
但是,由於StackOverflow不是設計安全系統的地方,您可能需要在其他地方尋求幫助。
我寧願使用SHA256(SHA-2)而不是MD5。 – Thomas
你能解釋你的問題的背景嗎?然後,我們可以從安全角度爲最佳設計提供建議,然後再介紹如何使用Java實現。 –
@ Duncan:上下文:在用戶端,用戶輸入一個字母數字字符串(前面給出),程序需要生成一個密鑰(由我的程序的用戶解開),然後用於加密一些數據。然後在我的身邊,我需要知道這個密鑰(或生成它)以解密這些數據。 – Arsenic