RC2CryptoServiceProvider 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
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
- 继承
- 属性
示例
下面的代码示例对字符串进行加密,然后解密。
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 对象不使用 salt。
注意
提供了较新的对称加密算法,即高级加密标准 (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 的密钥。 |
方法
显式接口实现
IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。
已过时.
释放由 SymmetricAlgorithm 占用的非托管资源,还可以另外再释放托管资源。 (继承自 SymmetricAlgorithm) |