简体   繁体   English

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

[英]MD5 SHA512Managed c# conversion to Java code

I need help to convert this code to Java for password comparison and it must run on Android. 我需要帮助将此代码转换为Java以进行密码比较,并且它必须在Android上运行。 I am specially confused in how to add the salt given in this C# Code here: 我对如何添加此C#代码中给出的盐感到特别困惑:

Code 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

    }

}

I have already written this java method which creates a MD5 hash: 我已经写了这个创建MD5哈希的java方法:

Code 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();
}

Test 测试

For testing purposes you can use the following case: 为了进行测试,可以使用以下情况:

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

I tried to reproduce your code. 我试图重现您的代码。

For the password test it produces the following BASE64 output 对于密码测试,它将产生以下BASE64输出

Q0Y2QkI0MTBFRUJFOTAyNkU1OUZGMUNGMzU0NkYzMkI3NDZFMzE5RjQzNTc0MDM5QjU2MUI2NEQxOTQzNzRGMDRENDM0QzMyQjg3MjMwQkM1N0I0ODFDRDlEODlBNjMxQjMyNjRGQjNBQjAwMEYwNjk5Rjc0NUNEQjgzMzY1RkM=

I used the following code: 我使用以下代码:

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