DES(KL,DES-1(KR,DES(KL,X))), 究竟怎样用work key "31313131313131313232323232323232" 来加密"33333333333333333333333333333333"呢?(用3des算法)望高手指教!

解决方案 »

  1.   

    http://www.codeproject.com/samples/cryptest__mfc_style_.asp
      

  2.   

    http://www.fpga.com.cn/application/a69.htm
      

  3.   

    本代码参照了部分MSDN上的代码示例,再根据自己的实际情况,补充了一部分MSDN上没有提到的内容 using System;using System.Security;using System.Security.Cryptography;using System.IO;using System.Text;using System.Threading; namespace TRIP3DES{    ///     /// Class1 的摘要说明。    ///     public class dllEncrypt    {      //密钥      private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";       //矢量,矢量可以为空      private const string sIV = "qcDY6X+aPLw=";      //构造一个对称算法      private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();         public dllEncrypt(){}       #region public string EncryptString(string Value)      ///       /// 加密字符串      ///       /// 输入的字符串      /// 加密后的字符串      public string EncryptString(string Value)      {         ICryptoTransform ct;         MemoryStream ms;         CryptoStream cs;         byte[] byt;         mCSP.Key = Convert.FromBase64String(sKey);         mCSP.IV = Convert.FromBase64String(sIV);         //指定加密的运算模式         mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;        //获取或设置加密算法的填充模式         mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;         ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);         byt = Encoding.UTF8.GetBytes(Value);         ms = new MemoryStream();         cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);         cs.Write(byt, 0, byt.Length);         cs.FlushFinalBlock();         cs.Close();         return Convert.ToBase64String(ms.ToArray());      }      #endregion       #region public string DecryptString(string Value)      ///       /// 解密字符串      ///       /// 加过密的字符串      /// 解密后的字符串      public string DecryptString(string Value)      {         ICryptoTransform ct;         MemoryStream ms;         CryptoStream cs;         byte[] byt;         mCSP.Key = Convert.FromBase64String(sKey);         mCSP.IV = Convert.FromBase64String(sIV);         mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;         mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;         ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);         byt = Convert.FromBase64String(Value);         ms = new MemoryStream();         cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);         cs.Write(byt, 0, byt.Length);         cs.FlushFinalBlock();         cs.Close();          return Encoding.UTF8.GetString(ms.ToArray());      }      #endregion    }} 三、总结做成类库对于密钥和矢量的保管比较方便,输入输出全部是string型变量,这样也比较方便,密钥的生成可以用mSCP. GenerateKey()来生成,矢量的生成也可以用mCSP.GenerateIV()来生成。大家也可以自己灵活的编写符合自己的3DES算法。
      

  4.   

    我现在有基于C代码的Des算法,3Des算法只是Des算法的衍生,这个公式DES(KL,DES-1(KR,DES(KL,X)))是我在网上看到的,现在有一个问题,因为单Des算法接收的key和data(待加密数据)均是16byte,根据上述公式,我认为3des算法就是进行3次des运算,也就是将32位的key划分成left key(16byte)和right key(16byte),即上述公式中的KL和KR,但是这样理解有一个矛盾是X(待加密数据)的长度问题,因为单des算法中X的长度应该是16byte,但实际上3des算法加密的数据长度是32byte(与key 长度对应),所以困惑中......                        请高手指教!
      

  5.   

    知道了,就是x实际上只能是16byte,如果是32byte,将它分为2次分别计算。