国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net實(shí)現(xiàn)的DES加密解密操作示例

2024-07-10 13:32:31
字體:
供稿:網(wǎng)友

本文實(shí)例講述了asp.net實(shí)現(xiàn)的DES加密解密操作。分享給大家供大家參考,具體如下:

//加密方法private string encrypt(string strToEncrypt){    if (strToEncrypt == null || strToEncrypt == "") return strToEncrypt;    DESCryptoServiceProvider des = new DESCryptoServiceProvider();    //把字符串放到byte數(shù)組中,主意編碼方式    byte[] inputByteArray = Encoding.Default.GetBytes(strToEncrypt);    //建立加密對象的密鑰和偏移量    des.Key = new byte[] { 1, 3, 5, 7, 2, 4, 6, 8 };    des.Mode = CipherMode.ECB;    MemoryStream ms = new MemoryStream();    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);    cs.Write(inputByteArray, 0, inputByteArray.Length);    cs.FlushFinalBlock();    StringBuilder ret = new StringBuilder();    foreach (byte b in ms.ToArray())    {      ret.AppendFormat("{0:X2}", b);    }    return ret.ToString();}//解密方法private string Decrypt(string pToDecrypt){    if (pToDecrypt == null || pToDecrypt == "") return pToDecrypt;    try    {      DESCryptoServiceProvider des = new DESCryptoServiceProvider();      //Put the input string into the byte array      byte[] inputByteArray = new byte[pToDecrypt.Length / 2];      for (int x = 0; x < pToDecrypt.Length / 2; x++)      {        int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));        inputByteArray[x] = (byte)i;      }      //key      des.Key = new byte[] { 1, 3, 5, 7, 2, 4, 6, 8 }; ;      //des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);      des.Mode = CipherMode.ECB;      MemoryStream ms = new MemoryStream();      CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);      cs.Write(inputByteArray, 0, inputByteArray.Length);      cs.FlushFinalBlock();      //建立StringBuild對象,CreateDecrypt使用的是流對象,必須把解密后的文本變成流對象      StringBuilder ret = new StringBuilder();      return System.Text.Encoding.Default.GetString(ms.ToArray());    }    catch (Exception Exp)    {      return String.Empty;    }}

希望本文所述對大家asp.net程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵台县| 南康市| 孟州市| 平远县| 瑞昌市| 兴山县| 文安县| 长丰县| 恩施市| 越西县| 灵寿县| 柳林县| 瑞安市| 洞头县| 潞城市| 香格里拉县| 陕西省| 滁州市| 郯城县| 山东| 尼木县| 仙居县| 自治县| 佛山市| 东光县| 红桥区| 南漳县| 县级市| 上蔡县| 钟山县| 雅安市| 枣庄市| 凭祥市| 南康市| 安陆市| 化德县| 徐汇区| 从化市| 雷山县| 东乡| 东乡|