CryptoProvider.Decrypt Method (Byte[])

 

Decrypts cipher text to clear text.

Namespace:   System.Security.RightsManagement
Assembly:  WindowsBase (in WindowsBase.dll)

public byte[] Decrypt(
	byte[] cryptoText
)

Parameters

cryptoText
Type: System.Byte[]

The cipher text to decrypt.

Return Value

Type: System.Byte[]

The decrypted clear text of cryptoText.

Exception Condition
ArgumentNullException

cipherText is null.

RightsManagementException

Decryption right not granted.

The following example shows how to use the Decrypt method to convert encrypted data to unencrypted data.

byte[] imageBuffer;
using (Stream cipherTextStream = File.OpenRead(encryptedFile))
{
    byte[] contentLengthByteBuffer = new byte[sizeof(Int32)];
    // Read the length of the source content file
    // from the first four bytes of the encrypted file.
    ReliableRead(cipherTextStream, contentLengthByteBuffer,
                 0, sizeof(Int32));

    // Allocate the clearText buffer.
    int contentLength =
        BitConverter.ToInt32(contentLengthByteBuffer, 0);
    imageBuffer = new byte[contentLength];

    // Allocate the cipherText block.
    byte[] cipherTextBlock =
        new byte[cryptoProvider.BlockSize];

    // decrypt cipherText to clearText block by block.
    int imageBufferIndex = 0;
    for ( ; ; )
    {   // Read cipherText block.
        int readCount = ReliableRead(
                            cipherTextStream,
                            cipherTextBlock, 0,
                            cryptoProvider.BlockSize);
        // readCount of zero is end of data.
        if (readCount == 0)
            break; // for

        // Decrypt cipherText to clearText.
        byte[] clearTextBlock =
            cryptoProvider.Decrypt(cipherTextBlock);

        // Copy the clearTextBlock to the imageBuffer.
        int copySize = Math.Min(clearTextBlock.Length,
                            contentLength-imageBufferIndex);
        Array.Copy(clearTextBlock, 0,
            imageBuffer, imageBufferIndex, copySize);
        imageBufferIndex += copySize;
    }
}// end:using (Stream cipherTextStream = (close/dispose)

.NET Framework
Available since 3.0
Return to top
Show: