C# RC2CryptoServiceProvider.GenerateKey方法代码示例(c#rc2cryptoserviceprovider.generatekey方法代码示例)

本文整理汇总了C#中System.Security.Cryptography.RC2CryptoServiceProvider.GenerateKey方法的典型用法代码示例。如果您正苦于以下问题:C# RC2CryptoServiceProvider.GenerateKey方法的具体用法?C# RC2CryptoServiceProvider.GenerateKey怎么用?C# RC2CryptoServiceProvider.GenerateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.RC2CryptoServiceProvider的用法示例。


C# RC2CryptoServiceProvider.GenerateKey方法代码示例(c#rc2cryptoserviceprovider.generatekey方法代码示例)

在下文中一共展示了RC2CryptoServiceProvider.GenerateKey方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RC2CryptoServiceProvider_Examples
{
    class MyMainClass
    {
        public static void Main()
        {

            // Create 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);

            // Get the key and IV.
            byte[] key = rc2CSP.Key;
            byte[] IV = rc2CSP.IV;

            // 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 is 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 transmitted or saved.          
            ///////////////////////////////////////////////////////

            //Get a decryptor that uses the same key 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.
            MemoryStream msDecrypt = new MemoryStream(encrypted);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            // Read the decrypted bytes from the decrypting stream
            // and place them in a StringBuilder class.

            StringBuilder roundtrip = new StringBuilder();
            
            int b = 0;

            do
            {
                b = csDecrypt.ReadByte();
                
                if (b != -1)
                {
                    roundtrip.Append((char)b);
                }
            } while (b != -1);

            // Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", original);
            Console.WriteLine("Round Trip: {0}", roundtrip);

            Console.ReadLine();
        }
    }
}
开发者ID:.NET开发者,项目名称:System.Security.Cryptography,代码行数:78,代码来源:RC2CryptoServiceProvider.GenerateKey

本文标签属性:

示例:示例英语

代码:代码生成器

RC2CryptoServiceProvider:RC2CryptoServiceProvider

GenerateKey:GenerateKey

上一篇:如果有人故意用激光笔照人眼睛犯不犯法(激光笔对眼睛的伤害有多大?)
下一篇:PHP HTML_QuickForm::arrayMerge方法代码示例(php_quickform::arraymerge方法的典型用法代码示例)

为您推荐