RC2CryptoServiceProvider.UseSalt プロパティ

定義

11 バイト長、値 0 の salt を使用してキーを作成するかどうかを指定する値を取得または設定します。

C#
public bool UseSalt { get; [System.Runtime.Versioning.SupportedOSPlatform("windows")] set; }
C#
public bool UseSalt { get; set; }
C#
[System.Runtime.InteropServices.ComVisible(false)]
public bool UseSalt { get; set; }

プロパティ値

11 バイト長、値 0 の salt を使用してキーを作成する場合は true。それ以外の場合は、false。 既定値は、false です。

属性

次のコード例では、 プロパティを UseSalttrue設定し、値の暗号化と暗号化解除を行います。

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

注釈

UseSaltプロパティを使用すると、11 バイト長の 0 値の salt を使用する既存のアプリケーションと相互運用できます。 ほとんどのシナリオでは、キーで RC2CryptoServiceProvider salt を使用しないでください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください