.NET与JAVA 同步的MD5加密方式

.NET中MD5加密

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OAOnline.BLL;
using System.Diagnostics;
using System.Text;
using System.Security.Cryptography;
using System.IO;
//1、加密成32位16进制字符串
    protected string HashMD5_String(string str)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
        }

//2、同上,增加偏移量

        public string GetMD5(string sDataIn, string move)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byt, bytHash;
            byt = System.Text.Encoding.UTF8.GetBytes(move + sDataIn);
            bytHash = md5.ComputeHash(byt);     
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
               sTemp += bytHash[i].ToString("x").PadLeft(2, '0');        
            }
            return sTemp;
        }

//3、将字符串编码成ASCII数组后进行MD5加密,然后再编码成ASCII字符串
        public static string MD5Encrypt(string strEnc)
        {
            string str = "加密失败!";
            try
            {

                System.Security.Cryptography.MD5CryptoServiceProvider MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();                
                str = ASCIIEncoding.ASCII.GetString(MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strEnc)));
                return str;
            }
            catch (System.Exception ex)
            {
                throw (ex);
            }     
        }

 

 

JAVA中的md5加密

  import java.security.MessageDigest;

public class MD5Ecnrypt {        
    //将字符串加密成MD5,32位16进制字串,如"3031209"转成"e043a49740adde7aae4f34818c52528e"
    public static String EncodeMD5Hex(String text) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes("US-ASCII"));
        byte[] digest = md.digest();    
        StringBuffer md5 = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));
            md5.append(Character.forDigit((digest[i] & 0xF), 16));
        }
        return md5.toString();
    }
    //将字符串加密成ASCII字串,如"3031209"转成"?C??@??z?O4??RR?"
    public static String EncodeMD5ASCII(String text) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(text.getBytes("US-ASCII"));
        byte[] digest = md.digest();        
        return new String(digest,"US-ASCII");        
    }
    //将ASCII字串编码成16进制字串,如:“?C??@??z?O4??RR?“转成”3f433f3f403f3f7a3f4f343f3f52523f“
    //参数text
    //返回结果16进制字符串
    public static String DecodeMD5Hex(String text) throws Exception {        
        byte[] digest = text.getBytes();        
        StringBuffer md5 = new StringBuffer();
        for (int i = 0; i < digest.length; i++) {
            md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));
            md5.append(Character.forDigit((digest[i] & 0xF), 16));
        }
        return md5.toString();    
    }
    //比较输入密码MD5加密后与数据库密码相等
    //参数a:输入的密码
    //参数b:数据库的ASCII字串
    //返回结果:a进行md5加密后和b相等则为真,反之则为假
    public static Boolean CheckPSW(String a,String b) throws Exception
    {        
        String strone=EncodeMD5ASCII(a);            
        return strone.equals(b);
    }
}

 

相关资源:asp.net知识库
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页