C# - Rijndael文件加密类

 

using System;
using System.Security.Cryptography;
using System.IO;

 public class Rijndael
 {
  //data members
  private string m_InFileName;
  private string m_OutFileName;
  private byte[] m_Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
           0x07, 0xa8, 0x09, 0x10, 0x11, 0x12, 0x13,
           0x14, 0xde, 0x16 };
  private byte[] m_Vector = { 0x01, 0x02, 0x03, 0x04, 0x0b, 0x54,
           0x07, 0x08, 0x09, 0x10, 0xf1, 0x12, 0x13,
           0x14, 0x15, 0xff };
  //properties
  public string InFileName
  {
   get{return m_InFileName;}
   set{m_InFileName = value;}
  }
  public string OutFileName
  {
   get{return m_OutFileName;}
   set{m_OutFileName = value;}
  }
  public byte[] Key
  {
   get{return m_Key;}
   set{m_Key = value;}
  }
  public byte[] Vector
  {
   get{return m_Vector;}
   set{m_Vector = value;}
  }
  //ctor
  public Rijndael()
  {
   m_InFileName="";
   m_OutFileName="";
  }
  public Rijndael(string infile,string outfile)
  {
   m_InFileName=infile;
   m_OutFileName=outfile;
  }

  //interface methods
  public void Encrypt()
  {
   if (m_InFileName != "" && m_OutFileName != "")
   {
    try
    {
     RijndaelManaged rm;
     rm = new RijndaelManaged();
     FileStream src = new FileStream(m_InFileName, FileMode.Open);
     ICryptoTransform encoder = rm.CreateEncryptor(m_Key, m_Vector); //定义基本的加密转换运算。
     CryptoStream str = new CryptoStream(src, encoder, CryptoStreamMode.Read);
     FileStream outFile = new FileStream(m_OutFileName, FileMode.Create);
     int i = 0;
     while ((i = str.ReadByte()) != -1) //转换为 int 的字节,或者如果从流的末尾读取则为 -1。
     {
      outFile.WriteByte((byte)i);
     }
     src.Close();
     outFile.Close();
    }
    catch (Exception)
    {
    }
   }
  }

  public void Decrypt()
  {
   if (m_InFileName != "" && m_OutFileName != "")
   {
    try
    {
     RijndaelManaged rm;
     rm = new RijndaelManaged();
     FileStream src = new FileStream(m_InFileName, FileMode.Open);
     ICryptoTransform decoder = rm.CreateDecryptor(m_Key, m_Vector);
     CryptoStream str = new CryptoStream(src, decoder, CryptoStreamMode.Read);
     FileStream outFile = new FileStream(m_OutFileName, FileMode.Create);
     int i = 0;
     while ((i = str.ReadByte()) != -1)
     {
      outFile.WriteByte((byte)i);
     }
     src.Close();
     outFile.Close();
    }
    catch (Exception)
    {
    }
   }
  }

 }


 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值