C# CryptoStream

來源:互聯網
上載者:User

標籤:

using System;using System.IO;using System.Security.Cryptography;namespace RijndaelManaged_Example{    class RijndaelExample    {        public static void Main()        {            try            {                string original = "Here is 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.                    string roundtrip = 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(string plainText, byte[] Key, byte[] IV)        {            // Check arguments.            if (plainText == null || plainText.Length <= 0)                throw new ArgumentNullException("plainText");            if (Key == null || Key.Length <= 0)                throw new ArgumentNullException("Key");            if (IV == null || IV.Length <= 0)                throw new ArgumentNullException("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 = new MemoryStream())                {                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))                    {                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))                        {                            //Write all data to the stream.                            swEncrypt.Write(plainText);                        }                        encrypted = msEncrypt.ToArray();                    }                }            }            // Return the encrypted bytes from the memory stream.            return encrypted;        }        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)        {            // Check arguments.            if (cipherText == null || cipherText.Length <= 0)                throw new ArgumentNullException("cipherText");            if (Key == null || Key.Length <= 0)                throw new ArgumentNullException("Key");            if (IV == null || IV.Length <= 0)                throw new ArgumentNullException("Key");            // Declare the string used to hold            // the decrypted text.            string plaintext = 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 = new MemoryStream(cipherText))                {                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))                    {                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))                        {                            // Read the decrypted bytes from the decrypting stream                            // and place them in a string.                            plaintext = srDecrypt.ReadToEnd();                        }                    }                }            }            return plaintext;        }    }}

 

原文地址:https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.cryptostream.aspx

     

C# CryptoStream

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.