那位高手做过C#加解密,我这加密没问题,解密总是返回空!
ECB DES解密
public static string DecryptDES(string encryptString, string encryptKey)
        {
            encryptString = encryptString.Replace(" ", "");
            encryptKey = encryptKey.Replace(" ", "");
            if ((encryptString.Length % 2) != 0)
                encryptString += " ";
            if ((encryptKey.Length % 2) != 0)
                encryptKey += " ";
            byte[] clearData = new byte[encryptString.Length / 2];
            byte[] keys = new byte[encryptKey.Length / 2];
            for (int i = 0; i < clearData.Length; i++)
            {
                clearData[i] = Convert.ToByte(encryptString.Substring(i * 2, 2), 16);
            }
            for (int i = 0; i < keys.Length; i++)
            {
                keys[i] = Convert.ToByte(encryptKey.Substring(i * 2, 2), 16);
            }
            try
            {
                DES desDecrypt = new DESCryptoServiceProvider();
                desDecrypt.Mode = CipherMode.ECB;
                desDecrypt.Key = keys;
                ICryptoTransform transForm = desDecrypt.CreateDecryptor();
                MemoryStream decryptedStreams = new MemoryStream();  
                CryptoStream cryptoStreams = new CryptoStream(decryptedStreams, transForm,CryptoStreamMode.Write);
                cryptoStreams.Write(clearData,0,clearData.Length);
                
                byte[] encryptedDatas = decryptedStreams.ToArray();
                string returnStrs = "";
                for (int i = 0; i < encryptedDatas.Length; i++)
                {
                    returnStrs += encryptedDatas[i].ToString("X2");
                }
                return returnStrs;
            }
            catch (Exception e)
            {
                 return e.Message;          
            }
        }
调用解密函数string desjms = DecryptDES("0DA06156D09594C3", "3837363534333231");
应返回3132333435363738 为正确

解决方案 »

  1.   


    static byte[] rgbKey = { 22, 33, 97, 10, 5, 7, 67, 38 }; //des加密钥匙
        static byte[] rgbIV = { 10, 23, 34, 45, 51, 99, 27, 68 };//des解密钥匙    /// <summary>
        /// des加密字符串
        /// </summary>
        /// <param name="encryptString">需要加密的字符串</param>
        /// <returns></returns>
        public static string DESEncrypt(string encryptString)
        {
            try
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, desc.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>
        /// <returns></returns>
        public static string DESDecrypt(string decryptString)
        {
            try
            {
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, desc.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                    }
                    return Encoding.UTF8.GetString(mStream.ToArray());
                }
            }
            catch
            {
                return decryptString;
            }
        }
      

  2.   

    需要ECB DES解密,上面的是标准加解密,不是针对ECB模式
    可按照我的数据和密钥解出相对应的数据即可。。谢谢!
      

  3.   

    http://blog.csdn.net/sdfkfkd/article/details/6004847