zoukankan      html  css  js  c++  java
  • C# 实现3Des加密 解密

    3Des对每个数据块进行了三次的DES加密算法,是DES的一个更安全的变形。比起最初的DES,3DES更为安全。

    都是感觉一目了然的摘过来。

    下面是加密解密的源码。ECB模式的。

     1 public class _3DESEncrypt
     2     {
     3 
     4         public static string Encrypt3DES(string a_strString, string a_strKey)
     5         {
     6             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
     7             DES.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a_strKey, "md5").Substring(0, 24));
     8             DES.Mode = CipherMode.ECB;
     9             ICryptoTransform DESEncrypt = DES.CreateEncryptor();
    10             byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
    11             return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    12         }
    13 
    14         public static string Decrypt3DES(string a_strString, string a_strKey)
    15         {
    16             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
    17             DES.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a_strKey, "md5").Substring(0, 24));
    18             DES.Mode = CipherMode.ECB;
    19             DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
    20             ICryptoTransform DESDecrypt = DES.CreateDecryptor();
    21             string result = "";
    22             try
    23             {
    24                 byte[] Buffer = Convert.FromBase64String(a_strString);
    25 
    26                 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
    27 
    28                 //MemoryStream msDecrypt = new MemoryStream(Buffer);
    29                 //CryptoStream csDecrypt = new CryptoStream(msDecrypt,
    30                 //       DES.CreateDecryptor(DES.Key, DES.IV),
    31                 //       CryptoStreamMode.Read);
    32 
    33                 //// Create buffer to hold the decrypted data.
    34                 //byte[] fromEncrypt = new byte[Buffer.Length];
    35 
    36                 //// Read the decrypted data out of the crypto stream
    37                 //// and place it into the temporary buffer.
    38                 //csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
    39                 //result = System.Text.Encoding.Default.GetString(fromEncrypt);
    40             }
    41             catch (Exception e)
    42             {
    43             }
    44             return result;
    45 
    46         }
    47     }

    里面加解密都是在DES的基础上实现、区别在于3Des的Key值是24位、DES而是8位。
    DES的加密解密源码:http://www.cnblogs.com/tainshi/p/3501258.html

    对于3DES算法的CBC模式、我是新人存在迷惑、解密出来的数据有乱码现象。大大们有这方面有关的文章、推荐给我喽。

  • 相关阅读:
    转载:SQL server2005 里面没有management studio!下载SQL开发版本
    LInux、Ubuntu、win7、win8纯净版 镜像包链接地址 收集
    [转]Lucene经验总结 (转注:较旧,但有干货)
    几篇调试IIS内存过高或CPU过高的好文章
    Web应用程序
    使用ASP.NET State Server实现多应用程序间共享Session State
    [转]使用visual studio进行web应用程序性能测试
    [转] ASP.NET WEB API程序在VS启动或发布到IIS后启动后发生
    [转]优化Redis内存的9个要点
    [转]使用Memcached的8个要点
  • 原文地址:https://www.cnblogs.com/tainshi/p/3507091.html
Copyright © 2011-2022 走看看