请高手明示,我在弄一个传输文件的实例,其中要对各种不同类型的文件进行加密,在接收方进行解码。遇到最多的就是编码方式的不同,导致的乱码。有没有比较完善的加密和解密的类啊,这个问题都困扰我一星期了,给点解决办法也好啊。尤其像rar文件,解密过程中就会出错,一般文本文件都可以加密解密的。请高手指教以下是我用的一个DES类,不知道这个类本身对解密rar文件有问题吗?//注意事项:sKey要为8位
        /// <summary>
        /// 加密原函数
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串</param>
        /// <param name="sKey">加密密钥</param>
        /// <returns>字符串</returns>
        public static string DesEncrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }
        /// <summary>
        /// 解密原函数
        /// </summary>
        /// <param name="pToDecrypt">解密字符串</param>
        /// <param name="sKey">解密密钥</param>
        /// <returns>字符串</returns>
        public static string DesDecrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }
给指条活路吧!在线等!

解决方案 »

  1.   

    会报这种错误 已引发:“未能找到任何可识别的数字。”(System.FormatException) 异常消息 = "未能找到任何可识别的数字。", 异常类型 = "System.FormatException" 是由于以下代码报的错for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
      

  2.   

    我是采用文件流缓冲1MB文件直接base64加密解密的方式
    Encoding.Default.GetString(Convert.FromBase64String("所需要解密的内容"));