-1
我使用下面的代碼,以生成用於提供密碼的哈希:PBEKeySpec不產生期望的輸出的密鑰長度
public static SecretKey generateKey(String passphraseOrPin) throws NoSuchAlgorithmException, InvalidKeySpecException {
final int iterations = 10000;
// Generate a 256-bit key
final int outputKeyLength = 256;
char[] chars = new char[passphraseOrPin.length()];
passphraseOrPin.getChars(0, passphraseOrPin.length(), chars, 0);
byte[] salt = "thisSaltIsInClient".getBytes();
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(chars, salt, iterations, outputKeyLength);
return secretKeyFactory.generateSecret(keySpec);
}
因此,當我得到從所生成的密碼字節,並將其轉換爲一字符串:
String passwordEncrypted = Base64.encodeToString(bytesPass, Base64.DEFAULT);
的輸出是這樣的:
yo2NDJ96VYadiP2jpzL3p + LW0b4qkdWS ++ WqAm0W9d8 =
不應該更長嗎?
我想我沒有睡不夠好今天。我的大腦決定忽略這些字節!=位 – Reinherd