日期:2014-05-18 浏览次数:20977 次
try { 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(); FileStream file = new FileStream(@"f:\a.txt",FileMode.Open); CryptoStream crypto = new CryptoStream(file,RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Read); StreamReader SReader = new StreamReader(crypto); SReader.Close(); file.Close(); } catch { Console.WriteLine("The Listener Failed."); }
class TestEnDencrypt { static byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; static byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; static void Encrypt(string fileName) { try { RijndaelManaged RMCrypto = new RijndaelManaged(); FileStream file = new FileStream(fileName, FileMode.Open); byte[] data = new byte[file.Length]; file.Read(data, 0, (int)file.Length); file.Close(); MemoryStream ms = new MemoryStream(); CryptoStream crypto = new CryptoStream(ms, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write); using (StreamWriter sw = new StreamWriter(crypto)) { string text = Encoding.Unicode.GetString(data); sw.Write(text); } File.WriteAllBytes(fileName + "_", ms.ToArray()); } catch (Exception ex) { Console.WriteLine(ex.Message); } } static string Dencrypt(string fileName) { string ret = null; try { RijndaelManaged RMCrypto = new RijndaelManaged(); FileStream file = new FileStream(fileName, FileMode.Open); CryptoStream crypto = new CryptoStream(file, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read); StreamReader SReader = new StreamReader(crypto); ret = SReader.ReadToEnd(); SReader.Close(); file.Close(); } catch { Console.WriteLine("The Listener Failed."); } return ret; } }