RC2CryptoServiceProvider 类

定义

注意

Derived cryptographic types are obsolete. Use the Create method on the base type instead.

定义访问 RC2 算法的加密服务提供程序 (CSP) 实现的包装对象。 此类不能被继承。

public ref class RC2CryptoServiceProvider sealed : System::Security::Cryptography::RC2
public sealed class RC2CryptoServiceProvider : System.Security.Cryptography.RC2
[System.Obsolete("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId="SYSLIB0021", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public sealed class RC2CryptoServiceProvider : System.Security.Cryptography.RC2
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RC2CryptoServiceProvider : System.Security.Cryptography.RC2
type RC2CryptoServiceProvider = class
    inherit RC2
[<System.Obsolete("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId="SYSLIB0021", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type RC2CryptoServiceProvider = class
    inherit RC2
[<System.Runtime.InteropServices.ComVisible(true)>]
type RC2CryptoServiceProvider = class
    inherit RC2
Public NotInheritable Class RC2CryptoServiceProvider
Inherits RC2
继承
RC2CryptoServiceProvider
属性

示例

下面的代码示例加密并解密字符串。

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

    End Sub
End Module

注解

实现 RC2CryptoServiceProvider 支持从 40 位到 128 位的密钥长度,增量为 8 位。

RC2CryptoServiceProvider 对象是一个块密码,用于加密和解密 8 个字节块中的数据。 如果数据块小于 8 个字节,则此类会填充最终的数据块。 由于此填充,加密数据的长度可能大于原始纯文本。

请注意,对象 RC2CryptoServiceProvider 不使用盐。

备注

可以使用较新的对称加密算法高级加密标准 (AES) 。 请考虑使用 Aes 算法及其派生类而不是 RC2CryptoServiceProvider 类。 仅用于 RC2CryptoServiceProvider 与旧应用程序和数据的兼容性。

构造函数

RC2CryptoServiceProvider()

初始化 RC2CryptoServiceProvider 类的新实例。

字段

BlockSizeValue

表示加密操作的块大小(以位为单位)。

(继承自 SymmetricAlgorithm)
EffectiveKeySizeValue

表示 RC2 算法使用的机密密钥的有效大小(以位为单位)。

(继承自 RC2)
FeedbackSizeValue

表示加密操作的反馈大小(以位为单位)。

(继承自 SymmetricAlgorithm)
IVValue

表示对称算法的初始化向量 (IV)。

(继承自 SymmetricAlgorithm)
KeySizeValue

表示对称算法使用的密钥的大小(以位为单位)。

(继承自 SymmetricAlgorithm)
KeyValue

表示对称算法的密钥。

(继承自 SymmetricAlgorithm)
LegalBlockSizesValue

指定对称算法支持的块大小(以位为单位)。

(继承自 SymmetricAlgorithm)
LegalKeySizesValue

指定对称算法支持的密钥大小(以位为单位)。

(继承自 SymmetricAlgorithm)
ModeValue

表示对称算法中使用的密码模式。

(继承自 SymmetricAlgorithm)
PaddingValue

表示对称算法中使用的填充模式。

(继承自 SymmetricAlgorithm)

属性

BlockSize

获取或设置加密操作的块大小(以位为单位)。

(继承自 SymmetricAlgorithm)
EffectiveKeySize

获取或设置 RC2 算法所用密钥的有效大小(以位为单位)。

FeedbackSize

获取或设置针对密码反馈 (CFB) 和输出反馈 (OFB) 密码模式的加密操作的反馈大小(以位为单位)。

(继承自 SymmetricAlgorithm)
IV

获取或设置对称算法的初始化向量 (IV)。

(继承自 SymmetricAlgorithm)
Key

获取或设置对称算法的密钥。

(继承自 SymmetricAlgorithm)
KeySize

获取或设置 RC2 算法所用密钥的大小(以位为单位)。

(继承自 RC2)
LegalBlockSizes

获取对称算法支持的块大小(以位为单位)。

(继承自 SymmetricAlgorithm)
LegalKeySizes

获取对称算法支持的密钥大小(以位为单位)。

(继承自 SymmetricAlgorithm)
Mode

获取或设置对称算法的运算模式。

(继承自 SymmetricAlgorithm)
Padding

获取或设置对称算法中使用的填充模式。

(继承自 SymmetricAlgorithm)
UseSalt

获取或设置一个值,该值确定是否创建一个具有 11 字节长的零值 salt 的密钥。

方法

Clear()

释放 SymmetricAlgorithm 类使用的所有资源。

(继承自 SymmetricAlgorithm)
CreateDecryptor()

用当前的 Key 属性和初始化向量 (IV) 创建对称解密器对象。

(继承自 SymmetricAlgorithm)
CreateDecryptor(Byte[], Byte[])

用指定的密钥 (Key) 和初始化向量 (IV) 创建对称的 RC2 解密器对象。

CreateEncryptor()

用当前的 Key 属性和初始化向量 (IV) 创建对称加密器对象。

(继承自 SymmetricAlgorithm)
CreateEncryptor(Byte[], Byte[])

用指定的密钥 (RC2) 和初始化向量 (Key) 创建对称的 IV 加密器对象。

DecryptCbc(Byte[], Byte[], PaddingMode)

使用具有指定填充模式的 CBC 模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
DecryptCfb(Byte[], Byte[], PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
DecryptEcb(Byte[], PaddingMode)

使用具有指定填充模式的分片模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptEcb(ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的分片模式解密数据。

(继承自 SymmetricAlgorithm)
DecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的分片模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
Dispose()

释放 SymmetricAlgorithm 类的当前实例所使用的所有资源。

(继承自 SymmetricAlgorithm)
Dispose(Boolean)

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。

(继承自 SymmetricAlgorithm)
EncryptCbc(Byte[], Byte[], PaddingMode)

使用指定的填充模式,使用 CBC 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

使用指定的填充模式,使用 CBC 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
EncryptCfb(Byte[], Byte[], PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式,将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
EncryptEcb(Byte[], PaddingMode)

使用具有指定填充模式的 ECB 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptEcb(ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式对数据进行加密。

(继承自 SymmetricAlgorithm)
EncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GenerateIV()

生成用于该算法的随机初始化向量 (IV)。

GenerateKey()

生成用于该算法的随机密钥 (Key)。

GetCiphertextLengthCbc(Int32, PaddingMode)

获取在 CBC 模式下具有给定填充模式和纯文本长度的密码文本长度。

(继承自 SymmetricAlgorithm)
GetCiphertextLengthCfb(Int32, PaddingMode, Int32)

获取在 CFB 模式下具有给定填充模式和纯文本长度的密码文本长度。

(继承自 SymmetricAlgorithm)
GetCiphertextLengthEcb(Int32, PaddingMode)

以 ECB 模式获取具有给定填充模式和纯文本长度的密码文本长度。

(继承自 SymmetricAlgorithm)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)
TryDecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode)

尝试使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区。

(继承自 SymmetricAlgorithm)
TryDecryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

当在派生类中重写时,尝试使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryDecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32)

尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据解密到指定的缓冲区。

(继承自 SymmetricAlgorithm)
TryDecryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32)

在派生类中重写时,尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryDecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

尝试将数据解密到指定的缓冲区中,同时使用具有指定填充模式的分片模式。

(继承自 SymmetricAlgorithm)
TryDecryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

当在派生类中重写时,尝试使用具有指定填充模式的分片模式将数据解密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryEncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode)

尝试使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

在派生类中重写时,尝试使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryEncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32)

尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryEncryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32)

在派生类中重写时,尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
TryEncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

尝试将数据加密到指定的缓冲区,同时使用具有指定填充模式的分片模式。

(继承自 SymmetricAlgorithm)
TryEncryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

当在派生类中重写时,尝试使用具有指定填充模式的 ECB 模式将数据加密到指定的缓冲区中。

(继承自 SymmetricAlgorithm)
ValidKeySize(Int32)

确定指定的密钥大小对当前算法是否有效。

(继承自 SymmetricAlgorithm)

显式接口实现

IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。

(继承自 SymmetricAlgorithm)

适用于

另请参阅