PBKDF2 HMACSHA512 C# と Java の違い
温馨提示:
本文最后更新于 2024年04月12日,已超过 47 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
私のパートナーが使っていますhttps://github.com/realindiahotel/PWDTK.NETユーザーのパスワードを暗号化します。これらのユーザーを別のアプリケーションにインポートする予定なので、同じエンコーダーを使用する必要があります。私のアプリケーションは Spring Boot です。彼と同じハッシュを取得することに問題があります。
String salt = "F2ZEAo/UMjzIJwNvCC6cRwObd4CX462BTv4Tn/ntaTad6P8Xej1VNqlwNdueYo/XklYPH2RDfLUjsa/dkqnQzg==";
String hash = "hwuR4Ol/tVi9DxRsjAstLt+wfJsTMr+ZzOBh3X90pAmhZU2gyeOngE7OMbQuVB/VYjj+47IqtIYffJ9CHfV7TQ==";
このパスワードはです"123456"
。
関連コード (C#):
public static HashedPassword GetNewPassword(string plainPassword)
{
byte[] randomSalt = PWDTK.GetRandomSalt();
byte[] hash = PWDTK.PasswordToHash(randomSalt, plainPassword);
return new HashedPassword { PasswordHash = Convert.ToBase64String(hash), Salt = Convert.ToBase64String(randomSalt) };
}
私のコード (Java):
public static boolean matches(String rawPassword, String storedHash, String storedSalt) throws NoSuchProviderException, UnsupportedEncodingException {
byte[] decodedHash = Base64.getDecoder().decode(storedHash.getBytes());
System.out.println("hash " + Hex.encodeHexString(decodedHash));
byte[] decodedSalt = Base64.getDecoder().decode(storedSalt.getBytes());
System.out.println("salt " + Hex.encodeHexString(decodedSalt));
try {
PBEKeySpec spec = new PBEKeySpec(rawPassword.toCharArray(), decodedSalt, 5000, 512);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
SecretKey generatedKey = factory.generateSecret(spec);
byte[] generatedHash = generatedKey.getEncoded();
System.out.println(Base64.getEncoder().encodeToString(generatedHash));
return MessageDigest.isEqual(decodedHash, generatedHash);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
// Handle exceptions
e.printStackTrace();
return false;
}
}
いくつかのオンライン PBKDF2 ツールを試しましたが、それらはすべて Java と同じハッシュを返しました。https://onlinephp.io/c/3304b
ソルトの場合は「F2ZEAo/UMjzIJwNvCC6cRwObd4CX462BTv4Tn/ntaTad6P8Xej1VNqlwNdueYo/XklYPH2RDfLUjsa/dkqnQzg==」、パスワード「123456」、5000回の再生では「5281e548c33c0fece48967」 b6af8dcfc66d7266fbede24be918192d3feb1437827e7c1c0b93c289abb805fbbfb76d66b512082f74b76061f5bd21d9d41d0ad」という結果が得られます。
違いは何ですか?
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2623
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(192) 评论(0)