Export (0) Print
Expand All

RC2CryptoServiceProvider.CreateEncryptor Method (Byte[], Byte[])

Creates a symmetric RC2 encryptor object with the specified key (Key) and initialization vector (IV).

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

public override ICryptoTransform CreateEncryptor (
	byte[] rgbKey,
	byte[] rgbIV
)
public ICryptoTransform CreateEncryptor (
	byte[] rgbKey, 
	byte[] rgbIV
)
public override function CreateEncryptor (
	rgbKey : byte[], 
	rgbIV : byte[]
) : ICryptoTransform
Not applicable.

Parameters

rgbKey

The secret key to use for the symmetric algorithm.

rgbIV

The initialization vector to use for the symmetric algorithm.

Return Value

A symmetric RC2 encryptor object.

Exception typeCondition

CryptographicException

An OFB cipher mode was used.

-or-

A CFB cipher mode with a feedback size other than 8 bits was used.

-or-

An invalid key size was used.

-or-

The algorithm key size was not available.

Use the CreateDecryptor overload with the same parameters to decrypt the result of this method.

The following code example encrypts and then decrypts a string.

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();
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft