- //解密,返回解密后的 DateSet,传入的参数是需解密的文件名
- public DataSet EnCrypt(string InName)
- {
- FileStream fsIn = new FileStream(InName,FileMode.Open,FileAccess.Read);
- byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
- byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
- RijndaelManaged RMCrypto = new RijndaelManaged();
- System.Security.Cryptography.CryptoStream cs = new CryptoStream(fsIn,RMCrypto.CreateDecryptor(Key,IV),CryptoStreamMode.Read);
- DataSet myDataSet = new DataSet ();
- System.Xml.XmlTextReader XmlReader ;
- try
- {
- XmlReader = new System.Xml.XmlTextReader (cs);
- myDataSet.ReadXml(XmlReader);
- XmlReader.Close ();
- fsIn.Close();
- }
- catch(System.Exception e)
- {
- }
- return myDataSet;
- }
- //加密,InName表示要加密的文件,OutName为加密后的文件名
- public void Crypt(string InName,string OutName)
- {
- FileStream fsIn = new FileStream(InName,FileMode.Open,FileAccess.Read);
- FileStream fsOut = new FileStream(OutName,FileMode.Create,FileAccess.Write);
- byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
- byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
- RijndaelManaged RMCrypto = new RijndaelManaged();
- System.Security.Cryptography.CryptoStream cs = new CryptoStream(fsOut,RMCrypto.CreateEncryptor(Key,IV),CryptoStreamMode.Write);
- byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
- long rdlen = 0; //This is the total number of bytes written.
- long totlen = fsIn.Length; //This is the total length of the input file.
- int len; //This is the number of bytes to be written at a time.
- while(rdlen < totlen)
- {
- len = fsIn.Read(bin, 0, 100);
- cs.Write(bin, 0, len);
- rdlen = rdlen + len;
- Console.WriteLine("{0} bytes processed", rdlen);
- }
- cs.Close();
- fsOut.Close();
- fsIn.Close();
- }