簡體   English   中英

MD5 SHA512托管的C#到Java代碼的轉換

[英]MD5 SHA512Managed c# conversion to Java code

我需要幫助將此代碼轉換為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==

我試圖重現您的代碼。

對於密碼測試,它將產生以下BASE64輸出

Q0Y2QkI0MTBFRUJFOTAyNkU1OUZGMUNGMzU0NkYzMkI3NDZFMzE5RjQzNTc0MDM5QjU2MUI2NEQxOTQzNzRGMDRENDM0QzMyQjg3MjMwQkM1N0I0ODFDRDlEODlBNjMxQjMyNjRGQjNBQjAwMEYwNjk5Rjc0NUNEQjgzMzY1RkM=

我使用以下代碼:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM