C# SHA256加密算法记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# SHA256加密算法记录相关的知识,希望对你有一定的参考价值。
1 using System.Text; 2 using System.Diagnostics; 3 using System.Security; 4 using System.Security.Cryptography; 5 6 protected void Page_Load(object sender, EventArgs e) 7 { 8 string salted = "abcdefghijklm"; 9 string digest = SHA256Encrypt(salted); 10 11 byte[] b = System.Text.Encoding.Default.GetBytes(digest); 12 13 Response.Write(Convert.ToBase64String(b)); 14 Response.End(); 15 } 16 17 public string SHA256Encrypt(string strIN) 18 { 19 //string strIN = getstrIN(strIN); 20 byte[] tmpByte; 21 SHA256 sha256 = new SHA256Managed(); 22 23 tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN)); 24 sha256.Clear(); 25 26 return GetStringValue(tmpByte); 27 28 } 29 30 private string GetStringValue(byte[] Byte) 31 { 32 string tmpString = ""; 33 ASCIIEncoding Asc = new ASCIIEncoding(); 34 tmpString = Asc.GetString(Byte); 35 return tmpString; 36 } 37 38 private byte[] GetKeyByteArray(string strKey) 39 { 40 ASCIIEncoding Asc = new ASCIIEncoding(); 41 42 int tmpStrLen = strKey.Length; 43 byte[] tmpByte = new byte[tmpStrLen - 1]; 44 45 tmpByte = Asc.GetBytes(strKey); 46 47 return tmpByte; 48 49 }
以上是关于C# SHA256加密算法记录的主要内容,如果未能解决你的问题,请参考以下文章