CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

找来的一个文件加密/解密算法,为什么解密出来的文件少了几个字节?

楼主kpken()2004-09-03 22:27:50 在 .NET技术 / C# 提问

public   static   void   test()  
  {      
                       
  FileStream   fin=new   FileStream("input.txt",FileMode.Open,FileAccess.Read);  
  FileStream   fout=new   FileStream("output.txt",FileMode.OpenOrCreate,FileAccess.Write);  
                           
  //创建加密类  
   
  DES   des   =   new   DESCryptoServiceProvider();  
  des.GenerateKey();     //获取随机产生的Key,和   IV  
  des.GenerateIV();  
  byte[]   keys=des.Key;  
  byte[]   ivs=des.IV;  
                           
  long   leng=fin.Length;   //获取文件的长度  
  long   temp=0;     //临时变量  
  byte[]   buffer=new   byte[400];     //用于存储文件内容于缓存  
   
  CryptoStream   cStream=new   CryptoStream(fout,des.CreateEncryptor(keys,ivs),CryptoStreamMode.Write);   //创建加密流并写入到"output.txt"文件  
  while(temp<leng)      
  {  
  int   wLeng=fin.Read(buffer,0,400);   //写入缓冲  
  cStream.Write(buffer,0,wLeng);   //写入文件  
  temp=temp+wLeng;  
  Console.WriteLine("加密成功");  
   
  Console.ReadLine();  
  }  
  cStream.Close();  
                           
  fout.Close();  
  fin.Close();  
   
  FileStream   fin1=new   FileStream("output.txt",FileMode.Open,FileAccess.Read);  
  FileStream   fout1=new   FileStream("解密.txt",FileMode.OpenOrCreate,FileAccess.Write);  
                           
                             
   
  //DES   des1   =   new   DESCryptoServiceProvider();  
  //des1.GenerateKey();     //获取随机产生的Key,和   IV  
  //des1.GenerateIV();  
  //byte[]   keys1=des1.Key;  
  //byte[]   ivs1=des1.IV;  
                           
  long   leng1=fin1.Length;   //获取文件的长度  
  long   temp1=0;     //临时变量  
  byte[]   buffer1=new   byte[400];     //用于存储文件内容于缓存  
   
  CryptoStream   cStream1=new   CryptoStream(fout1,des.CreateDecryptor(keys,ivs),CryptoStreamMode.Write);   //创建加密流并写入到"output.txt"文件  
  while(temp1<leng1)      
  {  
  int   wLeng1=fin1.Read(buffer1,0,400);   //写入缓冲  
  cStream1.Write(buffer1,0,wLeng1);   //写入文件  
  temp1=temp1+wLeng1;  
  Console.WriteLine("解密成功");  
   
  Console.ReadLine();  
  }  
  cStream.Close();  
                           
  fout.Close();  
  fin.Close();  
   
  }  
  问题点数:50、回复次数:12Top

1 楼bingzhihan(冰之寒)回复于 2004-09-04 00:22:18 得分 10

会不会加密的太长,要睡觉了,实在看不下去了:)帮你顶吧Top

2 楼kpken()回复于 2004-09-04 10:00:19 得分 0

加密1M的xml文件,少了大概4个字节吧,加密10多个byte的文件也少了4个字节。Top

3 楼coollzh(良子)回复于 2004-09-04 11:14:12 得分 10

看看有没有编码的问题Top

4 楼kpken()回复于 2004-09-04 12:30:11 得分 0

我看过另外一个算法,中间用到了Encode,不过不是很懂Top

5 楼kpken()回复于 2004-09-04 20:21:31 得分 0

upTop

6 楼happyno7(夕丁)回复于 2004-09-04 22:10:24 得分 10

会不会是少了结束符?  
  看上去代码没有问题  
   
  Top

7 楼kpken()回复于 2004-09-04 23:48:44 得分 0

大家可以试试代码Top

8 楼kpken()回复于 2004-09-06 10:29:07 得分 0

大家还有什么好算法介绍一下Top

9 楼khpcg(欢乐英雄)回复于 2004-09-06 14:51:47 得分 10

using     System.IO;      
  using     System.Text;  
   
  namespace   WebADESCSDN  
  {  
  ///   <summary>  
  ///   DESCSDN2   的摘要说明。  
  ///   </summary>  
  public   class   DESCSDN2  
  {  
  public   DESCSDN2()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
  //加密字符串  
  public   static   string   DesEncrypt(string   strText)    
  {  
  try    
  {    
  byte[]   byKey   =new   byte[8]{0x12,   0x34,   0x56,   0x78,   0x90,   0xAB,   0xCD,   0xEF};    
  byte[]   IV   =new   byte[8]{0x23,   0x45,   0x67,   0x89,   0x0A,   0xBC,   0xDE,   0xF1};    
   
  DESCryptoServiceProvider   des   =   new   DESCryptoServiceProvider();    
  byte[]   inputByteArray   =   System.Text.Encoding.UTF8.GetBytes(strText);    
  MemoryStream   ms   =   new   MemoryStream();    
  CryptoStream   cs   =   new   CryptoStream(ms,   des.CreateEncryptor(byKey,   IV),   CryptoStreamMode.Write)   ;    
  cs.Write(inputByteArray,   0,   inputByteArray.Length);    
  cs.FlushFinalBlock();    
  return   Convert.ToBase64String(ms.ToArray());    
  }    
  catch(System.Exception   error)    
  {    
  return   "error:"   +error.Message+"\r";    
  }    
  }  
     
  public   static   string   DesDecrypt(string   strText)    
  {    
  try    
  {    
  byte[]   byKey   =new   byte[]   {0x12,   0x34,   0x56,   0x78,   0x90,   0xAB,   0xCD,   0xEF};    
  byte[]   IV   =new   byte[]{0x23,   0x45,   0x67,   0x89,   0x0A,   0xBC,   0xDE,   0xF1};    
   
  byte[]   inputByteArray   =   new   Byte[strText.Length];    
  DESCryptoServiceProvider   des   =   new   DESCryptoServiceProvider();    
  inputByteArray   =   Convert.FromBase64String(strText);    
  MemoryStream   ms   =   new   MemoryStream();    
  CryptoStream   cs   =   new   CryptoStream(ms,   des.CreateDecryptor(byKey,   IV),   CryptoStreamMode.Write);    
  cs.Write(inputByteArray,   0,   inputByteArray.Length);    
  cs.FlushFinalBlock();    
  System.Text.Encoding   encoding   =   new   System.Text.UTF8Encoding();    
  return   encoding.GetString(ms.ToArray());    
  }    
  catch(System.Exception   error)    
  {    
  return   "error:"+error.Message+"\r";    
  }    
  }  
   
  }  
  }  
  Top

10 楼khpcg(欢乐英雄)回复于 2004-09-06 14:53:25 得分 10

using   System;  
  using   System.Security.Cryptography;  
  using   System.Text;  
   
  class   RSACSPSample  
  {  
   
  static   void   Main()  
  {  
  try  
  {  
  //Create   a   UnicodeEncoder   to   convert   between   byte   array   and   string.  
  UnicodeEncoding   ByteConverter   =   new   UnicodeEncoding();  
   
  //Create   byte   arrays   to   hold   original,   encrypted,   and   decrypted   data.  
  byte[]   dataToEncrypt   =   ByteConverter.GetBytes("Data   to   中国人   Encrypt");  
  byte[]   encryptedData;  
  byte[]   decryptedData;  
                           
  //Create   a   new   instance   of   RSACryptoServiceProvider   to   generate  
  //public   and   private   key   data.  
  RSACryptoServiceProvider   RSA   =   new   RSACryptoServiceProvider();  
   
  //Pass   the   data   to   ENCRYPT,   the   public   key   information    
  //(using   RSACryptoServiceProvider.ExportParameters(false),  
  //and   a   boolean   flag   specifying   no   OAEP   padding.  
  encryptedData   =   RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false),   false);  
   
  //Pass   the   data   to   DECRYPT,   the   private   key   information    
  //(using   RSACryptoServiceProvider.ExportParameters(true),  
  //and   a   boolean   flag   specifying   no   OAEP   padding.  
  decryptedData   =   RSADecrypt(encryptedData,RSA.ExportParameters(true),   false);  
   
  //Display   the   decrypted   plaintext   to   the   console.    
  for(long   i=0;i<5000000;i++)  
  {  
  Console.WriteLine("Decrypted   plaintext:   {0}",   ByteConverter.GetString(decryptedData));  
  }  
   
  }  
  catch(ArgumentNullException)  
  {  
  //Catch   this   exception   in   case   the   encryption   did  
  //not   succeed.  
  Console.WriteLine("Encryption   failed.");  
   
  }  
   
   
  }  
   
  static   public   byte[]   RSAEncrypt(byte[]   DataToEncrypt,   RSAParameters   RSAKeyInfo,   bool   DoOAEPPadding)  
  {  
  try  
  {          
  //Create   a   new   instance   of   RSACryptoServiceProvider.  
  RSACryptoServiceProvider   RSA   =   new   RSACryptoServiceProvider();  
   
  //Import   the   RSA   Key   information.   This   only   needs  
  //toinclude   the   public   key   information.  
  RSA.ImportParameters(RSAKeyInfo);  
   
  //Encrypt   the   passed   byte   array   and   specify   OAEP   padding.      
  //OAEP   padding   is   only   available   on   Microsoft   Windows   XP   or  
  //later.      
  return   RSA.Encrypt(DataToEncrypt,   DoOAEPPadding);  
  }  
  //Catch   and   display   a   CryptographicException      
  //to   the   console.  
  catch(CryptographicException   e)  
  {  
  Console.WriteLine(e.Message);  
   
  return   null;  
  }  
   
  }  
   
  static   public   byte[]   RSADecrypt(byte[]   DataToDecrypt,   RSAParameters   RSAKeyInfo,bool   DoOAEPPadding)  
  {  
  try  
  {  
  //Create   a   new   instance   of   RSACryptoServiceProvider.  
  RSACryptoServiceProvider   RSA   =   new   RSACryptoServiceProvider();  
   
  //Import   the   RSA   Key   information.   This   needs  
  //to   include   the   private   key   information.  
  RSA.ImportParameters(RSAKeyInfo);  
   
  //Decrypt   the   passed   byte   array   and   specify   OAEP   padding.      
  //OAEP   padding   is   only   available   on   Microsoft   Windows   XP   or  
  //later.      
  return   RSA.Decrypt(DataToDecrypt,   DoOAEPPadding);  
  }  
  //Catch   and   display   a   CryptographicException      
  //to   the   console.  
  catch(CryptographicException   e)  
  {  
  Console.WriteLine(e.ToString());  
   
  return   null;  
  }  
   
   
  }  
   
   
  }  
   
   
  /*try  
  {  
          //Create   a   new   RSACryptoServiceProvider   object.  
  RSACryptoServiceProvider   RSA   =   new   RSACryptoServiceProvider();  
   
   
          //Export   the   key   information   to   an   RSAParameters   object.  
          //Pass   false   to   export   the   public   key   information   or   pass  
          //true   to   export   public   and   private   key   information.  
  RSAParameters   RSAParams   =   RSA.ExportParameters(false);  
   
   
  }  
  catch(CryptographicException   e)  
  {  
          //Catch   this   exception   in   case   the   encryption   did  
          //not   succeed.  
  Console.WriteLine(e.Message);  
   
  } */  
  Top

11 楼kpken()回复于 2004-09-06 21:01:28 得分 0

你这个算法能够适应比较大(10M以内)的文本文件吗?Top

12 楼kpken()回复于 2004-09-06 23:54:21 得分 0

换了一个算法,可以了。  
  public   static   void   EncryptFile(string   sInputFilename,string   sOutputFilename,   string   sKey)    
  {  
  FileStream   fsInput   =   new   FileStream(sInputFilename,    
  FileMode.Open,    
  FileAccess.Read);  
   
  FileStream   fsEncrypted   =   new   FileStream(sOutputFilename,    
  FileMode.Create,    
  FileAccess.Write);  
  DESCryptoServiceProvider   DES   =   new   DESCryptoServiceProvider();  
  DES.Key   =   ASCIIEncoding.ASCII.GetBytes(sKey);  
  DES.IV   =   ASCIIEncoding.ASCII.GetBytes(sKey);  
  ICryptoTransform   desencrypt   =   DES.CreateEncryptor();  
  CryptoStream   cryptostream   =   new   CryptoStream(fsEncrypted,    
  desencrypt,    
  CryptoStreamMode.Write);    
   
  byte[]   bytearrayinput   =   new   byte[fsInput.Length];  
  fsInput.Read(bytearrayinput,   0,   bytearrayinput.Length);  
  cryptostream.Write(bytearrayinput,   0,   bytearrayinput.Length);  
  cryptostream.Close();  
  fsInput.Close();  
  fsEncrypted.Close();  
  }  
  public   static   void   DecryptFile(string   sInputFilename,   string   sOutputFilename,string   sKey)  
  {  
  DESCryptoServiceProvider   DES   =   new   DESCryptoServiceProvider();  
  //A   64   bit   key   and   IV   is   required   for   this   provider.  
  //Set   secret   key   For   DES   algorithm.  
  DES.Key   =   ASCIIEncoding.ASCII.GetBytes(sKey);  
  //Set   initialization   vector.  
  DES.IV   =   ASCIIEncoding.ASCII.GetBytes(sKey);  
   
  //Create   a   file   stream   to   read   the   encrypted   file   back.  
  FileStream   fsread   =   new   FileStream(sInputFilename,    
  FileMode.Open,    
  FileAccess.Read);  
  //Create   a   DES   decryptor   from   the   DES   instance.  
  ICryptoTransform   desdecrypt   =   DES.CreateDecryptor();  
  //Create   crypto   stream   set   to   read   and   do   a    
  //DES   decryption   transform   on   incoming   bytes.  
  CryptoStream   cryptostreamDecr   =   new   CryptoStream(fsread,    
  desdecrypt,  
  CryptoStreamMode.Read);  
  //Print   the   contents   of   the   decrypted   file.  
  StreamWriter   fsDecrypted   =   new   StreamWriter(sOutputFilename);  
  fsDecrypted.Write(new   StreamReader(cryptostreamDecr).ReadToEnd());  
  fsDecrypted.Flush();  
  fsDecrypted.Close();  
  }  
  Top

相关问题

  • 求加密解密算法
  • 谁有RSA加密及解密算法
  • 加密和解密:md5算法
  • 有关加密解密算法!
  • 求一个加密算法,能加密也能解密,谢谢!
  • 100分求助RSA加密和解密算法
  • 有谁懂DES加密/解密算法原理?
  • 寻求有关加密解密算法RC5的资料,急用!!!
  • 200分 求java 加密 VC 解密的算法程序
  • 寻求一种数据库的加密解密算法

关键词

  • 文件
  • 加密
  • descsdn2
  • iv
  • cryptostream
  • bykey
  • inputbytearray
  • descryptoserviceprovider
  • des
  • filestream

得分解答快速导航

  • 帖主:kpken
  • bingzhihan
  • coollzh
  • happyno7
  • khpcg
  • khpcg

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo