MD5 SHA512管理c#转换为Java代码

时间:2014-08-04 15:10:39

标签: java c# android md5 sha512

我需要帮助才能将此代码转换为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方法:

Code 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==

1 个答案:

答案 0 :(得分:1)

我尝试重现您的代码。

对于密码 test ,它会生成以下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;
    }
}