Using system;using system.io;using system.text;using system.security.cryptography;namespace Rc2cryptoserviceprovider_examples{class MyMainClass1 {public static void Main () {//Crea Te a new instance of the Rc2cryptoserviceprovider class//and automatically generate a Key and Iv. Rc2cryptoserviceprovider RC2CSP = new Rc2cryptoserviceprovider (); Console.WriteLine ("Effective key size is {0} bits.", rc2csp.effectivekeysize); String striv = "1234567"; Get the key and Iv. byte[] key = Rc2csp.key; Byte[] IV;//= Rc2csp.iv; IV = ASCIIEncoding.ASCII.GetBytes (Striv); Get an encryptor. ICryptoTransform encryptor = Rc2csp.createencryptor (key, IV); Encrypt the data as an array of encrypted bytes in memory. MemoryStream msencrypt = new MemoryStream (); CryptoStream csencrypt = new CryptoStream (Msencrypt, Encryptor, CrYptostreammode.write); Convert the data to a byte array. String original = "Here are some data to encrypt."; byte[] Toencrypt = Encoding.ASCII.GetBytes (original); Write all data to the crypto stream and flush it. Csencrypt.write (toencrypt, 0, toencrypt.length); Csencrypt.flushfinalblock (); Get the encrypted array of bytes. Byte[] encrypted = Msencrypt.toarray (); /**//////////////////////////////////////////////////////////This is where the data could be Tran Smitted or saved. /**//////////////////////////////////////////////////////////get A decryptor that uses the same ke Y and IV as the encryptor. ICryptoTransform decryptor = Rc2csp.createdecryptor (key, IV); Now decrypt the previously encrypted message using the Decryptor//obtained in the above step. MemoryStreamMsdecrypt = new MemoryStream (encrypted); CryptoStream csdecrypt = new CryptoStream (Msdecrypt, Decryptor, CryptoStreamMode.Read); byte[] Fromencrypt = new Byte[toencrypt.length]; Read the data out of the crypto stream. Csdecrypt.read (fromencrypt, 0, toencrypt.length); Convert the byte array back to a string. String roundtrip = Encoding.ASCII.GetString (Fromencrypt); Display the original data and the decrypted data. Console.WriteLine ("Original: {0}", Original); Console.WriteLine ("Round trip: {0}", roundtrip); Console.ReadLine (); } }}
Microsoft Rc2cryptoserviceprovider Plus Decryption