wuyong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

       static void Main(string[] args)
        
{
            SymmetricAlgorithm provider 
= SymmetricAlgorithm.Create("TripleDES");
            provider.Key 
= Encoding.Default.GetBytes("6HD+Ngi8JoGmDog=");
            provider.IV 
= Encoding.Default.GetBytes("gi8joCmd");


            ICryptoTransform encryptor 
= provider.CreateEncryptor();
            
string clearData = "This is test";
            Console.WriteLine(
"Clear Data:" + clearData);
            Stream clearData1 
= new MemoryStream(Encoding.Default.GetBytes(clearData));
            Stream encryptedData 
= new MemoryStream();

            CryptoStream crypoStream 
= new CryptoStream(encryptedData, encryptor, CryptoStreamMode.Write);
            
byte[] buffer = new byte[1024];
            
int count = 0;

            
while ((count = clearData1.Read(buffer, 01024)) > 0)
            
{
                crypoStream.Write(buffer, 
0, count);
            }


            crypoStream.FlushFinalBlock();
            encryptedData.Position 
= 0;
            
byte[] encryptedDataArray = new Byte[encryptedData.Length];
            encryptedData.Read(encryptedDataArray, 
0, encryptedDataArray.Length);
            Console.WriteLine(
"Encrypted data:"+Encoding.Default.GetString(encryptedDataArray ));
            Console.ReadLine();
        }
posted on 2006-09-29 10:16  吴勇  阅读(296)  评论(0编辑  收藏  举报