C#字符串應用密鑰停止加解密。本站提示廣大學習愛好者:(C#字符串應用密鑰停止加解密)文章只能為提供參考,不一定能成為您想要的結果。以下是C#字符串應用密鑰停止加解密正文
第一個為年夜家分享的是C#字符串應用密鑰停止加解密代碼,詳細內容以下
public class DesEncrypt { /// <summary> /// 算法偏移量 /// </summary> const string m_IV = "12345678"; /// <summary> /// 功效描寫:依據輸出的密鑰生成8位密鑰 /// 作 者: 愛給模板網 2gei.cn /// 創立日期:2015-07-20 17:25:26 /// </summary> /// <param name="strkey">strkey</param> /// <returns>8位密鑰</returns> private static string GetKey(string strkey) { if (string.IsNullOrEmpty(strkey)) { strkey = "InfoColl"; } if (strkey.Length % 8 == 0) { return strkey; } else { return GetKey(strkey + "0"); } } /// <summary> /// 功效描寫:加密字符串 /// 作 者: 愛給模板網 2gei.cn /// 創立日期:2015-07-20 17:18:31 /// 義務編號: /// </summary> /// <param name="strSourceString">原字符串</param> /// <param name="strKey">密鑰</param> /// <returns>加密後的字符串</returns> public static string Encrypt(string strSourceString, string strKey) { strKey = GetKey(strKey); byte[] btKey = Encoding.UTF8.GetBytes(strKey); byte[] btIV = Encoding.UTF8.GetBytes(m_IV); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { try { byte[] inData = Encoding.UTF8.GetBytes(strSourceString); using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { return strSourceString; } } } /// <summary> /// 功效描寫:解密字符串 /// 作 者: 愛給模板網 2gei.cn /// 創立日期:2015-07-20 17:18:49 /// 義務編號: /// </summary> /// <param name="strEncryptedString">原字符串</param> /// <param name="strKey">密鑰</param> /// <returns>解密後的字符串</returns> public static string Decrypt(string strEncryptedString, string strKey) { strKey = GetKey(strKey); byte[] btKey = Encoding.UTF8.GetBytes(strKey); byte[] btIV = Encoding.UTF8.GetBytes(m_IV); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { try { byte[] inData = Convert.FromBase64String(strEncryptedString); using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } catch { return strEncryptedString; } } } }
C#字符串加密息爭密
using System.Security.Cryptography; using System.IO; //默許密鑰向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密鑰,請求為8位</param> /// <returns>加密勝利前往加密後的字符串,掉敗前往源串 </returns> public static string EncryptDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//轉換為字節 byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//實例化數據加密尺度 MemoryStream mStream = new MemoryStream();//實例化內存流 //將數據流鏈接到加密轉換的流 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密鑰,請求為8位,和加密密鑰雷同</param> /// <returns>解密勝利前往解密後的字符串,掉敗返源串</returns> public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } string EncryptStr = EncryptDESString.EncryptDES("aaaaaaaaaa", "ssssssss"); //前往加密後的字符串 string DecryptStr = EncryptDESString.DecryptDES(EncryptStr, "ssssssss");//解密字符串
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。