C# MD5加密字符串

/// <summary>
/// 用MD5加密字符串,可选择生成16位或者32位的加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <param name="bit">位数,一般取值16 或 32</param>
/// <returns>返回的加密后的字符串</returns>
public string MD5Encrypt(string password, int bit)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
if (bit == 16)
return tmp.ToString().Substring(8, 16);
else
if (bit == 32) return tmp.ToString();//默认情况
else return string.Empty;
}
/// <summary>
/// 用MD5加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <returns></returns>
public string MD5Encrypt(string password)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
return tmp.ToString();
}

命名空间:System.Security.Cryptography.MD5CryptoServiceProvider

原文地址:https://www.cnblogs.com/xifengyeluo/p/8215062.html

时间: 2024-10-10 09:11:21

C# MD5加密字符串的相关文章

C#加密方法汇总(SHA1加密字符串,MD5加密字符串,可逆加密等)

using System;using System.Collections.Generic;using System.Text; namespace StringEncry{ class EncodeAndDecode { /// <summary> /// Base64加密 /// </summary> /// <param name="codeName">加密采用的编码方式</param> /// <param name=&qu

iOS中MD5加密字符串实现

在说原型模式之前,我们先来看java里面的深复制和浅复制: 1. 浅复制:被复制的对象的所有变量都持有和原来对象的变量相同的值,而所有的对其他对象的引用都指向原来的对象. 2. 深复制:被复制对象的所有变量都含有与原来对象相同的值,除去那些引用其他变量的对象.那些引用其他对象的变量将指向被复制过来的新对象,而不是原来那些被引用的对象.深复制需要把要复制的对象的所有引用都复制一遍. 这两者的区别就是关于引用对象的处理,浅复制是不考虑引用对象,而深复制需要考虑引用对象的问题. 对java中的clon

[iOS]MD5加密字符串

将字符串进行MD5加密,返回加密后的字符串. #import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access(引入头文件) - (NSString *)md5:(NSString *)str{    const char *cStr = [str UTF8String];    unsigned char result[16];    CC_MD5(cStr, strlen(cStr), result); //

C#中使用SHA1和MD5加密字符串

SHA1和MD5加密均为不可逆加密.代码如下: using System.Security.Cryptography; //添加Using static void Main(string[] args) { string password = "0123456789"; //SHA1加密方法 var sha1 = new SHA1CryptoServiceProvider(); byte[] str01 = Encoding.Default.GetBytes(password); by

MD5加密字符串--基于python

import hashlib#md5加密32位def md5(str): import hashlib m = hashlib.md5() m.update(str) return m.hexdigest() for i in range(20000,20020): print md5(str(i))

MD5 加密字符串

public class MD5 { /*** * MD5加码 生成32位md5码 */ public static String string2MD5(String inStr){ MessageDigest md5 = null; try{ md5 = MessageDigest.getInstance("MD5"); }catch (Exception e){ System.out.println(e.toString()); e.printStackTrace(); retur

.Net实现Md5加密字符串

using System;using System.Configuration;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web

Lua MD5加密字符串

function md5_sumhexa(k) local md5_core = require "md5.core" k = md5_core.sum(k) return (string.gsub(k, ".", function (c) return string.format("%02x", string.byte(c)) end ) ) end print(md5_sumhexa("Hello World!")) md

MD5加密字符串-备用

@interface NSString (MyExtensions) - (NSString *) md5; @end @implementation NSString (MyExtensions) - (NSString *) md5 { const char *cStr = [self UTF8String]; unsigned char result[16]; CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call ret