using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Security.Cryptography;
namespace Application1
{
class Program
{
static int counter = 0;
static string displayString = "Things can only get better!";
public static string SHA256(string str)
{
// 如果str有中文,不同Encoding的sha是不同的!!
byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
SHA256Managed Sha256 = new SHA256Managed();
byte[] by = Sha256.ComputeHash(SHA256Data);
return BitConverter.ToString(by).Replace("-", "").ToLower(); // 64
// return Convert.ToBase64String(by); // 44
}
static void Main(string[] args)
{
string strPassword = "12345678";
string sha = SHA256(strPassword);
Console.WriteLine("SHA256:{0}", sha);
Console.ReadKey();
}
}
}
代码
//如果是文本,先转换为字节序列,如果有汉字,请不要使用 ASCII 编码,会导致汉字变成问号
byte[] buffer = Encoding.UTF8.GetBytes("洪星的博客");//UTF-8 编码
buffer = Encoding.Default.GetBytes("洪星的博客");//GBK/GB2312 编码
//16字节,128位
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] h1 = MD5.ComputeHash(buffer);
//20字节,160位
SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
byte[] h2 = SHA1.ComputeHash(buffer);
//32字节,256位
SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider();
byte[] h3 = SHA256.ComputeHash(buffer);
//48字节,384位
SHA384CryptoServiceProvider SHA384 = new SHA384CryptoServiceProvider();
byte[] h4 = SHA384.ComputeHash(buffer);
//64字节,512位
SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
byte[] h5 = SHA512.ComputeHash(buffer);
string s1 = BitConverter.ToString(h1).Replace("-", string.Empty);
string s2 = BitConverter.ToString(h2).Replace("-", string.Empty);
string s3 = BitConverter.ToString(h3).Replace("-", string.Empty);
string s4 = BitConverter.ToString(h4).Replace("-", string.Empty);
string s5 = BitConverter.ToString(h5).Replace("-", string.Empty);
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
Console.WriteLine(s5);
public static CookieContainer GetCooKie(string url, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream;
//stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();
//接收响应
//response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
//Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
Console.WriteLine(strcrook);
return cc;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw ex;
}
}