다음을 통해 공유


RC2CryptoServiceProvider.UseSalt 속성

정의

11바이트 길이의 0값 솔트를 사용하여 키를 만들 것인지 여부를 결정하는 값을 가져오거나 설정합니다.

public:
 property bool UseSalt { bool get(); void set(bool value); };
public bool UseSalt { get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
public bool UseSalt { get; set; }
[System.Runtime.InteropServices.ComVisible(false)]
public bool UseSalt { get; set; }
[<set: System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.UseSalt : bool with get, set
member this.UseSalt : bool with get, set
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.UseSalt : bool with get, set
Public Property UseSalt As Boolean

속성 값

11바이트 길이의 0값 솔트를 사용하여 키를 만들어야 하면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

특성

예제

다음 코드 예제에서는 설정 합니다 UseSalt 속성을 true, 다음 암호화 하 고 값을 암호 해독 합니다.

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;

int main()
{
    array <Byte>^ originalBytes = 
        ASCIIEncoding::ASCII->GetBytes("Here is some data.");

    //Create a new RC2CryptoServiceProvider.
    RC2CryptoServiceProvider^ rc2Provider = 
        gcnew RC2CryptoServiceProvider();
    rc2Provider->UseSalt = true;

    rc2Provider->GenerateKey();
    rc2Provider->GenerateIV();

    //Encrypt the data.
    MemoryStream^ encryptionMemoryStream = gcnew MemoryStream();
    CryptoStream^ encryptionCryptoStream = gcnew CryptoStream(
        encryptionMemoryStream, rc2Provider->CreateEncryptor(
        rc2Provider->Key, rc2Provider->IV), CryptoStreamMode::Write);

    //Write all data to the crypto stream and flush it.
    encryptionCryptoStream->Write(originalBytes, 0, originalBytes->Length);
    encryptionCryptoStream->FlushFinalBlock();

    //Get encrypted array of bytes.
    array<Byte>^ encryptedBytes = encryptionMemoryStream->ToArray();

    //Decrypt the previously encrypted message.
    MemoryStream^ decryptionMemoryStream = 
        gcnew MemoryStream(encryptedBytes);
    CryptoStream^ decryptionCryptoStream = 
        gcnew CryptoStream(decryptionMemoryStream,
        rc2Provider->CreateDecryptor(rc2Provider->Key,rc2Provider->IV),
        CryptoStreamMode::Read);

    array<Byte>^ unencryptedBytes = 
        gcnew array<Byte>(originalBytes->Length); 

    //Read the data out of the crypto stream.
    decryptionCryptoStream->Read(unencryptedBytes, 0, 
        unencryptedBytes->Length);

    //Convert the byte array back into a string.
    String^ plainText = ASCIIEncoding::ASCII->GetString(unencryptedBytes);

    //Display the results.
    Console::WriteLine("Unencrypted text: {0}", plainText);

    Console::ReadLine();
}
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RC2CryptoServiceProvider_Examples
{
    class MyMainClass
    {
        public static void Main()
        {
            byte[] originalBytes = ASCIIEncoding.ASCII.GetBytes("Here is some data.");

        //Create a new RC2CryptoServiceProvider.
            RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();

            rc2CSP.UseSalt = true;

            rc2CSP.GenerateKey();
            rc2CSP.GenerateIV();

            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, rc2CSP.CreateEncryptor(rc2CSP.Key, rc2CSP.IV), CryptoStreamMode.Write);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(originalBytes, 0, originalBytes.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            byte[] encryptedBytes = msEncrypt.ToArray();

            //Decrypt the previously encrypted message.
            MemoryStream msDecrypt = new MemoryStream(encryptedBytes);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, rc2CSP.CreateDecryptor(rc2CSP.Key, rc2CSP.IV), CryptoStreamMode.Read);

            byte[] unencryptedBytes = new byte[originalBytes.Length];

            //Read the data out of the crypto stream.
            csDecrypt.Read(unencryptedBytes, 0, unencryptedBytes.Length);

            //Convert the byte array back into a string.
            string plaintext = ASCIIEncoding.ASCII.GetString(unencryptedBytes);

            //Display the results.
            Console.WriteLine("Unencrypted text: {0}", plaintext);

            Console.ReadLine();
        }
    }
}
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography



Module MyMainModule

    Sub Main()
        Dim originalBytes As Byte() = ASCIIEncoding.ASCII.GetBytes("Here is some data.")

        'Create a new RC2CryptoServiceProvider.
        Dim rc2CSP As New RC2CryptoServiceProvider()

        rc2CSP.UseSalt = True

        rc2CSP.GenerateKey()
        rc2CSP.GenerateIV()

        'Encrypt the data.
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, rc2CSP.CreateEncryptor(rc2CSP.Key, rc2CSP.IV), CryptoStreamMode.Write)

        'Write all data to the crypto stream and flush it.
        csEncrypt.Write(originalBytes, 0, originalBytes.Length)
        csEncrypt.FlushFinalBlock()

        'Get encrypted array of bytes.
        Dim encryptedBytes As Byte() = msEncrypt.ToArray()

        'Decrypt the previously encrypted message.
        Dim msDecrypt As New MemoryStream(encryptedBytes)
        Dim csDecrypt As New CryptoStream(msDecrypt, rc2CSP.CreateDecryptor(rc2CSP.Key, rc2CSP.IV), CryptoStreamMode.Read)

        Dim unencryptedBytes(originalBytes.Length - 1) As Byte

        'Read the data out of the crypto stream.
        csDecrypt.Read(unencryptedBytes, 0, unencryptedBytes.Length)

        'Convert the byte array back into a string.
        Dim plaintext As String = ASCIIEncoding.ASCII.GetString(unencryptedBytes)

        'Display the results.
        Console.WriteLine("Unencrypted text: {0}", plaintext)

        Console.ReadLine()

    End Sub
End Module

설명

UseSalt 속성을 11 바이트 길이의 0 값 솔트를 사용 하는 기존 애플리케이션과 상호 운용할 수 있습니다. 대부분의 시나리오에서는 키와 함께 RC2CryptoServiceProvider 솔트를 사용하면 안 됩니다.

적용 대상

추가 정보