Export (0) Print
Expand All
3 out of 8 rated this helpful - Rate this topic

DESCryptoServiceProvider Class

Defines a wrapper object to access the cryptographic service provider (CSP) version of the Data Encryption Standard (DES) algorithm. This class cannot be inherited.

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

[ComVisibleAttribute(true)] 
public sealed class DESCryptoServiceProvider : DES
/** @attribute ComVisibleAttribute(true) */ 
public final class DESCryptoServiceProvider extends DES
ComVisibleAttribute(true) 
public final class DESCryptoServiceProvider extends DES

This algorithm supports a key length of 64 bits.

The following example uses the DESCryptoServiceProvider class to encrypt some data to memory and then decrypt the data.

// This sample demonstrates using a key based on the cryptographic service provider (CSP) version
// of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then 
// to decrypt the byte array back to a string.

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

class CryptoMemoryStream
{
	// Main method.
    public static void Main()
    {
        // Create a new DES key.
        DESCryptoServiceProvider key = new DESCryptoServiceProvider();

        // Encrypt a string to a byte array.
        byte[] buffer = Encrypt("This is some plaintext!", key);

        // Decrypt the byte array back to a string.
        string plaintext =  Decrypt(buffer, key);

        // Display the plaintext value to the console.
        Console.WriteLine(plaintext);
    }
    
	// Encrypt the string.
    public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
    {
        // Create a memory stream.
        MemoryStream ms = new MemoryStream();

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key.  
        CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

        // Create a StreamWriter to write a string
        // to the stream.
        StreamWriter sw = new StreamWriter(encStream);

        // Write the plaintext to the stream.
        sw.WriteLine(PlainText);

        // Close the StreamWriter and CryptoStream.
        sw.Close();
        encStream.Close();

        // Get an array of bytes that represents
        // the memory stream.
        byte[] buffer = ms.ToArray();

        // Close the memory stream.
        ms.Close();

        // Return the encrypted byte array.
        return buffer;
    }

   // Decrypt the byte array.
    public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
    {
        // Create a memory stream to the passed buffer.
        MemoryStream ms = new MemoryStream(CypherText);

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key. 
        CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

        // Create a StreamReader for reading the stream.
        StreamReader sr = new StreamReader(encStream);

        // Read the stream as a string.
        string val = sr.ReadLine();

        // Close the streams.
        sr.Close();
        encStream.Close();
        ms.Close();
            
        return val;
    }
}

// This sample demonstrates using a key based on the cryptographic 
// service provider (CSP) version
// of the Data Encryption Standard (DES)algorithm to encrypt a string 
// to a byte array, and then to decrypt the byte array back to a string.

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

class CryptoMemoryStream
{
    // main method.
    public static void main(String[] args)
    {
        // Create a new DES key.
        DESCryptoServiceProvider key = new DESCryptoServiceProvider();

        // Encrypt a string to a byte array.
        ubyte buffer[] = Encrypt("This is some plaintext!", key);

        // Decrypt the byte array back to a string.
        String plainText = Decrypt(buffer, key);

        // Display the plaintext value to the console.
        Console.WriteLine(plainText);
    } //main

    // Encrypt the string.
    public static ubyte[] Encrypt(String plainText, SymmetricAlgorithm key)
    {
        // Create a memory stream.
        MemoryStream ms = new MemoryStream();

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key.  
        CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), 
            CryptoStreamMode.Write);

        // Create a StreamWriter to write a string
        // to the stream.
        StreamWriter sw = new StreamWriter(encStream);

        // Write the plaintext to the stream.
        sw.WriteLine(plainText);

        // Close the StreamWriter and CryptoStream.
        sw.Close();
        encStream.Close();

        // Get an array of bytes that represents
        // the memory stream.
        ubyte buffer[] = ms.ToArray();

        // Close the memory stream.
        ms.Close();

        // Return the encrypted byte array.
        return buffer;
    } //Encrypt

    // Decrypt the byte array.
    public static String Decrypt(ubyte cypherText[], SymmetricAlgorithm key)
    {
        // Create a memory stream to the passed buffer.
        MemoryStream ms = new MemoryStream(cypherText);

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key. 
        CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), 
            CryptoStreamMode.Read);

        // Create a StreamReader for reading the stream.
        StreamReader sr = new StreamReader(encStream);

        // Read the stream as a string.
        String val = sr.ReadLine();

        // Close the streams.
        sr.Close();
        encStream.Close();
        ms.Close();
        return val;
    } //Decrypt
} //CryptoMemoryStream

System.Object
   System.Security.Cryptography.SymmetricAlgorithm
     System.Security.Cryptography.DES
      System.Security.Cryptography.DESCryptoServiceProvider
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.