没有看懂啊,用断点看看哪里出的异常另外看看Stream 是不是有更改长度的属性之类的
解决方案 »
- 截取字符串问题
- CheckedItems和SelectedItems有什么区别
- 关于数字分离
- C#效果
- 急!winform程序中如何判断某个控件是否获得焦点
- C#在数据库上的应用,想找些这方面的学习资料?!
- 高分求一简单的问题-------如何用回车键模拟TAB键?
- !!!求:MSDN Library for Visual Studio 2005 中文版,能下载,我立马结帖!!!
- 关于读取压缩包的问题
- C#:如何让程序在后台能捕获光驱换盘的操作?
- 如何实现基于Internet的报表管理软件
- 紧急求助:.Net客户端调用 Java写的webservice问题(关于Soap header)!【不惜一切代价】
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();
}
}
}
MemoryStream msOut = new MemoryStream();
是基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。所以当文件过大超过stream的容量的时候就会异常了。你可以使它变成可以调整大小的流,就没问题了。
但我不太明白为何用MemoryStream就不能成功加密大文件呢???
请指教阿!!!