今天小編就為大家?guī)硪黄狢#中的常見加密解密類的文章。小編覺得挺不錯的,為此分享給大家做個參考。一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,安澤企業(yè)網(wǎng)站建設,安澤品牌網(wǎng)站建設,網(wǎng)站定制,安澤網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,安澤網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。常見的加密方式分為可逆和不可逆兩種方式
可逆:RSA,AES,DES等
不可逆:常見的MD5,SHAD等
常見的加密方式封裝到一個Password類中
public class Password { ////// 此代碼示例通過創(chuàng)建哈希字符串適用于任何 MD5 哈希函數(shù) (在任何平臺) 上創(chuàng)建 32 個字符的十六進制格式哈希字符串 /// 官網(wǎng)案例改編 /// /// ///public static string Get32MD5One(string source) { using (System.Security.Cryptography.MD5 md5Hash = System.Security.Cryptography.MD5.Create()) { byte[] data = md5Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source)); System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } string hash = sBuilder.ToString(); return hash.ToUpper(); } } /// /// 獲取16位md5加密 /// /// ///public static string Get16MD5One(string source) { using (System.Security.Cryptography.MD5 md5Hash = System.Security.Cryptography.MD5.Create()) { byte[] data = md5Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source)); //轉換成字符串,并取9到25位 string sBuilder = BitConverter.ToString(data, 4, 8); //BitConverter轉換出來的字符串會在每個字符中間產(chǎn)生一個分隔符,需要去除掉 sBuilder = sBuilder.Replace("-", ""); return sBuilder.ToString().ToUpper(); } } //// /// /// 需要加密的明文 ///返回32位加密結果,該結果取32位加密結果的第9位到25位 public static string Get32MD5Two(string source) { System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //獲取密文字節(jié)數(shù)組 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(source)); //轉換成字符串,32位 string strResult = BitConverter.ToString(bytResult); //BitConverter轉換出來的字符串會在每個字符中間產(chǎn)生一個分隔符,需要去除掉 strResult = strResult.Replace("-", ""); return strResult.ToUpper(); } /////// /// 需要加密的明文 ///返回16位加密結果,該結果取32位加密結果的第9位到25位 public static string Get16MD5Two(string source) { System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //獲取密文字節(jié)數(shù)組 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(source)); //轉換成字符串,并取9到25位 string strResult = BitConverter.ToString(bytResult, 4, 8); //BitConverter轉換出來的字符串會在每個字符中間產(chǎn)生一個分隔符,需要去除掉 strResult = strResult.Replace("-", ""); return strResult.ToUpper(); } //SHA為不可逆加密方式 public static string SHA1Encrypt(string normalTxt) { var bytes = System.Text.Encoding.Default.GetBytes(normalTxt); var SHA = new System.Security.Cryptography.SHA1CryptoServiceProvider(); var encryptbytes = SHA.ComputeHash(bytes); return Convert.ToBase64String(encryptbytes); } public static string SHA256Encrypt(string normalTxt) { var bytes = System.Text.Encoding.Default.GetBytes(normalTxt); var SHA256 = new System.Security.Cryptography.SHA256CryptoServiceProvider(); var encryptbytes = SHA256.ComputeHash(bytes); return Convert.ToBase64String(encryptbytes); } public static string SHA384Encrypt(string normalTxt) { var bytes = System.Text.Encoding.Default.GetBytes(normalTxt); var SHA384 = new System.Security.Cryptography.SHA384CryptoServiceProvider(); var encryptbytes = SHA384.ComputeHash(bytes); return Convert.ToBase64String(encryptbytes); } public string SHA512Encrypt(string normalTxt) { var bytes = System.Text.Encoding.Default.GetBytes(normalTxt); var SHA512 = new System.Security.Cryptography.SHA512CryptoServiceProvider(); var encryptbytes = SHA512.ComputeHash(bytes); return Convert.ToBase64String(encryptbytes); } ////// 將base64格式,轉換utf8 /// /// 解密內(nèi)容 ///public static string Base64Decode(string content) { byte[] bytes = Convert.FromBase64String(content); return System.Text.Encoding.UTF8.GetString(bytes); } /// /// DES加密數(shù)據(jù) /// /// /// ///public static string DESEncryption(string Text, string sKey=null) { sKey = sKey ?? "zhiqiang"; try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = System.Text.Encoding.Default.GetBytes(Text); string md5SKey = Get32MD5One(sKey).Substring(0, 8); des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.StringBuilder ret = new System.Text.StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); }catch { return "error"; } } /// /// DES解密數(shù)據(jù) /// /// /// ///public static string DESDecrypt(string Text, string sKey=null) { sKey = sKey ?? "zhiqiang"; try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); int len; len = Text.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } string md5SKey = Get32MD5One(sKey).Substring(0, 8); des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }catch { return "error"; } } /// /// RSA加密數(shù)據(jù) /// /// /// ///public static string RSAEncryption(string express, string KeyContainerName = null) { System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters(); param.KeyContainerName = KeyContainerName ?? "zhiqiang"; //密匙容器的名稱,保持加密解密一致才能解密成功 using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param)) { byte[] plaindata = System.Text.Encoding.Default.GetBytes(express);//將要加密的字符串轉換為字節(jié)數(shù)組 byte[] encryptdata = rsa.Encrypt(plaindata, false);//將加密后的字節(jié)數(shù)據(jù)轉換為新的加密字節(jié)數(shù)組 return Convert.ToBase64String(encryptdata);//將加密后的字節(jié)數(shù)組轉換為字符串 } } /// /// RSA解密數(shù)據(jù) /// /// /// ///public static string RSADecrypt(string ciphertext, string KeyContainerName = null) { System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters(); param.KeyContainerName = KeyContainerName ?? "zhiqiang"; using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param)) { byte[] encryptdata = Convert.FromBase64String(ciphertext); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return System.Text.Encoding.Default.GetString(decryptdata); } } }
以上就是C#中的常見加密解密類的具體內(nèi)容,代碼詳細清楚,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道!
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。