다음을 통해 공유


RC2CryptoServiceProvider 클래스

래퍼 개체를 정의하여 RC2 알고리즘의 CSP(암호화 서비스 공급자) 구현에 액세스합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Security.Cryptography
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public NotInheritable Class RC2CryptoServiceProvider
    Inherits RC2
‘사용 방법
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

설명

RC2CryptoServiceProvider 구현에서는 한 번에 8비트씩 증가하는 40비트에서 128비트까지의 키 길이를 지원합니다.

RC2CryptoServiceProvider는 데이터를 8바이트 블록으로 암호화하고 해독하는 블록 암호화 개체입니다. 이 클래스는 최종 데이터 블록이 8바이트 미만일 경우 이를 채웁니다. 따라서 암호화된 데이터 길이가 원래의 일반 텍스트보다 커질 수 있습니다.

RC2CryptoServiceProvider 개체는 솔트를 사용하지 않습니다.

예제

다음 코드 예제에서는 문자열을 암호화한 다음 해독합니다.

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

상속 계층 구조

System.Object
   System.Security.Cryptography.SymmetricAlgorithm
     System.Security.Cryptography.RC2
      System.Security.Cryptography.RC2CryptoServiceProvider

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

RC2CryptoServiceProvider 멤버
System.Security.Cryptography 네임스페이스

기타 리소스

암호화 서비스