任何人都可以告诉我在下面的代码中128 * 8的用法是什么? 我已经写了密码加密的代码,我仍然不知道这个128 * 8在做什么。PBKDF2WithHmacSHA1混淆
这是我得到尽可能从这个代码的返回:
67a0759ac6266ca2156555426aae10b18c34b436ea036247e6c0e16cd8d4199b9df508c32cd14e50a533ac00c071888cb8167982d9bf22a89ccd1c02a9d9c76d4e5fb5c3be91711a444a3b453c54790d5b540d7f3d0ef5798cf6a08e5acaf1b0fb445e174befd2e5b97978534aa7c22c4e404503e40f06f6832fe4a5843c9b01
的toHex()函数如下所示:我想返回值是字符。
private static String toHex(byte[] array) throws NoSuchAlgorithmException
{
BigInteger bi = new BigInteger(1, array);
String hex = bi.toString(16);
int paddingLength = (array.length * 2) - hex.length();
if(paddingLength > 0)
{
return String.format("%0" +paddingLength + "d", 0) + hex;
}else{
return hex;
}
}
public static String encrypt(String password,String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
int iterations = 4096;
char[] chars = password.toCharArray();
byte[] salt = key.getBytes();
PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 128 * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = skf.generateSecret(spec).getEncoded();
return toHex(hash);
}
所以返回的字符串的长度是64个字符? –
(我也会在这里选择你的名字,这不是加密,这是密钥生成,我也强烈建议你不要使用无参数的'String.getBytes'方法。) –
Ya它是一个秘密密钥生成。你能告诉我什么是128 * 8,为什么我得到256位的返回值? – user3387358