gpt4 book ai didi

java - MD5 SHA512托管 C# 到 Java 代码的转换

转载 作者:行者123 更新时间:2023-12-01 12:40:44 25 4
gpt4 key购买 nike

我需要帮助将此代码转换为 Java 以进行密码比较,并且它必须在 Android 上运行。我对如何添加此 C# 代码中给出的盐特别困惑:

C# 代码

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CMS.Core.Utility
{
public sealed class CMSHashManager
{
private static readonly string _salt = "3D5900AE-111A-45BE-96B3-D9E4606CA793";
private static readonly int _hashIterationsMax = 10;
private CMSHashManager()
{
}

#region Public Methods
//Gets the salted hash value with predetermined iterations.
public static string GetPasswordHash(string plaintextPassword)
{
string hashData = plaintextPassword;
for (int hashLimit = 0; hashLimit < _hashIterationsMax; hashLimit++)
hashData = GetHash(_salt + hashData);
return hashData;
}

//Verifies the hash
public static bool VerifyHashedPassword(string plaintextPassword, string encryptedPassword)
{
string hashData = GetPasswordHash(plaintextPassword);
return encryptedPassword.Equals(hashData);
}

#endregion Public Methods

#region Private Methods
//Gets the hash value of the data using SHA512Managed
private static string GetHash(string unhashedData)
{
byte[] hashData = Encoding.UTF8.GetBytes(unhashedData);
// on server 2003 or higher, can use SHA512CryptoServiceProvider
//SHA512CryptoServiceProvider sha512CryptoServiceProvider = new SHA512CryptoServiceProvider();

SHA512Managed sha512CryptoServiceProvider = new SHA512Managed();
hashData = sha512CryptoServiceProvider.ComputeHash(hashData);
sha512CryptoServiceProvider.Clear();
return Convert.ToBase64String(hashData);
}
#endregion Private Methods

}

}

我已经编写了这个创建 MD5 哈希值的 java 方法:

Java 代码

public String getMD5Password(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-512");
digest.update(password.getBytes("UTF-16LE"));
byte messageDigest[] = digest.digest();

// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
}

测试

出于测试目的,您可以使用以下案例:

明文:12345
加密后:NgkuakH7UsCQwGHMQOhVXI3nW6M+1AtREY4Qx35osQo87p/whZIzy8cZU7+R7XnmyzgMzLWSvX+rTiW‌​‌​zfGTPsA==

最佳答案

我尝试重现您的代码。

对于密码test,它会生成以下 BASE64 输出

Q0Y2QkI0MTBFRUJFOTAyNkU1OUZGMUNGMzU0NkYzMkI3NDZFMzE5RjQzNTc0MDM5QjU2MUI2NEQxOTQzNzRGMDRENDM0QzMyQjg3MjMwQkM1N0I0ODFDRDlEODlBNjMxQjMyNjRGQjNBQjAwME YwNjk5Rjc0NUNEQjgzMzY1RkM=

我使用了以下代码:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//import javax.xml.bind.DatatypeConverter;
import android.util.Base64;


public class Support {

private static final String SALT = "3D5900AE-111A-45BE-96B3-D9E4606CA793";
private static final int MAX_HASH_ITERATIONS = 10;

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
String result = Support.GetPasswordHash("test");
System.out.println(result);
}

public static String GetPasswordHash(String plaintextPassword) throws NoSuchAlgorithmException, UnsupportedEncodingException {
String hashData = plaintextPassword;
for (int hashLimit = 0; hashLimit < MAX_HASH_ITERATIONS; hashLimit++) {
hashData = GetHash(SALT + hashData);
}
return hashData;
}

//Gets the hash value of the data using SHA512Managed
private static String GetHash(String unhashedData) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return getMD5Password(unhashedData);
}

//Verifies the hash
public static boolean VerifyHashedPassword(String plaintextPassword, String encryptedPassword) throws NoSuchAlgorithmException, UnsupportedEncodingException {
String hashData = GetPasswordHash(plaintextPassword);
return encryptedPassword.equals(hashData);
}


public static String getMD5Password(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-512");
digest.update(password.getBytes("UTF-16LE"));
byte messageDigest[] = digest.digest();

StringBuilder sb = new StringBuilder();
for(int iPos = 0; iPos < messageDigest.length; iPos++) {
String h = Integer.toHexString(0xFF & messageDigest[iPos]);
while (h.length() < 2) {
h = "0" + h;
}
sb.append(h);
}

String md5String = sb.toString().toUpperCase();
String res = Base64.encodeToString(md5String.getBytes(), Base64.DEFAULT);

return res;
}
}

关于java - MD5 SHA512托管 C# 到 Java 代码的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com