RC2CryptoServiceProvider-Klasse
Definiert ein Wrapperobjekt, um auf die Kryptografiedienstanbieter-Implementierung (Cryptographic Service Provider) des RC2-Algorithmus zuzugreifen. Diese Klasse kann nicht vererbt werden.
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class RC2CryptoServiceProvider
Inherits RC2
'Usage
Dim instance As RC2CryptoServiceProvider
[ComVisibleAttribute(true)]
public sealed class RC2CryptoServiceProvider : RC2
[ComVisibleAttribute(true)]
public ref class RC2CryptoServiceProvider sealed : public RC2
/** @attribute ComVisibleAttribute(true) */
public final class RC2CryptoServiceProvider extends RC2
ComVisibleAttribute(true)
public final class RC2CryptoServiceProvider extends RC2
Hinweise
Die RC2CryptoServiceProvider-Implementierung unterstützt eine Schlüssellänge von 40 bis 128 Bits und eine Inkrementierung um je 8 Bits.
Das RC2CryptoServiceProvider-Objekt ist eine Blockverschlüsselung, die Daten in Blöcken von 8 Bytes verschlüsselt und entschlüsselt. Diese Klasse füllt den abschließenden Block der Daten auf, wenn er weniger als 8 Bytes groß ist. Als Folge dieses Auffüllens kann die Länge der verschlüsselten Daten größer als der ursprüngliche Klartext sein.
Beachten Sie, dass das RC2CryptoServiceProvider-Objekt keine Salts verwendet.
Beispiel
Im folgenden Codebeispiel wird eine Zeichenfolge verschlüsselt und dann entschlüsselt.
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Module MyMainModule
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)
Dim fromEncrypt(toEncrypt.Length) As Byte
' Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, toEncrypt.Length)
' Convert the byte array back into a string.
Dim roundtrip As String = Encoding.ASCII.GetString(fromEncrypt)
' 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
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);
byte[] fromEncrypt = new byte[toEncrypt.Length];
// Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, toEncrypt.Length);
// Convert the byte array back into a string.
String roundtrip = Encoding.ASCII.GetString(fromEncrypt);
// Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
Console.ReadLine();
}
}
}
Vererbungshierarchie
System.Object
System.Security.Cryptography.SymmetricAlgorithm
System.Security.Cryptography.RC2
System.Security.Cryptography.RC2CryptoServiceProvider
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0
Siehe auch
Referenz
RC2CryptoServiceProvider-Member
System.Security.Cryptography-Namespace