日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DES 加密解密

發(fā)布時間:2025/3/13 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DES 加密解密 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【概念】?

數(shù)據(jù)加密算法(Data Encryption Algorithm,DEA)是一種對稱加密算法,很可能是使用最廣泛的密鑰系統(tǒng),特別是在保護金融數(shù)據(jù)的安全中,最初開發(fā)的DEA是嵌入硬件中的。通常,自動取款機(Automated Teller Machine,ATM)都使用DEA。它出自IBM的研究工作【from baidu】

?

【部分代碼實現(xiàn)】

#region DES加密/解密字符串//默認密鑰向量private static byte[] Keys = { 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xBB, 0xCC };public static string EncryptDES(string encryptString){string encryptKey = StringUtils.ConvertToStr(GetConfig("CryptDESKey"), "abcd");//GetConfig方法為從配置文件讀區(qū)數(shù)據(jù)return EncryptDES(encryptString, encryptKey);}/// <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()).Replace("+", "%20");}catch (Exception ex){return string.Empty;}}public static string DecryptDES(string decryptString){string decryptKey = StringUtils.ConvertToStr(GetConfig("CryptDESKey"), "abcd");return DecryptDES(decryptString, decryptKey);}/// <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.Replace(' ', '+'));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 (Exception ex){WrLogger logger = new WrLogger();logger.WriteException("App_Code.WebUtils.DecryptDES(string, string) Error, DecryptString:" + decryptString, ex);return string.Empty;}}#endregion

?注:此日志僅留作以后查詢。

轉(zhuǎn)載于:https://www.cnblogs.com/fo0ol/p/3614296.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的DES 加密解密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。