c#兼容 PHP中的md5

原文:c#兼容 PHP中的md5

由于工作需要,需要使用C#去对一个php程序做二次开发.在登录验证的时候,发现一个小问题.

就是用C#写的md5算法得出的结果和php的md5()得出的结果有时候会不一样. 导致有些账号的密码验证不能通过.后来网上找了一下,在国外一个网站上找到了答案.

C#常用的MD5算法.

public static string MD5(string password) {
   byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
   try {
      System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
      cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] hash = cryptHandler.ComputeHash (textBytes);
      string ret = "";
      foreach (byte a in hash) {
            ret += a.ToString ("x");
      }
      return ret ;
   }
   catch {
      throw;
   } 

}

但是此算法与md5()得出的结果是不一样的.后调整为如下,即可以了.

public static string MD5(string password) {
   byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
   try {
      System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
      cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] hash = cryptHandler.ComputeHash (textBytes);
      string ret = "";
      foreach (byte a in hash) {
         if (a<16)
            ret += "0" + a.ToString ("x");
         else
            ret += a.ToString ("x");
      }
      return ret ;
   }
   catch {
      throw;
   } 

}

时间: 2024-08-04 05:22:44

c#兼容 PHP中的md5的相关文章

asp中的md5/sha1/sha256算法收集

对于asp这种古董级的技术,这年头想找一些有用的资料已经不容易了,下面是一些常用的加密算法: md5 (将以下代码另存为md5.inc) + ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

MD5的好处及linux中文件MD5校验和的应用

MD5算法简介: MD5全称是报文摘要算法(Message-Digest Algorithm5),此算法对任意长度的信息逐位进行计算,产生一个二进制长度为128位(十六进制长度就是32位)的"指纹"(或称"报文摘要"),不同的文件产生相同的报文摘要的可能性是非常非常之小的. Message Digest Algorithm MD5为计算机安全领域广泛使用的一种散列函数,MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.它是用复杂的数学算法求得的一个12

JAVA中使用MD5加密工具类实现对数据的加密处理

1.MD5工具类 package com.ssm.util; import java.security.MessageDigest; public class MD5Util { //将字节数组转成十六进制字符串 private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.

javascript中使用md5函数

javascript中使用md5函数 这对于js来讲本来是没有的,现在可以自己定义一个md5的函数,达到加密效果. var hexcase = 0; function hex_md5(a) { if (a == "") return a; return rstr2hex(rstr_md5(str2rstr_utf8(a))) } function hex_hmac_md5(a, b) { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(a), s

C#统计目录中文件MD5值

1. [代码]统计目录中文件MD5值 using System.IO;using System.Security.Cryptography;using System.Collections;using System.Windows.Forms; IDataObject iData = Clipboard.GetDataObject();   if (iData.GetDataPresent(DataFormats.Text))   {string filepath=(string)iData.G

java中使用MD5加密的算法

MD5,全名Message Digest Algorithm 5,中文名为消息摘要算法第五版,为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.以下是JAVA语言中使用MD5加密的工具类. import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Util { /** * 对字符串进行MD5摘要加密,返回结果与MySQL的MD5函数一致 *

在swift中使用md5

在swift中使用md5 by 伍雪颖 在ProjectName-Bridging-Header.h中添加 #import<CommonCrypto/CommonCrypto.h> extension String { var MD5:String { let cString =self.cStringUsingEncoding(NSUTF8StringEncoding) let length =CUnsignedInt( self.lengthOfBytesUsingEncoding(NSU

eCharts图形在IE兼容模式中不能打开解决方法?

一.首先当然是看你页面布局是否正确. 二.对于eCharts在IE兼容模式中显示,需要指定meta元素中放入X-UA-Compatible ie=edge 标头. (不理解"X-UA-Compatible ie=edge"?) 实践中我的做法:

IOS中把字符串加密/IOS中怎么样MD5加密/IOS中NSString分类的实现

看完过后,你会学到: 1学习IOS开发中的分类实现, 2以及类方法的书写, 3以及字符串的MD5加密/解密. ---------------------------wolfhous------------------ [1]新建工程,新建分类 [2]分类的命名方式以及类的选择 [3]书写NSString分类的类方法,以及实现类方法,以及MD5如何加密 [4]测试加密结果 [5]解密:直接百度MD5解密,随便点歌网站进去,输入解密KEY,在我这个Demo中, wolfhous正常加密44bf025