Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

RC2CryptoServiceProvider Class

Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the RC2 algorithm. This class cannot be inherited.

System.Object
  System.Security.Cryptography.SymmetricAlgorithm
    System.Security.Cryptography.RC2
      System.Security.Cryptography.RC2CryptoServiceProvider

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
[ComVisibleAttribute(true)]
public sealed class RC2CryptoServiceProvider : RC2

The RC2CryptoServiceProvider type exposes the following members.

  NameDescription
Public methodRC2CryptoServiceProviderInitializes a new instance of the RC2CryptoServiceProvider class.
Top
  NameDescription
Public propertyBlockSizeGets or sets the block size, in bits, of the cryptographic operation. (Inherited from SymmetricAlgorithm.)
Public propertyEffectiveKeySizeGets or sets the effective size, in bits, of the secret key used by the RC2 algorithm. (Overrides RC2.EffectiveKeySize.)
Public propertyFeedbackSizeGets or sets the feedback size, in bits, of the cryptographic operation. (Inherited from SymmetricAlgorithm.)
Public propertyIVGets or sets the initialization vector (IV) for the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyKeyGets or sets the secret key for the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyKeySizeGets or sets the size of the secret key used by the RC2 algorithm in bits. (Inherited from RC2.)
Public propertyLegalBlockSizesGets the block sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyLegalKeySizesGets the key sizes, in bits, that are supported by the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyModeGets or sets the mode for operation of the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyPaddingGets or sets the padding mode used in the symmetric algorithm. (Inherited from SymmetricAlgorithm.)
Public propertyUseSaltGets or sets a value that determines whether to create a key with an 11-byte-long, zero-value salt.
Top
  NameDescription
Public methodClearReleases all resources used by the SymmetricAlgorithm class. (Inherited from SymmetricAlgorithm.)
Public methodCreateDecryptor()Creates a symmetric decryptor object with the current Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm.)
Public methodCreateDecryptor(Byte[], Byte[])Creates a symmetric RC2 decryptor object with the specified key (Key)and initialization vector (IV). (Overrides SymmetricAlgorithm.CreateDecryptor(Byte[], Byte[]).)
Public methodCreateEncryptor()Creates a symmetric encryptor object with the current Key property and initialization vector (IV). (Inherited from SymmetricAlgorithm.)
Public methodCreateEncryptor(Byte[], Byte[])Creates a symmetric RC2 encryptor object with the specified key (Key) and initialization vector (IV). (Overrides SymmetricAlgorithm.CreateEncryptor(Byte[], Byte[]).)
Public methodDispose()Releases all resources used by the current instance of the SymmetricAlgorithm class. (Inherited from SymmetricAlgorithm.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGenerateIVGenerates a random initialization vector (IV) to use for the algorithm. (Overrides SymmetricAlgorithm.GenerateIV().)
Public methodGenerateKeyGenerates a random key (Key) to be used for the algorithm. (Overrides SymmetricAlgorithm.GenerateKey().)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodValidKeySizeDetermines whether the specified key size is valid for the current algorithm. (Inherited from SymmetricAlgorithm.)
Top

The RC2CryptoServiceProvider implementation supports key lengths from 40 bits to 128 bits in increments of 8 bits.

The RC2CryptoServiceProvider object is a block cipher that encrypts and decrypts data in blocks of 8 bytes. This class pads the final block of data if it is less than 8 bytes. As a result of this padding, the length of encrypted data could be greater than the original plaintext.

Note that the RC2CryptoServiceProvider object does not use salt.

NoteNote

A newer symmetric encryption algorithm, Advanced Encryption Standard (AES), is available. Consider using the Aes algorithm and its derived classes instead of the RC2CryptoServiceProvider class. Use RC2CryptoServiceProvider only for compatibility with legacy applications and data.

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.