由于项目中要和php对接,要将一段字符串生成md5(16位)验证码,在英文字符时,没有太大问题,但在遇到中文时,两边字条始终不一致。

php是别人的项目,看不到源码,网上一查,估计是这样写的:

<?php
echo substr(md5("admin"),8,16); // 16位MD5加密
echo "<hr>";
echo md5("admin"); // 32位MD5加密
?>

之前的 md5代如下(对md5没有过多研究,这段代码也是网上找的):

public static string Md5_16(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), , );
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}

在苦恼之际,想到以前还有一种md5的做法,得出的值和php生成的一比对,是正确的。但现在4.5环境下警告该方法过期了

return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();   //32位

return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(, );  //16位

本着认真负责的精神,再找找找(刚好也有时间),结果还是在msdn上找到的一段代码,只不过把 HashPasswordForStoringInConfigFile 这个方法重新实现了下

public static string GetMd5Hash(string input)
{
MD5 md5Hash = MD5.Create(); // Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = ; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
} // Return the hexadecimal string.
return sBuilder.ToString();
}

来源链接:https://social.msdn.microsoft.com/Forums/zh-TW/590bd6a8-57d7-4041-81da-80fe8b832b77/md5

FormsAuthentication.HashPasswordForStoringInConfigFile 的替代方法的更多相关文章

  1. FormsAuthentication.HashPasswordForStoringInConfigFile方法再.net core中的替代代码

    FormsAuthentication.HashPasswordForStoringInConfigFile()这个加密方法再.net core中不存在了,可以用下面的方式达到一样的加密效果 usin ...

  2. FormsAuthentication.HashPasswordForStoringInConfigFile 方法 之研究

    摘自:http://time-is-life.cnblogs.com/articles/322523.html 给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码. [C#] ...

  3. System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)已过时的解决办法

    FormsAuthentication.HashPasswordForStoringInConfigFile 方法是一个在.NET 4.5中已经废弃不用的API,参见: https://msdn.mi ...

  4. iOS中的过期方法和新的替代方法

    关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...

  5. spring替代方法

    总结spring替代方法的使用 MyValueCalculator类中的computerValue方法将会被替代 public class MyValueCalculator { public Str ...

  6. Android中getDrawable和getColor过时的替代方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 Android SDK 升级到 23 之后,getDrawable和getColor方法提示过时. 解决方案 getResources(). ...

  7. 画删除线的方法,如何找替代方法,Deprecated注释

    用@Deprecated注释的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择.在使用不被赞成的程序元素或在不被赞成的代码中执行重写时,编译器会发出警告. 那么相应的替代方法应 ...

  8. sizeWithFont:的替代方法

    sizeWithFont:的替代方法 -(CGFloat)changeStationWidth:(NSString *)string{ UIFont * textFont = [UIFont syst ...

  9. 新版 Scrapy 中 sys.conf.settings 的替代方法

    新版 Scrapy 中 sys.conf.settings 的替代方法 在 scrapy 项目目录下,有个 settings.py 文件,此文件是用来存放爬虫项目的各种配置,比如说 MongoDB 的 ...

随机推荐

  1. 三栏布局之 css3 calc和 flex

    圣杯布局的实现,有很多种. 大致都是借助 padding, margin, float之类的,当然这是传统的实现方式.更多的参考方式圣杯布局小结. 这里说的是用css3 cal 和flex来实现,因为 ...

  2. DIV里面的图片水平与垂直居中的方法

    <div class=“box”> <img /> </div> 1.水平居中: 1)box设置  text-align:center ;    text-alig ...

  3. python文件处理相关函数

    用open()创建文件 open('a.txt','wt') 用exists()检查文件是否存在 os.path.exists() 用isfile()检查是否为文件 os.path.isfile(na ...

  4. Spring MVC 详解之废话少说

    <陈翔六点半之废话少说>.... Spring WEB MVC 的请求流程: Spring WEB MVC架构: 开始创建.配置gradle项目 1.在gralde项目中,选择SDK 和框 ...

  5. RHM-M60型挖掘机力矩限制器/载荷指示器

    RHM-M60挖掘机力矩限制器RHM-M60 excavator crane moment limiter     RHM-M60型挖掘机力矩限制器是臂架型起重机机械的安全保护装置,本产品采用32位高 ...

  6. AdaBoostRegressor

    class sklearn.ensemble.AdaBoostRegressor(base_estimator=None, n_estimators=50, learning_rate=1.0, lo ...

  7. Machine Learning - week 4 - Non-linear Hypotheses

    为什么计算机图像识别很难呢?因为我们看到的是汽车,而计算机看到的是表示颜色的 RGB 数值.计算机需要根据这些数值来判断. 如果图片是 50 * 50 像素,那么一共有 2500 个像素点.如果是 Q ...

  8. Gym 101667I Slot Machines

    原题传送门 题意:给定n(n≤106)个数,要求将它化为混偱环小数的形式,即前k个数不参与循环,之后所有数以p为循环节长度进行循环.求k和p,要求k+p尽量小,k+p相等时要求p尽量小. 样例1 输入 ...

  9. Oracle实战笔记(第二天)

    导读 今日主要内容:表管理.表操作(增删改查).表查询(简单查询&复杂查询).创建数据库. 一.表管理 1.表命名规范 必须以字母开头: 长度不能超过30个字符: 不能使用Oracle保留字: ...

  10. post请求测试

    也可以用下图来指定测试参数