我的加密类实现方法
class DesSecurity 
    {
        const string KEY_64 = "Security";
        const string IV_64 = "Security";
        DESCryptoServiceProvider DesProvider = new DESCryptoServiceProvider();        public Stream Encrypt(Stream inputStream)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cst = new CryptoStream(memoryStream, DesProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
            byte[] buffer = new byte[inputStream.Length];
            inputStream.Read(buffer, 0, buffer.Length);
            cst.Write(buffer, 0, buffer.Length);
            cst.Flush();
            return memoryStream;
        }        public Stream Decrypt(Stream inputStream)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cst = new CryptoStream(memoryStream, DesProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);
            byte[] buffer = new byte[inputStream.Length];
            inputStream.Read(buffer, 0, buffer.Length);
            cst.Write(buffer, 0, buffer.Length);
            cst.Flush();
            return memoryStream;
        }
现在是我加密后的流无法解密原来的流,快晕了,请教高人啊 ~~~~

解决方案 »

  1.   

    把cst.Flush()换成cst.FlushFinalBlock()试试
      

  2.   

    public Stream Decrypt(Stream inputStream)
            {
                byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
                byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
                MemoryStream memoryStream = new MemoryStream();
                //以下作了更改
                CryptoStream cst = new CryptoStream(inputStream, 
                      DesProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                byte[] buffer = new byte[inputStream.Length];
                inputStream.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, buffer.Length);
                return memoryStream;
            }
      

  3.   

    一个 des加密类public class Des
    {
    public Des()
    {
    // 
    // TODO: 在此处添加构造函数逻辑
    //
    } /// <summary>
    /// 加密。注意:sKey输入密码的时候,必须使用英文字符,区分大小写,且字符数量是8个,不能多也不能少,否则出错。
    /// </summary>
    public static string Encrypt(string pToEncrypt, string sKey)
    {
    DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();  
    //把字符串放到byte数组中  
    //原来使用的UTF8编码,我改成Unicode编码了,不行  
    byte[]  inputByteArray  =  Encoding.Default.GetBytes(pToEncrypt);  
    //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);  
     
    //建立加密对象的密钥和偏移量  
    //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
    //使得输入密码必须输入英文文本  
    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);  
    //Write  the  byte  array  into  the  crypto  stream  
    //(It  will  end  up  in  the  memory  stream)  
    cs.Write(inputByteArray,  0,  inputByteArray.Length);  
    cs.FlushFinalBlock();  
    //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
    StringBuilder  ret  =  new  StringBuilder();  
    foreach(byte  b  in  ms.ToArray())  
    {  
    //Format  as  hex  
    ret.AppendFormat("{0:X2}",  b);  
    }  
    return  ret.ToString();  
    } /// <summary>
    /// 解密。
    /// </summary>
    public static string Decrypt(string pToDecrypt, string sKey)
    {
    DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();  
     
    //Put  the  input  string  into  the  byte  array  
    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);  
    //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
    cs.Write(inputByteArray,  0,  inputByteArray.Length);  
    cs.FlushFinalBlock();  
     
    //Get  the  decrypted  data  back  from  the  memory  stream  
    //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
    StringBuilder  ret  =  new  StringBuilder();  
                 
    return  System.Text.Encoding.Default.GetString(ms.ToArray());  
    } }//更多 http://blog.csdn.net/hertcloud/archive/2007/03/02/1518960.aspx
      

  4.   

    目前测试的结果好像是之前我的流在转byte数组的时候,没有把Position置0,正在修改中
      

  5.   

    public Stream Decrypt(Stream cipherStream)
    {
        Stream plainStream = null;    if (cipherStream != null && cipherStream.Length > 0)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);        //把加密流读出字节数组
            byte[] encrypBytes = new byte[cipherStream.Length];
            cipherStream.Position = 0;
            cipherStream.Read(encrypBytes, 0, encrypBytes.Length);        //把内存流对象包装成解密流对象
            plainStream = new MemoryStream();
            CryptoStream cs = new CryptoStream(plainStream, 
                     DesProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);        cs.Write(encrypBytes, 0, encrypBytes.Length);
            cs.FlushFinalBlock();
            plainStream.Position = 0;
        }    return plainStream;
    }
      

  6.   

    似乎只能加密解密文本吧,
    如果是二进制数据,可以转为Base64字符串再加密
    Convert.FromBase64String
    Convert.ToBase64String
      

  7.   

    这个是我现在一直在用的,你自己看看吧!        /// <summary>
            /// 使用DES加密
            /// </summary>
            /// <param name="OriginalValue">待加密的字符串</param>
            /// <param name="Key">密钥(最大长度8)</param>
            /// <param name="IV">初始化向量(最大长度8)</param>
            /// <returns>加密后的字符串</returns>
            public string DESEncrypt(string OriginalValue, string Key, string IV)
            {
                //将key和IV处理成8个字符
                Key += "12345678";
                IV += "12345678";
                Key = Key.Substring(0, 8);
                IV = IV.Substring(0, 8);            SymmetricAlgorithm sa;
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;            sa = new DESCryptoServiceProvider();
                sa.Key = Encoding.UTF8.GetBytes(Key);
                sa.IV = Encoding.UTF8.GetBytes(IV);
                ct = sa.CreateEncryptor();            byt = Encoding.UTF8.GetBytes(OriginalValue);            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());        }        public string DESEncrypt(string OriginalValue, string Key)
            {
                return DESEncrypt(OriginalValue, Key, Key);
            }        /// <summary>
            /// 使用DES解密
            /// </summary>
            /// <param name="EncryptedValue">待解密的字符串</param>
            /// <param name="Key">密钥(最大长度8)</param>
            /// <param name="IV">m初始化向量(最大长度8)</param>
            /// <returns>解密后的字符串</returns>
            public string DESDecrypt(string EncryptedValue, string Key, string IV)
            {
                //将key和IV处理成8个字符
                Key += "12345678";
                IV += "12345678";
                Key = Key.Substring(0, 8);
                IV = IV.Substring(0, 8);            SymmetricAlgorithm sa;
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;            sa = new DESCryptoServiceProvider();
                sa.Key = Encoding.UTF8.GetBytes(Key);
                sa.IV = Encoding.UTF8.GetBytes(IV);
                ct = sa.CreateDecryptor();            byt = Convert.FromBase64String(EncryptedValue);            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());        }        public string DESDecrypt(string EncryptedValue, string Key)
            {
                return DESDecrypt(EncryptedValue, Key, Key);
            }
      

  8.   

    public static string DecriptPassword(string key, string iv, string password)
    {
    TripleDES triDES = new TripleDESCryptoServiceProvider();
    using (MemoryStream ms = new MemoryStream())
    {
    byte[] bs = Convert.FromBase64String(password);
    ms.Write(bs, 0, bs.Length);
    ms.Seek(0, SeekOrigin.Begin);
    CryptoStream cs = new CryptoStream(ms, triDES.CreateDecryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv)), CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cs);
    string clear = sr.ReadToEnd();
    sr.Close();
    cs.Close();
    return clear;
    }
    }