EnCrypto / DeCrypto




//
//read orignal text
            TextReader tr = new StreamReader(File.OpenRead("TextFile1.txt"));
            string result = tr.ReadToEnd();
            tr.Close();

            byte[] orignalTextByte = Encoding.Default.GetBytes(result);

            //crypto alogrithm
            SymmetricAlgorithm sa = DES.Create();


            //encrypto
            MemoryStream encryptoMs = new MemoryStream();
            CryptoStream encryptoStream = new CryptoStream(encryptoMs, 
                sa.CreateEncryptor(), CryptoStreamMode.Write);
            encryptoStream.Write(orignalTextByte, 0, orignalTextByte.Length);
            encryptoStream.Close();
            byte[] encByte = encryptoMs.ToArray();
            encryptoMs.Close();

            Console.WriteLine("encryto result : " + 
                Environment.NewLine +Encoding.Default.GetString(encByte));

            /*
            TextWriter tw = new StreamWriter(File.OpenWrite("TextFile1.txt"));
            tw.Write(Encoding.Default.GetString(cseByte));
            tw.Flush();
            tw.Close();
            */

            //decrypto
            MemoryStream decryptoMs = new MemoryStream(encByte);
            CryptoStream dcryptoStream = new CryptoStream(decryptoMs, 
                sa.CreateDecryptor(), CryptoStreamMode.Read);
            byte[] deseByte = new byte[encByte.Length];
            dcryptoStream.Read(deseByte, 0, deseByte.Length);
            dcryptoStream.Close();
            decryptoMs.Close();
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("decrypto result " +Environment.NewLine +
                Encoding.Default.GetString(deseByte));
        }

Yamecoder 야매코더_
C# 2012.03.09 13:14
Powerd by Tistory, designed by criuce
rss