using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{    public class EncryptionForMe
    {
        private RijndaelManaged rmCrypto = new RijndaelManaged();        private byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 
                              ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };        private byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 
                             ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };        public string Encrypt(string strSource)
        {
            //这里加密的都没问题,问题都在解密那方法里面            byte[] byteSource = Encoding.Unicode.GetBytes(strSource);            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);            //CryptoStream.Write方法把byteSource加密写进ms里.
            cs.Write(byteSource, 0, byteSource.Length);            string temp = Encoding.Unicode.GetString(ms.GetBuffer(), 0, (int)ms.Length);            cs.Close();            return temp;
        }        public string Decrypt(string strSource)
        {            byte[] tempBtye = Encoding.Unicode.GetBytes(strSource);            MemoryStream ms = new MemoryStream(tempBtye);
            
            //MSDN: public CryptoStream(Stream stream,ICryptoTransform transform,CryptoStreamMode mode)
            CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);            //CryptoStream.Read()这个方法怎么用的?
            //MSDN: public override int Read(byte[] buffer,int offset,int count)            //看说明是把 ms 解压后得到的byte[] 写进 thisByte 那里 可我都不可能知道ms 解压后的实际长度啊?我该怎么才能让它Read进来
            byte[] thisByte = new byte[ms.Length];   //  <<这试了用byte[] thisByte = new byte[9999];也不行
            cs.Read(thisByte, 0, (int)ms.Length);  //  <<这老报错误 信息是: 填充无效,无法被移除。
            cs.Close();            //VS版本:Team System 2008
            //.NET版本:2.0            string temp = Encoding.Unicode.GetString(thisByte);            return temp;
        }    }    class Program
    {
        static void Main(string[] args)
        {
            EncryptionForMe thisEncryption = new EncryptionForMe();
            string tempStr = thisEncryption.Encrypt("我要被加密了!呜呜呜");
            Console.WriteLine(tempStr);
            Console.WriteLine(thisEncryption.Decrypt(tempStr));
        }
    }
}

解决方案 »

  1.   

    你的密钥和向量怎么不是随机生成而是固定的啊 
      我给你发个加密的类  DES加密
                    //生成随机的密钥和向量
                    TDCSP.GenerateKey();
                    TDCSP.GenerateIV();                //在当前的文本框中显示密钥与向量
                    this.txtKey.Text = Convert.ToBase64String(TDCSP.Key);
                    this.txtIV.Text = Convert.ToBase64String(TDCSP.IV);
                    //将密钥与向量赋值给全局变量以储存
                    this.Key = this.txtKey.Text;
                    this.IV = this.txtIV.Text;
                    //使用某种编码方式格式化明文
                    byte[] tempArray = ASCIIEncoding.ASCII.GetBytes(this.txtMsg1.Text.Trim());//UnicodeEncoding.Unicode.GetBytes(this.txtMsg1.Text.Trim());
                    
                    //调用3DES加密算法类对象的方法将格式化后的明文数组进行加密
                    byte[] tempArray2 = TDCSP.CreateEncryptor().TransformFinalBlock(tempArray, 0, tempArray.Length);
                    //将加密后的数组输出显示
                    this.txtCphiMsg.Text = Convert.ToBase64String(tempArray2);
                }
                else
                {
                    //弹出提示信息
                    MessageBox.Show("您还没有输入需要加密的明文");
                    //聚焦点至明文输入框
                    this.txtMsg1.Focus();
                    return;
                }
      

  2.   

    问题在于没有冲马桶//CryptoStream.Write方法把byteSource加密写进ms里.
                cs.Write(byteSource, 0, byteSource.Length);// 注意加上这句:
                cs.FlushFinalBlock(); // 输出缓冲区中剩余的数据
      

  3.   

    ls,我用sharpdevelop测试,cs.FlushFinalBlock();
    一样会出错,Index was outside the bounds of the array.
    读一位出来都是这个错误,等清醒点再搞
      

  4.   

    http://blog.csdn.net/acefly/archive/2008/03/14/2183637.aspx看看第34
      

  5.   

    看了楼上方式,还是出错,奇怪了,请帮看看哪出错了
    private RijndaelManaged rmCrypto = new RijndaelManaged();
    private Encoding TxtEncode= Encoding.UTF8;
    private byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 
                          ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
    private byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 
                         ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
    public string Encrypt(string strSource)
    {
        byte[] byteSource = TxtEncode.GetBytes(strSource);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
        cs.Write(byteSource, 0, byteSource.Length);
        cs.FlushFinalBlock();
        string temp = Convert.ToBase64String(ms.ToArray());
        return temp;
    }
    public string Decrypt(string strSource)
    {
        byte[] tempBuf = Convert.FromBase64String(strSource);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(rmCrypto.Key, rmCrypto.IV), CryptoStreamMode.Write);
        cs.Write(tempBuf, 0, tempBuf.Length);
        cs.FlushFinalBlock();//Error,Padding is invalid and cannot be removed
        StreamReader sr = new StreamReader(ms, TxtEncode);
        return sr.ReadToEnd();
    }
      

  6.   

    楼主试试:
        public string Encrypt(string strSource)
        {
            RijndaelManaged rmCrypto = new RijndaelManaged();
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            StreamWriter wr = new StreamWriter(cs);
            wr.Write(strSource);
            wr.Close();
            string temp = Convert.ToBase64String(ms.ToArray());
            return temp;
        }
        public string Decrypt(string strSource)
        {
            byte[] tempBuf = Convert.FromBase64String(strSource);
            MemoryStream ms = new MemoryStream(tempBuf);
            CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            string ret = sr.ReadToEnd();
            sr.Close();        return ret;
        }
      

  7.   

    把 CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(rmCrypto.Key, rmCrypto.IV), CryptoStreamMode.Write);
    中的rmCrypto.Key, rmCrypto.IV改为key,IV。   public string Decrypt(string strSource)
            {
                byte[] tempBuf = Convert.FromBase64String(strSource);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, rmCrypto.CreateDecryptor(key,IV), CryptoStreamMode.Write);
                cs.Write(tempBuf, 0, tempBuf.Length);
                cs.FlushFinalBlock();//Error,Padding is invalid and cannot be removed
                return Encoding.UTF8.GetString(ms.ToArray());
            }
      

  8.   

    解密: 
    public string Decode(string data)
            {
                if (data.Length <= 0)
                {
                    return "";
                }
                byte[] byEnc = Convert.FromBase64String(data);
                
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
               CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(key,IV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();

            }
      

  9.   

    return sr.ReadToEnd();我去百度到的人家封好的类就是使用这句但是解出来会伴随大小不等的'\0'所以我都用.IndexOf()处理但还是搞不懂CryptoStream.Read()怎么用的谢谢各位热心回答在此非常感谢CSDN这个平台
      

  10.   

    cs.FlushFinalBlock();//这句清了一下缓存,有无都不影响结果我测试过的,当执行cs.Close()时也会清理掉的上面说错了
    我是用Substring() + IndexOf()处理掉'\0'的问题现在基本没问题了!