C # CryptoStream

Source: Internet
Author: User

usingSystem;usingSystem.IO;usingSystem.Security.Cryptography;namespacerijndaelmanaged_example{classRijndaelexample { Public Static voidMain () {Try            {                stringOriginal ="Here's some data to encrypt!"; //Create A new instance of the Rijndael//class. This generates a new key and initialization//vector (IV).                using(Rijndael Myrijndael =rijndael.create ()) {                    //Encrypt The string to an array of bytes.                    byte[] encrypted =encryptstringtobytes (Original, Myrijndael.key, MYRIJNDAEL.IV); //Decrypt the bytes to a string.                    stringroundtrip =decryptstringfrombytes (Encrypted, Myrijndael.key, MYRIJNDAEL.IV); //Display The original data and the decrypted data.Console.WriteLine ("Original: {0}", original); Console.WriteLine ("Round trip: {0}", roundtrip); }            }            Catch(Exception e) {Console.WriteLine ("Error: {0}", E.message); }        }        Static byte[] Encryptstringtobytes (stringPlainText,byte[] Key,byte[] IV) {//Check arguments.            if(PlainText = =NULL|| Plaintext.length <=0)                Throw NewArgumentNullException ("plaintext"); if(Key = =NULL|| Key.length <=0)                Throw NewArgumentNullException ("Key"); if(IV = =NULL|| Iv.. Length <=0)                Throw NewArgumentNullException ("Key"); byte[] encrypted; //Create an Rijndael object//With the specified key and Iv.            using(Rijndael Rijalg =rijndael.create ()) {Rijalg.key=Key; Rijalg.iv=IV; //Create a decrytor to perform the stream transform.ICryptoTransform encryptor =Rijalg.createencryptor (Rijalg.key, RIJALG.IV); //Create The streams used for encryption.                using(MemoryStream msencrypt =NewMemoryStream ()) {                    using(CryptoStream csencrypt =NewCryptoStream (Msencrypt, Encryptor, CryptoStreamMode.Write)) {                        using(StreamWriter Swencrypt =NewStreamWriter (Csencrypt)) {                            //Write all data to the stream.Swencrypt.write (plaintext); } Encrypted=Msencrypt.toarray (); }                }            }            //Return the encrypted bytes from the memory stream.            returnencrypted; }        Static stringDecryptstringfrombytes (byte[] Ciphertext,byte[] Key,byte[] IV) {//Check arguments.            if(Ciphertext = =NULL|| Ciphertext.length <=0)                Throw NewArgumentNullException ("Ciphertext"); if(Key = =NULL|| Key.length <=0)                Throw NewArgumentNullException ("Key"); if(IV = =NULL|| Iv.. Length <=0)                Throw NewArgumentNullException ("Key"); //Declare The string used to hold//The decrypted text.            stringplaintext =NULL; //Create an Rijndael object//With the specified key and Iv.            using(Rijndael Rijalg =rijndael.create ()) {Rijalg.key=Key; Rijalg.iv=IV; //Create a decrytor to perform the stream transform.ICryptoTransform decryptor =Rijalg.createdecryptor (Rijalg.key, RIJALG.IV); //Create The streams used for decryption.                using(MemoryStream msdecrypt =NewMemoryStream (ciphertext)) {                    using(CryptoStream csdecrypt =NewCryptoStream (Msdecrypt, Decryptor, CryptoStreamMode.Read)) {                        using(StreamReader Srdecrypt =NewStreamReader (Csdecrypt)) {                            //Read The decrypted bytes from the decrypting stream//and place them in a string.plaintext =Srdecrypt.readtoend (); }                    }                }            }            returnplaintext; }    }}

Original address: Https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.cryptostream.aspx

C # CryptoStream

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.