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)
{
}
}
}
}