RC2CryptoServiceProvider.UseSalt Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets a value that determines whether to create a key with an 11-byte-long, zero-value salt.

Namespace:   System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

public bool UseSalt { get; set; }

Property Value

Type: System.Boolean

true if the key should be created with an 11-byte-long, zero-value salt; otherwise, false. The default is false.

The UseSalt property allows you to interoperate with an existing application that uses an 11-byte-long, zero-value salt. For most scenarios, you should not use a salt with an RC2CryptoServiceProvider key.

The following code example sets the UseSalt property to true, and then encrypts and decrypts a value.

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;


            //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);

            //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);


.NET Framework
Available since 2.0
Return to top