RC2CryptoServiceProvider Class
Updated: February 2009
Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the RC2 algorithm. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
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.
Note: |
|---|
A newer symmetric encryption algorithm, Advanced Encryption Standard (AES), is available. Consider using the AesCryptoServiceProvider class instead of the RC2CryptoServiceProvider class. |
The following code example encrypts and then decrypts a string.
Imports System Imports System.IO Imports System.Text Imports System.Security.Cryptography Module Crypto Sub Main() ' Create a new instance of the RC2CryptoServiceProvider class ' and automatically generate a Key and IV. Dim rc2CSP As New RC2CryptoServiceProvider() Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize) ' Get the key and IV. Dim key As Byte() = rc2CSP.Key Dim IV As Byte() = rc2CSP.IV ' Get an encryptor. Dim encryptor As ICryptoTransform = rc2CSP.CreateEncryptor(key, IV) ' Encrypt the data as an array of encrypted bytes in memory. Dim msEncrypt As New MemoryStream() Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write) ' Convert the data to a byte array. Dim original As String = "Here is some data to encrypt." Dim toEncrypt As Byte() = 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. Dim encrypted As Byte() = 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. Dim decryptor As ICryptoTransform = rc2CSP.CreateDecryptor(key, IV) ' Now decrypt the previously encrypted message using the decryptor ' obtained in the above step. Dim msDecrypt As New MemoryStream(encrypted) Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read) ' Read the decrypted bytes from the decrypting stream ' and place them in a StringBuilder class. Dim roundtrip As New StringBuilder() Dim b As Integer = 0 Do b = csDecrypt.ReadByte() If b <> -1 Then roundtrip.Append(ChrW(b)) End If Loop While b <> -1 ' Display the original data and the decrypted data. Console.WriteLine("Original: {0}", original) Console.WriteLine("Round Trip: {0}", roundtrip) Console.ReadLine() End Sub End Module
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.RC2
System.Security.Cryptography.RC2CryptoServiceProvider
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: