没有看懂啊,用断点看看哪里出的异常另外看看Stream 是不是有更改长度的属性之类的

解决方案 »

  1.   

    应该是MemoryStream容量不够,造成溢出.
      

  2.   

    http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemIOMemoryStreamClassctorTopic.asp
      

  3.   

    有些加密方法是會限制加密的內容的大小,比如說rsa算法就有它的限制
      

  4.   

    我曾经用AES算法加密过600M得东西啊
      

  5.   

    using System;
    using System.Threading;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;namespace UDPChat
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {

    //private static string Key1="!i~6oxli@]t2K'y$";
    //private static string Iv1="~x7Oq{6+ql@#VI$";
    private static byte[] Iv=new byte[16];
    private static byte[] Key=new byte[16];
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {

    //
    // TODO: 在此处添加代码以启动应用程序
    //
    Stream stringkey=new FileStream("c:\\Networking\\Streams\\MyStreamKey1.txt",FileMode.Open,FileAccess.Read);
    BinaryReader brK=new BinaryReader(stringkey);
    int endOfFileK;
    int m=0;

    do
    {
    endOfFileK=brK.PeekChar();
    if(endOfFileK!=-1)
    {

    //keyOne[m]=Convert.ToByte(brK.ReadUInt16());
    Key[m]=brK.ReadByte();
    Console.WriteLine("Key[{0}]={1}",m,Key[m]);
    m++;
    //Console.WriteLine("m={0}",m);

    }
    }
    while(endOfFileK!=-1);
    Stream stringkey1=new FileStream("c:\\Networking\\Streams\\MyStreamIv1.txt",FileMode.Open,FileAccess.Read);
    BinaryReader brK1=new BinaryReader(stringkey1);
    int endOfFileK1;
    int m1=0;

    do
    {
    endOfFileK1=brK.PeekChar();
    if(endOfFileK1!=-1)
    {

    //keyOne[m]=Convert.ToByte(brK.ReadUInt16());
    Iv[m]=brK1.ReadByte();
    Console.WriteLine("Iv[{0}]={1}",m1,Iv[m1]);
    m1++;
    //Console.WriteLine("m={0}",m);

    }
    }
    while(endOfFileK1!=-1);
    encryptData();
    decryptData();
    } static void encryptData()
    {
    //Create cipher file stream
    FileStream fs=new FileStream("c:\\Networking\\Streams\\MyStreamCipher",FileMode.Create);
    //Create the instance of cipher
    RijndaelManaged RMCrypto=new RijndaelManaged();
    //cipher and output
    CryptoStream cs=new CryptoStream(fs,RMCrypto.CreateEncryptor(Key,Iv),CryptoStreamMode.Write);
    FileStream fsIn=new FileStream("c:\\Networking\\Streams\\诺丁山\\CD1.rmvb",FileMode.Open);
    int datal;
    while((datal=fsIn.ReadByte())!=-1)
    cs.WriteByte((byte)datal);
    //close 
    fsIn.Close();
    cs.Close();
    fs.Close();
    } static void decryptData()
    {
    FileStream fs=new FileStream("c:\\Networking\\Streams\\MyStreamCipher",FileMode.Open);
    RijndaelManaged RMCrypto=new RijndaelManaged();
    CryptoStream cs=new CryptoStream(fs,RMCrypto.CreateDecryptor(Key,Iv),CryptoStreamMode.Read);
    FileStream fsOut=new FileStream("c:\\Networking\\Streams\\MyStreamCipherPlain",FileMode.Create);
    int datal;
    while((datal=cs.ReadByte())!=-1)
    fsOut.WriteByte((byte)datal);
    fsOut.Close();
    cs.Close();
    fs.Close();
    }
    }
    }
      

  6.   

    你使用的
    MemoryStream msOut = new MemoryStream();
    是基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。所以当文件过大超过stream的容量的时候就会异常了。你可以使它变成可以调整大小的流,就没问题了。
      

  7.   

    我将程序进行了改造,如 khpcg(欢乐英雄)那样,用FileStream,而不用MemoryStream,程序能正常运行并能加密大文件(如150Mb).
    但我不太明白为何用MemoryStream就不能成功加密大文件呢???
    请指教阿!!!
      

  8.   

    个人认为: FileStream 使用了磁盘缓存空间,而MemoryStream一直使用内存空间.所以会出现楼主所说那种情况。