Microsoft Rc2cryptoserviceprovider Plus Decryption

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.