加密代码如下:
     public class EncryptUtil
    {
        private  string _key ="m2343223152315692";  //密钥
        private  string _iv = "1134_222#33%6%888"; //初始化向量        public  string DESEncrypt(string encryptStr)
        {
            return DESEncrypt(encryptStr, _key, _iv);
        }        public  string DESDecrypt(string encryptedValue)
        {
            return DESDecrypt(encryptedValue, _key, _iv);
        }        public  string DESEncrypt(string encryptStr, string key, string IV)
        {
            //将key和IV处理成8个字符 
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);            SymmetricAlgorithm sa;
            ICryptoTransform ict;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;            sa = new DESCryptoServiceProvider();
            sa.Key = _encoding.GetBytes(key);
            sa.IV = _encoding.GetBytes(IV);
            ict = sa.CreateEncryptor();            byt = _encoding.GetBytes(encryptStr);            ms = new MemoryStream();
            cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();            cs.Close();            //加上一些干扰字符 
            string retVal = Convert.ToBase64String(ms.ToArray());
            System.Random ra = new Random();
            for (int i = 0; i < 8; i++)
            {
                int radNum = ra.Next(36);
                char radChr = Convert.ToChar(radNum + 65);//生成一个随机字符                 retVal = retVal.Substring(0, 2 * i + 1) + radChr.ToString() + retVal.Substring(2 * i + 1);
            }            return retVal;
        }        public  string DESDecrypt(string encryptedValue, string key, string IV)
        {
            //去掉干扰字符 
            string tmp = encryptedValue;
            if (tmp.Length < 16)
            {
                return "";
            }            for (int i = 0; i < 8; i++)
            {
                tmp = tmp.Substring(0, i + 1) + tmp.Substring(i + 2);
            }
            encryptedValue = tmp;            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);            SymmetricAlgorithm sa;
            ICryptoTransform ict;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;            try
            {
                sa = new DESCryptoServiceProvider();
                sa.Key = _encoding.GetBytes(key);
                sa.IV = _encoding.GetBytes(IV);
                ict = sa.CreateDecryptor();                byt = Convert.FromBase64String(encryptedValue); //抛出异常处
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();                cs.Close();                return _encoding.GetString(ms.ToArray());
            }
            catch (System.Exception)
            {
                return "";
            }
        }
    }
    在进行代码调试时,对于使用DESEncrypt()方法得到的加密字符串,执行到“byt = Convert.FromBase64String(encryptedValue); ”时有时会抛出“字符串中存在无效字符”异常。不知是为何?
    在微软官网上,查了下对应的MSDN帮助,猜测可能是字符串中存在非法字符所致,但又不知是什么?

解决方案 »

  1.   


    /// <summary>
            /// 进行DES加密。
            /// </summary>
            /// <param name="pToEncrypt">要加密的字符串。</param>
            /// <param name="sKey">密钥,且必须为8位。</param>
            /// <returns>以Base64格式返回的加密字符串。</returns>
            public string Encrypt(string pToEncrypt, string sKey)
            {
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }        /// <summary>
            /// 进行DES解密。
            /// </summary>
            /// <param name="pToDecrypt">要解密的以Base64</param>
            /// <param name="sKey">密钥,且必须为8位。</param>
            /// <returns>已解密的字符串。</returns>
            public string Decrypt(string pToDecrypt, string sKey)
            {
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }        
      

  2.   

    拷丢了一句代码:
    private Encoding _encoding = new UnicodeEncoding();