C#学习(1)
来源:互联网 发布:海南大学知乎 编辑:程序博客网 时间:2024/06/08 11:27
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; } }
0 0
- 学习 C# (1)
- C#学习(1)
- C#学习日志(1)
- c#学习(基础1)
- 学习C#数组(1)
- C#学习笔记(1)
- C# 学习笔记(1)
- C#学习(1)——C#语言
- c#俄罗斯方块的学习(1)
- 学习C#基础知识(五)1
- C# 封装控件 学习(1)
- c#学习笔记(1)GridView问题
- C#学习之委托(1)
- C#学习之文件操作(1)
- C#代码优化学习总结(1)
- C#多线程学习(1-6)
- 学习C#1(窗体与界面设计)
- C#寒假学习笔记(1)
- 第七题(3)
- smack中ProviderManager的学习
- SQLServer 递归查询
- pthread 基础编程
- Excel Sheet Column Number - LeetCode 171
- C#学习(1)
- cookielib和urllib2模块相结合模拟网站登录
- 我的第一篇博客。
- C语言输出整数整数前补全零
- SQLite的使用
- 《CTCI》1.3 变位词
- 变频器维修技术光碟上市了
- Excel Sheet Column Title - LeetCode 168
- Android 中Home键和Back键监听的区别以及代码实现(附源代码)