问题描述
namespaceMyCryptoHelp{///<summary>///异常处理类///</summary>publicclassCryptoHelpException:ApplicationException{publicCryptoHelpException(stringmsg):base(msg){}}///<summary>///CryptHelp///</summary>publicclassCryptoHelp{privateconstulongFC_TAG=0xFC010203040506CF;privateconstintBUFFER_SIZE=128*1024;///<summary>///检验两个Byte数组是否相同///</summary>///<paramname="b1">Byte数组</param>///<paramname="b2">Byte数组</param>///<returns>true-相等</returns>privatestaticboolCheckByteArrays(byte[]b1,byte[]b2){if(b1.Length==b2.Length){for(inti=0;i<b1.Length;++i){if(b1!=b2)returnfalse;}returntrue;}returnfalse;}///<summary>///创建RijndaelSymmetricAlgorithm///</summary>///<paramname="password">密码</param>///<paramname="salt"></param>///<returns>加密对象</returns>privatestaticSymmetricAlgorithmCreateRijndael(stringpassword,byte[]salt){PasswordDeriveBytespdb=newPasswordDeriveBytes(password,salt,"SHA256",1000);SymmetricAlgorithmsma=Rijndael.Create();sma.KeySize=256;sma.Key=pdb.GetBytes(32);sma.Padding=PaddingMode.PKCS7;returnsma;}///<summary>///加密文件随机数生成///</summary>privatestaticRandomNumberGeneratorrand=newRNGCryptoServiceProvider();///<summary>///生成指定长度的随机Byte数组///</summary>///<paramname="count">Byte数组长度</param>///<returns>随机Byte数组</returns>privatestaticbyte[]GenerateRandomBytes(intcount){byte[]bytes=newbyte[count];rand.GetBytes(bytes);returnbytes;}///<summary>///加密文件///</summary>///<paramname="inFile">待加密文件</param>///<paramname="outFile">加密后输入文件</param>///<paramname="password">加密密码</param>publicstaticvoidEncryptFile(stringinFile,stringoutFile,stringpassword){using(FileStreamfin=File.OpenRead(inFile),fout=File.OpenWrite(outFile)){longlSize=fin.Length;//输入文件长度intsize=(int)lSize;byte[]bytes=newbyte[BUFFER_SIZE];//缓存intread=-1;//输入文件读取数量intvalue=0;//获取IV和saltbyte[]IV=GenerateRandomBytes(16);byte[]salt=GenerateRandomBytes(16);//创建加密对象SymmetricAlgorithmsma=CryptoHelp.CreateRijndael(password,salt);sma.IV=IV;//在输出文件开始部分写入IV和saltfout.Write(IV,0,IV.Length);fout.Write(salt,0,salt.Length);//创建散列加密HashAlgorithmhasher=SHA256.Create();using(CryptoStreamcout=newCryptoStream(fout,sma.CreateEncryptor(),CryptoStreamMode.Write),chash=newCryptoStream(Stream.Null,hasher,CryptoStreamMode.Write)){BinaryWriterbw=newBinaryWriter(cout);bw.Write(lSize);bw.Write(FC_TAG);//读写字节块到加密流缓冲区while((read=fin.Read(bytes,0,bytes.Length))!=0){cout.Write(bytes,0,read);chash.Write(bytes,0,read);value+=read;}//关闭加密流chash.Flush();chash.Close();//读取散列byte[]hash=hasher.Hash;//输入文件写入散列cout.Write(hash,0,hash.Length);//关闭文件流cout.Flush();cout.Close();}}}///<summary>///解密文件///</summary>///<paramname="inFile">待解密文件</param>///<paramname="outFile">解密后输出文件</param>///<paramname="password">解密密码</param>publicstaticvoidDecryptFile(stringinFile,stringoutFile,stringpassword){//创建打开文件流using(FileStreamfin=File.OpenRead(inFile),fout=File.OpenWrite(outFile)){intsize=(int)fin.Length;byte[]bytes=newbyte[BUFFER_SIZE];intread=-1;intvalue=0;intoutValue=0;byte[]IV=newbyte[16];fin.Read(IV,0,16);byte[]salt=newbyte[16];fin.Read(salt,0,16);SymmetricAlgorithmsma=CryptoHelp.CreateRijndael(password,salt);sma.IV=IV;value=32;longlSize=-1;//创建散列对象,校验文件HashAlgorithmhasher=SHA256.Create();using(CryptoStreamcin=newCryptoStream(fin,sma.CreateDecryptor(),CryptoStreamMode.Read),chash=newCryptoStream(Stream.Null,hasher,CryptoStreamMode.Write)){//读取文件长度BinaryReaderbr=newBinaryReader(cin);lSize=br.ReadInt64();ulongtag=br.ReadUInt64();if(FC_TAG!=tag)thrownewCryptoHelpException("文件被破坏");longnumReads=lSize/BUFFER_SIZE;longslack=(long)lSize%BUFFER_SIZE;for(inti=0;i<numReads;++i){read=cin.Read(bytes,0,bytes.Length);fout.Write(bytes,0,read);chash.Write(bytes,0,read);value+=read;outValue+=read;}if(slack>0){read=cin.Read(bytes,0,(int)slack);fout.Write(bytes,0,read);chash.Write(bytes,0,read);value+=read;outValue+=read;}chash.Flush();chash.Close();fout.Flush();fout.Close();byte[]curHash=hasher.Hash;//获取比较和旧的散列对象byte[]oldHash=newbyte[hasher.HashSize/8];read=cin.Read(oldHash,0,oldHash.Length);if((oldHash.Length!=read)||(!CheckByteArrays(oldHash,curHash)))thrownewCryptoHelpException("文件被破坏");}if(outValue!=lSize)thrownewCryptoHelpException("文件大小不匹配");}}}}//调用publicclassTestClass{stringmyPassword="TEST_PASSWORD_~!@#";stringmyPlainFile="test.txt";stringmyEncryptedFile="test.encrypted";stringmyDecryptedFile="test.decrypted";[STAThread]staticvoidMain(){CryptoHelp.EncryptFile(myPlainFile,myEncryptedFile,myPassword);CryptoHelp.DecryptFile(myEncryptedFile,myDecryptedFile,myPassword);}}谢谢了,写好帮忙发我信箱:hyuesf@qq.com
解决方案
解决方案二:
要实现什么?
解决方案三:
是解密文本用的程序
解决方案四:
...这么长,程序有错误还是怎么的?
解决方案五:
什么呀