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