private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        private 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());
            }
            catch
            {
                return encryptString;
            }
        }        private static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                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)
            {
                return decryptString;
            }
        }
以上是我使用的对于字符串加解密的方法,以前使用都是OK的,但是现在使用加密时在 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
就有错误了,但是还可以继续,可以返回加密后的字符串,但是等到解密时,系统会Catch到Exception,也是在   CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);时就有错误了,错误信息是
+ Length “cStream.Length”引发了“System.NotSupportedException”类型的异常 long {System.NotSupportedException}
,,,+ Position “cStream.Position”引发了“System.NotSupportedException”类型的异常 long {System.NotSupportedException}然后要运行到cStream.FlushFinalBlock();才会跳到异常捕获代码。异常信息为“不正确的数据”,ex.StackTrace为:
   在 System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
   在 System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)
   在 System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   在 System.Security.Cryptography.CryptoStream.FlushFinalBlock()
   在 KM.Web.Utility.CryptBase.DecryptDES(String decryptString, String decryptKey) 位置 d:\KM3.0\03_Code\01_SourceCode\KM3.0\KM3.0\KMWeb\App_Code\CryptBase.cs:行号 57请各位牛人帮忙解决,谢谢!

解决方案 »

  1.   

    记得C#里边的DES加密只能用ASC,用别的报错.
      

  2.   

    用C#实现DES加密解密
    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;namespace Common
    ...{
        /**//// <summary>
        /// DESEncrypt加密解密算法。
        /// </summary>
        public sealed class DESEncrypt
        ...{
            private DESEncrypt()
            ...{
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }        private static string key = "zhoufoxcn";        /**//// <summary>
            /// 对称加密解密的密钥
            /// </summary>
            public static string Key
            ...{
                get
                ...{
                    return key;
                }
                set
                ...{
                    key = value;
                }
            }        /**//// <summary>
            /// DES加密
            /// </summary>
            /// <param name="encryptString"></param>
            /// <returns></returns>
            public static string DesEncrypt(string encryptString)
            ...{
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }        /**//// <summary>
            /// DES解密
            /// </summary>
            /// <param name="decryptString"></param>
            /// <returns></returns>
            public static string DesDecrypt(string decryptString)
            ...{
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
        }
    }
      

  3.   

    加密与解密只要有加密就够用了
    你将得到的字符串进行加密然后比较两串加密的字符串就可以。
    在我的空间里有MD5,DES等加解密的案例
      

  4.   

    我测试了一下你的代码.很正常
    public class Program
        {
            private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };         static void Main(string[] args)
            {
                string key = "123456789";            string str = "cc";            string str2 = EncryptDES(str, key);
                Console.WriteLine(str2);            Console.WriteLine(DecryptDES(str2, key));            Console.ReadKey();
            }        private 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());
                }
                catch
                {
                    return encryptString;
                }
            }        private static string DecryptDES(string decryptString, string decryptKey)
            {
                try
                {
                    byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
                    byte[] rgbIV = Keys;
                    byte[] inputByteArray = Convert.FromBase64String(decryptString);
                    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)
                {
                    return decryptString;
                }
            } 
        }   
      

  5.   

    不过你的代码看着就怪.那两个方法都有一个Key了(第二个参数)
    你还要一个private的Keys干啥?代码我也没仔细看,感觉很奇怪.不过能测试通过发两个方法,参考一下.我一直在用#region TripleDES加/解密
    /// <summary>
    /// TripleDES解密
    /// </summary>
    public static string TripleDESDecrypt(string encryptedString,string key)
    {
    if(encryptedString.Equals(string.Empty))
    {
    return encryptedString;
    }
    TripleDESCryptoServiceProvider tdes=new TripleDESCryptoServiceProvider();
     
    //将秘钥编码成为二进制数组
    PasswordDeriveBytes derive = new PasswordDeriveBytes(key,null);
    byte[] tdesIV=new byte[8];
    byte[] btaKey=derive.CryptDeriveKey("TripleDES","SHA1",0,tdesIV); //从字符串转换为字节组
    byte[] btaCode=System.Convert.FromBase64String(encryptedString); tdes.Mode = CipherMode.ECB;
    MemoryStream ms=new MemoryStream(btaCode); CryptoStream encStream = new CryptoStream(ms,tdes.CreateDecryptor(btaKey,tdesIV),CryptoStreamMode.Read);
    StreamReader sr=new StreamReader(encStream,System.Text.Encoding.Unicode);
    string strtmp=sr.ReadToEnd();
    sr.Close();
    encStream.Close(); return strtmp;
    } /// <summary>
    /// TripleDES加密
    /// </summary>
    public static string TripleDESEcrypt(string plainString,string key)
    {
    if(plainString.Equals(string.Empty))
    {
    return plainString;
    } TripleDESCryptoServiceProvider tdes=new TripleDESCryptoServiceProvider();

    //将秘钥编码成为二进制数组
    PasswordDeriveBytes derive = new PasswordDeriveBytes(key,null);
    byte[] tdesIV=new byte[8];
    byte[] btaKey=derive.CryptDeriveKey("TripleDES","SHA1",0,tdesIV); //从字符串转换为字节组
    byte[] btaCode=Encoding.Unicode.GetBytes(plainString); tdes.Mode = CipherMode.ECB;
    MemoryStream ms=new MemoryStream();

    CryptoStream encStream = new CryptoStream(ms,tdes.CreateEncryptor(btaKey,tdesIV),CryptoStreamMode.Write);
    encStream.Write(btaCode, 0, btaCode.Length);
    encStream.FlushFinalBlock();
    encStream.Close(); //再转换为一个字符串
    return System.Convert.ToBase64String(ms.ToArray());
    }
    #endregion
      

  6.   

    对了,我以前开发环境是英文版的visual studio2005,现在使用的时中文版的,不知这有影响吗?应该没有吧??
      

  7.   

    我要加密的时URL后面的字符串,例如 http:wwww.dddd.com/a.aspx?mod,406中的 “mod,406”进行加密,以前开发工具时英文版时是没有问题的,但现在visual studio2005时中文版的,就有问题了。
      

  8.   

    我试了一下mod,406用你的代码没问题.我的是英文的.我这没有中文的,没办法测.
      

  9.   

    http://blog.csdn.net/PPLUNCLE/archive/2006/06/21/817828.aspx你看一下这篇文章下面的评论.
      

  10.   

    byte[] rgbKey = Encoding.ASCII.GetBytes(encryptKey.Substring(0, 8));
    都改成ASC的.
      

  11.   

    我用英文版的应该也没有问题,但是为什么中文版的出现这个错误呢?根据搜索的信息,好像是设置BLOCKSIZE 和FEEDBACKSIZE  就行了,请问怎么设置?
      

  12.   

    用过加解密:#region   对数据进行加密
            /// <summary>
            /// 对数据进行加密
            /// </summary>
            /// <param name="encryptstring">需要加密的数据</param>
            /// <returns></returns>
            public string DESEncrypt(string encryptstring)
            {
                string strRtn;
                try
                {
                    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
                    byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
                    byte[] data = System.Text.Encoding.Unicode.GetBytes(encryptstring);
                    MemoryStream ms = new MemoryStream();//存储加密后的数据
                    CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
                    cs.Write(data, 0, data.Length);//进行加密
                    cs.FlushFinalBlock();
                    strRtn = Convert.ToBase64String(ms.ToArray());
                    return strRtn;
                }            catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
            }
            #endregion        #region   对数据进行解密
            /// <summary>
            /// 对数据进行解密
            /// </summary>
            /// <param name="decryptstring">需要解密的数据</param>
            /// <returns></returns>
            public string DESDecrypt(string decryptstring)
            {
    string strRtn;
                try
                {
                    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                    byte[] key = System.Text.Encoding.Unicode.GetBytes(encryptkey);
                    byte[] data = Convert.FromBase64String(decryptstring);
                    MemoryStream ms = new MemoryStream();//存储解密后的数据
                    CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
                    cs.Write(data, 0, data.Length);//解密数据
                    cs.FlushFinalBlock();
                    strRtn = System.Text.Encoding.Unicode.GetString(ms.ToArray());
                    return strRtn;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误消息提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
            }
            #endregion