RC2CryptoServiceProvider RC2CryptoServiceProvider RC2CryptoServiceProvider RC2CryptoServiceProvider Class

定义

定义访问 RC2 算法的加密服务提供程序 (CSP) 实现的包装对象。Defines a wrapper object to access the cryptographic service provider (CSP) implementation of the RC2 algorithm. 此类不能被继承。This class cannot be inherited.

public ref class RC2CryptoServiceProvider sealed : System::Security::Cryptography::RC2
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RC2CryptoServiceProvider : System.Security.Cryptography.RC2
type RC2CryptoServiceProvider = class
    inherit RC2
Public NotInheritable Class RC2CryptoServiceProvider
Inherits RC2
继承
RC2CryptoServiceProviderRC2CryptoServiceProviderRC2CryptoServiceProviderRC2CryptoServiceProvider
属性

示例

下面的代码示例对字符串进行加密和解密。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();
        }
    }
}
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

注解

RC2CryptoServiceProvider实现支持从40位到128位的密钥长度 (以8位为增量)。The RC2CryptoServiceProvider implementation supports key lengths from 40 bits to 128 bits in increments of 8 bits.

RC2CryptoServiceProvider对象是一个块密码, 它以8个字节的块加密和解密数据。The RC2CryptoServiceProvider object is a block cipher that encrypts and decrypts data in blocks of 8 bytes. 此类在最终数据块小于8个字节的情况下填充最终数据块。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.

请注意, RC2CryptoServiceProvider该对象不使用 salt。Note that the RC2CryptoServiceProvider object does not use salt.

备注

提供了一种较新的对称加密算法高级加密标准 (AES)。A newer symmetric encryption algorithm, Advanced Encryption Standard (AES), is available. 请考虑使用Aes算法及其派生类 (而不是RC2CryptoServiceProvider类)。Consider using the Aes algorithm and its derived classes instead of the RC2CryptoServiceProvider class. RC2CryptoServiceProvider仅用于与旧版应用程序和数据兼容。Use RC2CryptoServiceProvider only for compatibility with legacy applications and data.

构造函数

RC2CryptoServiceProvider() RC2CryptoServiceProvider() RC2CryptoServiceProvider() RC2CryptoServiceProvider()

初始化 RC2CryptoServiceProvider 类的新实例。Initializes a new instance of the RC2CryptoServiceProvider class.

属性

BlockSize BlockSize BlockSize BlockSize

获取或设置加密操作的块大小(以位为单位)。Gets or sets the block size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
EffectiveKeySize EffectiveKeySize EffectiveKeySize EffectiveKeySize

获取或设置 RC2 算法所用密钥的有效大小(以位为单位)。Gets or sets the effective size, in bits, of the secret key used by the RC2 algorithm.

FeedbackSize FeedbackSize FeedbackSize FeedbackSize

获取或设置加密操作的反馈大小(以位为单位)。Gets or sets the feedback size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
IV IV IV IV

获取或设置对称算法的初始化向量 (IV)。Gets or sets the initialization vector (IV) for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Key Key Key Key

获取或设置对称算法的密钥。Gets or sets the secret key for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
KeySize KeySize KeySize KeySize

获取或设置 RC2 算法使用的机密密钥的大小(以位为单位)。Gets or sets the size of the secret key used by the RC2 algorithm in bits.

(Inherited from RC2)
LegalBlockSizes LegalBlockSizes LegalBlockSizes LegalBlockSizes

获取对称算法支持的块大小(以位为单位)。Gets the block sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalKeySizes LegalKeySizes LegalKeySizes LegalKeySizes

获取对称算法支持的密钥大小(以位为单位)。Gets the key sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Mode Mode Mode Mode

获取或设置对称算法的运算模式。Gets or sets the mode for operation of the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
Padding Padding Padding Padding

获取或设置对称算法中使用的填充模式。Gets or sets the padding mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
UseSalt UseSalt UseSalt UseSalt

获取或设置一个值,该值确定是否创建一个具有 11 字节长的零值 salt 的密钥。Gets or sets a value that determines whether to create a key with an 11-byte-long, zero-value salt.

方法

Clear() Clear() Clear() Clear()

释放 SymmetricAlgorithm 类使用的所有资源。Releases all resources used by the SymmetricAlgorithm class.

(Inherited from SymmetricAlgorithm)
CreateDecryptor() CreateDecryptor() CreateDecryptor() CreateDecryptor()

用当前的 Key 属性和初始化向量 (IV) 创建对称解密器对象。Creates a symmetric decryptor object with the current Key property and initialization vector (IV).

(Inherited from SymmetricAlgorithm)
CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[]) CreateDecryptor(Byte[], Byte[])

用指定的密钥 (Key) 和初始化向量 (IV) 创建对称的 RC2 解密器对象。Creates a symmetric RC2 decryptor object with the specified key (Key) and initialization vector (IV).

CreateEncryptor() CreateEncryptor() CreateEncryptor() CreateEncryptor()

用当前的 Key 属性和初始化向量 (IV) 创建对称加密器对象。Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

(Inherited from SymmetricAlgorithm)
CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[]) CreateEncryptor(Byte[], Byte[])

用指定的密钥 (RC2) 和初始化向量 (Key) 创建对称的 IV 加密器对象。Creates a symmetric RC2 encryptor object with the specified key (Key) and initialization vector (IV).

Dispose() Dispose() Dispose() Dispose()

释放 SymmetricAlgorithm 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the SymmetricAlgorithm class.

(Inherited from SymmetricAlgorithm)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(Inherited from SymmetricAlgorithm)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GenerateIV() GenerateIV() GenerateIV() GenerateIV()

生成用于该算法的随机初始化向量 (IV)。Generates a random initialization vector (IV) to use for the algorithm.

GenerateKey() GenerateKey() GenerateKey() GenerateKey()

生成用于该算法的随机密钥 (Key)。Generates a random key (Key) to be used for the algorithm.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
ValidKeySize(Int32) ValidKeySize(Int32) ValidKeySize(Int32) ValidKeySize(Int32)

确定指定的密钥大小对当前算法是否有效。Determines whether the specified key size is valid for the current algorithm.

(Inherited from SymmetricAlgorithm)

字段

BlockSizeValue BlockSizeValue BlockSizeValue BlockSizeValue

表示加密操作的块大小(以位为单位)。Represents the block size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
EffectiveKeySizeValue EffectiveKeySizeValue EffectiveKeySizeValue EffectiveKeySizeValue

表示 RC2 算法使用的机密密钥的有效大小(以位为单位)。Represents the effective size of the secret key used by the RC2 algorithm in bits.

(Inherited from RC2)
FeedbackSizeValue FeedbackSizeValue FeedbackSizeValue FeedbackSizeValue

表示加密操作的反馈大小(以位为单位)。Represents the feedback size, in bits, of the cryptographic operation.

(Inherited from SymmetricAlgorithm)
IVValue IVValue IVValue IVValue

表示对称算法的初始化向量 (IV)。Represents the initialization vector (IV) for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
KeySizeValue KeySizeValue KeySizeValue KeySizeValue

表示对称算法使用的密钥的大小(以位为单位)。Represents the size, in bits, of the secret key used by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
KeyValue KeyValue KeyValue KeyValue

表示对称算法的密钥。Represents the secret key for the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalBlockSizesValue LegalBlockSizesValue LegalBlockSizesValue LegalBlockSizesValue

指定对称算法支持的块大小(以位为单位)。Specifies the block sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue LegalKeySizesValue

指定对称算法支持的密钥大小(以位为单位)。Specifies the key sizes, in bits, that are supported by the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
ModeValue ModeValue ModeValue ModeValue

表示对称算法中使用的密码模式。Represents the cipher mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)
PaddingValue PaddingValue PaddingValue PaddingValue

表示对称算法中使用的填充模式。Represents the padding mode used in the symmetric algorithm.

(Inherited from SymmetricAlgorithm)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.

(Inherited from SymmetricAlgorithm)

适用于

另请参阅