Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

RijndaelManaged.CreateDecryptor Method (Byte[], Byte[])

Creates a symmetric Rijndael decryptor object with the specified Key and initialization vector (IV).

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

public override ICryptoTransform CreateDecryptor (
	byte[] rgbKey,
	byte[] rgbIV
)
public ICryptoTransform CreateDecryptor (
	byte[] rgbKey, 
	byte[] rgbIV
)
public override function CreateDecryptor (
	rgbKey : byte[], 
	rgbIV : byte[]
) : ICryptoTransform

Parameters

rgbKey

The secret key to be used for the symmetric algorithm.

rgbIV

The IV to be used for the symmetric algorithm.

Return Value

A symmetric Rijndael decryptor object.
Exception typeCondition

ArgumentNullException

The rgbKey parameter is a null reference (Nothing in Visual Basic).

-or-

The rgbIV parameter is a null reference (Nothing in Visual Basic).

CryptographicException

The value of the Mode parameter is not ECB, CBC, or CFB.

-or-

The rgbKey parameter has an invalid key size.

This method decrypts an encrypted message created using the CreateEncryptor overload with the same signature.

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RijndaelManaged_Examples
{
    class MyMainClass
    {
        public static void Main()
        {
            string original = "This is a much longer string of data than a public/private key algorithm will accept.";
            string roundtrip;
            ASCIIEncoding textConverter = new ASCIIEncoding();
            RijndaelManaged myRijndael = new RijndaelManaged();
            byte[] fromEncrypt;
            byte[] encrypted;
            byte[] toEncrypt;
            byte[] key;
            byte[] IV;

            //Create a new key and initialization vector.
            myRijndael.GenerateKey();
            myRijndael.GenerateIV();

            //Get the key and IV.
            key = myRijndael.Key;
            IV = myRijndael.IV;

            //Get an encryptor.
            ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
            
            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            //Convert the data to a byte array.
            toEncrypt = textConverter.GetBytes(original);

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

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();

            //This is where the message would be transmitted to a recipient
            // who already knows your secret key. Optionally, you can
            // also encrypt your secret key using a public key algorithm
            // and pass it to the mesage recipient along with the RijnDael
            // encrypted message.            

            //Get a decryptor that uses the same key and IV as the encryptor.
            ICryptoTransform decryptor = myRijndael.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);

            fromEncrypt = new byte[encrypted.Length];

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

            //Convert the byte array back into a string.
            roundtrip = textConverter.GetString(fromEncrypt);

            //Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", original);
            Console.WriteLine("Round Trip: {0}", roundtrip);
        }
    }
}

package RijndaelManaged_Examples; 

import System.*;
import System.IO.*;
import System.Text.*;
import System.Security.Cryptography.*;

class MyMainClass
{
    public static void main(String[] args)
    {      
        String original = "This is a much longer string of data than a "
            + "public/private key algorithm will accept.";
        String roundTrip;
        ASCIIEncoding textConverter = new ASCIIEncoding();
        RijndaelManaged myRijndael = new RijndaelManaged();
        ubyte fromEncrypt[];
        ubyte encrypted[];
        ubyte toEncrypt[];
        ubyte key[];
        ubyte iv[];
        //Create a new key and initialization vector.
        myRijndael.GenerateKey();
        myRijndael.GenerateIV();
        //Get the key and iv.
        key = myRijndael.get_Key();
        iv = myRijndael.get_IV();
        //Get an encryptor.
        ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, iv);
        //Encrypt the data.
        MemoryStream msEncrypt = new MemoryStream();
        CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, 
            CryptoStreamMode.Write);
        //Convert the data to a byte array.
        toEncrypt = textConverter.GetBytes(original);
        //Write all data to the crypto stream and flush it.
        csEncrypt.Write(toEncrypt, 0, toEncrypt.get_Length());
        csEncrypt.FlushFinalBlock();
        //Get encrypted array of bytes.
        encrypted = msEncrypt.ToArray();
        //This is where the message would be transmitted to a recipient
        // who already knows your secret key. Optionally, you can
        // also encrypt your secret key using a public key algorithm
        // and pass it to the mesage recipient along with the RijnDael
        // encrypted message.            
        //Get a decryptor that uses the same key and iv as the encryptor.
        ICryptoTransform decryptor = myRijndael.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);
        fromEncrypt = new ubyte[encrypted.get_Length()];
        //Read the data out of the crypto stream.
        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.get_Length());
        //Convert the byte array back into a string.
        roundTrip = textConverter.GetString(fromEncrypt);
        //Display the original data and the decrypted data.
        Console.WriteLine("Original:   {0}", original);
        Console.WriteLine("Round Trip: {0}", roundTrip);
    } //main
} //MyMainClass

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.