天天看点

数据加密

How To: Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key 

数据加密

C# code

数据加密
数据加密

[printer-friendly version] [code output] 

数据加密

///////////////////////////////////////////////////////////////////////////////

数据加密

// SAMPLE: Symmetric key encryption and decryption using Rijndael algorithm.

数据加密

// 

数据加密

// To run this sample, create a new Visual C# project using the Console

数据加密

// Application template and replace the contents of the Class1.cs file with

数据加密

// the code below.

数据加密

//

数据加密

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 

数据加密

// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 

数据加密

// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

数据加密
数据加密

// Copyright (C) 2003.  Obviex(TM).  All rights reserved.

数据加密
数据加密

using System;

数据加密

using System.IO;

数据加密

using System.Text;

数据加密

using System.Security.Cryptography;

数据加密
数据加密

/// <summary>

数据加密

/// This class uses a symmetric key algorithm (Rijndael/AES) to encrypt and 

数据加密

/// decrypt data. As long as encryption and decryption routines use the same

数据加密

/// parameters to generate the keys, the keys are guaranteed to be the same.

数据加密

/// The class uses static functions with duplicate code to make it easier to

数据加密

/// demonstrate encryption and decryption logic. In a real-life application, 

数据加密

/// this may not be the most efficient way of handling encryption, so - as

数据加密

/// soon as you feel comfortable with it - you may want to redesign this class.

数据加密

/// </summary>

数据加密

public class RijndaelSimple

数据加密

{

数据加密

    /// <summary>

数据加密

    /// Encrypts specified plaintext using Rijndael symmetric key algorithm

数据加密

    /// and returns a base64-encoded result.

数据加密

    /// </summary>

数据加密

    /// <param name="plainText">

数据加密

    /// Plaintext value to be encrypted.

数据加密

    /// </param>

数据加密

    /// <param name="passPhrase">

数据加密

    /// Passphrase from which a pseudo-random password will be derived. The

数据加密

    /// derived password will be used to generate the encryption key.

数据加密

    /// Passphrase can be any string. In this example we assume that this

数据加密

    /// passphrase is an ASCII string.

数据加密
数据加密

    /// <param name="saltValue">

数据加密

    /// Salt value used along with passphrase to generate password. Salt can

数据加密

    /// be any string. In this example we assume that salt is an ASCII string.

数据加密
数据加密

    /// <param name="hashAlgorithm">

数据加密

    /// Hash algorithm used to generate password. Allowed values are: "MD5" and

数据加密

    /// "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.

数据加密
数据加密

    /// <param name="passwordIterations">

数据加密

    /// Number of iterations used to generate password. One or two iterations

数据加密

    /// should be enough.

数据加密
数据加密

    /// <param name="initVector">

数据加密

    /// Initialization vector (or IV). This value is required to encrypt the

数据加密

    /// first block of plaintext data. For RijndaelManaged class IV must be 

数据加密

    /// exactly 16 ASCII characters long.

数据加密
数据加密

    /// <param name="keySize">

数据加密

    /// Size of encryption key in bits. Allowed values are: 128, 192, and 256. 

数据加密

    /// Longer keys are more secure than shorter keys.

数据加密
数据加密

    /// <returns>

数据加密

    /// Encrypted value formatted as a base64-encoded string.

数据加密

    /// </returns>

数据加密

    public static string Encrypt(string   plainText,

数据加密

                                 string   passPhrase,

数据加密

                                 string   saltValue,

数据加密

                                 string   hashAlgorithm,

数据加密

                                 int      passwordIterations,

数据加密

                                 string   initVector,

数据加密

                                 int      keySize)

数据加密

    {

数据加密

        // Convert strings into byte arrays.

数据加密

        // Let us assume that strings only contain ASCII codes.

数据加密

        // If strings include Unicode characters, use Unicode, UTF7, or UTF8 

数据加密

        // encoding.

数据加密

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);

数据加密

        byte[] saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);

数据加密
数据加密

        // Convert our plaintext into a byte array.

数据加密

        // Let us assume that plaintext contains UTF8-encoded characters.

数据加密

        byte[] plainTextBytes  = Encoding.UTF8.GetBytes(plainText);

数据加密
数据加密

        // First, we must create a password, from which the key will be derived.

数据加密

        // This password will be generated from the specified passphrase and 

数据加密

        // salt value. The password will be created using the specified hash 

数据加密

        // algorithm. Password creation can be done in several iterations.

数据加密

        PasswordDeriveBytes password = new PasswordDeriveBytes(

数据加密

                                                        passPhrase, 

数据加密

                                                        saltValueBytes, 

数据加密

                                                        hashAlgorithm, 

数据加密

                                                        passwordIterations);

数据加密
数据加密

        // Use the password to generate pseudo-random bytes for the encryption

数据加密

        // key. Specify the size of the key in bytes (instead of bits).

数据加密

        byte[] keyBytes = password.GetBytes(keySize / 8);

数据加密
数据加密

        // Create uninitialized Rijndael encryption object.

数据加密

        RijndaelManaged symmetricKey = new RijndaelManaged();

数据加密
数据加密

        // It is reasonable to set encryption mode to Cipher Block Chaining

数据加密

        // (CBC). Use default options for other symmetric key parameters.

数据加密

        symmetricKey.Mode = CipherMode.CBC;        

数据加密
数据加密

        // Generate encryptor from the existing key bytes and initialization 

数据加密

        // vector. Key size will be defined based on the number of the key 

数据加密

        // bytes.

数据加密

        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(

数据加密

                                                         keyBytes, 

数据加密

                                                         initVectorBytes);

数据加密
数据加密

        // Define memory stream which will be used to hold encrypted data.

数据加密

        MemoryStream memoryStream = new MemoryStream();        

数据加密
数据加密

        // Define cryptographic stream (always use Write mode for encryption).

数据加密

        CryptoStream cryptoStream = new CryptoStream(memoryStream, 

数据加密

                                                     encryptor,

数据加密

                                                     CryptoStreamMode.Write);

数据加密

        // Start encrypting.

数据加密

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

数据加密
数据加密

        // Finish encrypting.

数据加密

        cryptoStream.FlushFinalBlock();

数据加密
数据加密

        // Convert our encrypted data from a memory stream into a byte array.

数据加密

        byte[] cipherTextBytes = memoryStream.ToArray();

数据加密
数据加密

        // Close both streams.

数据加密

        memoryStream.Close();

数据加密

        cryptoStream.Close();

数据加密
数据加密

        // Convert encrypted data into a base64-encoded string.

数据加密

        string cipherText = Convert.ToBase64String(cipherTextBytes);

数据加密
数据加密

        // Return encrypted string.

数据加密

        return cipherText;

数据加密

    }

数据加密
数据加密
数据加密

    /// Decrypts specified ciphertext using Rijndael symmetric key algorithm.

数据加密
数据加密

    /// <param name="cipherText">

数据加密

    /// Base64-formatted ciphertext value.

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

    /// first block of plaintext data. For RijndaelManaged class IV must be

数据加密
数据加密
数据加密
数据加密

    /// Size of encryption key in bits. Allowed values are: 128, 192, and 256.

数据加密
数据加密
数据加密
数据加密

    /// Decrypted string value.

数据加密
数据加密

    /// <remarks>

数据加密

    /// Most of the logic in this function is similar to the Encrypt

数据加密

    /// logic. In order for decryption to work, all parameters of this function

数据加密

    /// - except cipherText value - must match the corresponding parameters of

数据加密

    /// the Encrypt function which was called to generate the

数据加密

    /// ciphertext.

数据加密

    /// </remarks>

数据加密

    public static string Decrypt(string   cipherText,

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        // Convert strings defining encryption key characteristics into byte

数据加密

        // arrays. Let us assume that strings only contain ASCII codes.

数据加密

        // If strings include Unicode characters, use Unicode, UTF7, or UTF8

数据加密
数据加密
数据加密
数据加密
数据加密

        // Convert our ciphertext into a byte array.

数据加密

        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

数据加密
数据加密

        // First, we must create a password, from which the key will be 

数据加密

        // derived. This password will be generated from the specified 

数据加密

        // passphrase and salt value. The password will be created using

数据加密

        // the specified hash algorithm. Password creation can be done in

数据加密

        // several iterations.

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        RijndaelManaged    symmetricKey = new RijndaelManaged();

数据加密
数据加密
数据加密
数据加密

        symmetricKey.Mode = CipherMode.CBC;

数据加密
数据加密

        // Generate decryptor from the existing key bytes and initialization 

数据加密
数据加密
数据加密

        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(

数据加密
数据加密
数据加密
数据加密
数据加密

        MemoryStream  memoryStream = new MemoryStream(cipherTextBytes);

数据加密
数据加密

        // Define cryptographic stream (always use Read mode for encryption).

数据加密

        CryptoStream  cryptoStream = new CryptoStream(memoryStream, 

数据加密

                                                      decryptor,

数据加密

                                                      CryptoStreamMode.Read);

数据加密
数据加密

        // Since at this point we don't know what the size of decrypted data

数据加密

        // will be, allocate the buffer long enough to hold ciphertext;

数据加密

        // plaintext is never longer than ciphertext.

数据加密

        byte[] plainTextBytes = new byte[cipherTextBytes.Length];

数据加密
数据加密

        // Start decrypting.

数据加密

        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 

数据加密

                                                   0, 

数据加密

                                                   plainTextBytes.Length);

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        // Convert decrypted data into a string. 

数据加密

        // Let us assume that the original plaintext string was UTF8-encoded.

数据加密

        string plainText = Encoding.UTF8.GetString(plainTextBytes, 

数据加密
数据加密

                                                   decryptedByteCount);

数据加密
数据加密

        // Return decrypted string.   

数据加密

        return plainText;

数据加密
数据加密

}

数据加密
数据加密
数据加密

/// Illustrates the use of RijndaelSimple class to encrypt and decrypt data.

数据加密
数据加密

public class RijndaelSimpleTest

数据加密
数据加密
数据加密

    /// The main entry point for the application.

数据加密
数据加密

    [STAThread]

数据加密

    static void Main(string[] args)

数据加密
数据加密

        string   plainText          = "Hello, World!";    // original plaintext

数据加密
数据加密

        string   passPhrase         = "Pas5pr@se";        // can be any string

数据加密

        string   saltValue          = "s@1tValue";        // can be any string

数据加密

        string   hashAlgorithm      = "SHA1";             // can be "MD5"

数据加密

        int      passwordIterations = 2;                  // can be any number

数据加密

        string   initVector         = "@1B2c3D4e5F6g7H8"; // must be 16 bytes

数据加密

        int      keySize            = 256;                // can be 192 or 128

数据加密
数据加密

        Console.WriteLine(String.Format("Plaintext : {0}", plainText));

数据加密
数据加密

        string  cipherText = RijndaelSimple.Encrypt(plainText,

数据加密

                                                    passPhrase,

数据加密

                                                    saltValue,

数据加密

                                                    hashAlgorithm,

数据加密

                                                    passwordIterations,

数据加密

                                                    initVector,

数据加密

                                                    keySize);

数据加密
数据加密

        Console.WriteLine(String.Format("Encrypted : {0}", cipherText));

数据加密
数据加密

        plainText          = RijndaelSimple.Decrypt(cipherText,

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        Console.WriteLine(String.Format("Decrypted : {0}", plainText));

数据加密
数据加密
数据加密
数据加密

// END OF FILE

数据加密
数据加密

^ Back to top  

数据加密
数据加密
数据加密
数据加密

--------------------------------------------------------------------------------

数据加密
数据加密
数据加密

VB.NET code

数据加密
数据加密
数据加密

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

数据加密

' SAMPLE: Symmetric key encryption and decryption using Rijndael algorithm.

数据加密

数据加密

' To run this sample, create a new Visual Basic.NET project using the Console 

数据加密

' Application template and replace the contents of the Module1.vb file with 

数据加密

' the code below.

数据加密
数据加密

' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 

数据加密

' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 

数据加密

' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

数据加密
数据加密

' Copyright (C) 2003.  Obviex(TM).  All rights reserved.

数据加密

'

数据加密

Imports System

数据加密

Imports System.IO

数据加密

Imports System.Text

数据加密

Imports System.Security.Cryptography

数据加密
数据加密

Module Module1

数据加密
数据加密

' <summary>

数据加密

' This class uses a symmetric key algorithm (Rijndael/AES) to encrypt and 

数据加密

' decrypt data. As long as encryption and decryption routines use the same 

数据加密

' parameters to generate the keys, the keys are guaranteed to be the same.

数据加密

' The class uses static functions with duplicate code to make it easier to 

数据加密

' demonstrate encryption and decryption logic. In a real-life application, 

数据加密

' this may not be the most efficient way of handling encryption, so - as 

数据加密

' soon as you feel comfortable with it - you may want to redesign this class.

数据加密

' </summary>

数据加密

Public Class RijndaelSimple

数据加密
数据加密

    ' <summary>

数据加密

    ' Encrypts specified plaintext using Rijndael symmetric key algorithm

数据加密

    ' and returns a base64-encoded result.

数据加密

    ' </summary>

数据加密

    ' <param name="plainText">

数据加密

    ' Plaintext value to be encrypted.

数据加密

    ' </param>

数据加密

    ' <param name="passPhrase">

数据加密

    ' Passphrase from which a pseudo-random password will be derived. The 

数据加密

    ' derived password will be used to generate the encryption key. 

数据加密

    ' Passphrase can be any string. In this example we assume that this 

数据加密

    ' passphrase is an ASCII string.

数据加密
数据加密

    ' <param name="saltValue">

数据加密

    ' Salt value used along with passphrase to generate password. Salt can 

数据加密

    ' be any string. In this example we assume that salt is an ASCII string.

数据加密
数据加密

    ' <param name="hashAlgorithm">

数据加密

    ' Hash algorithm used to generate password. Allowed values are: "MD5" and

数据加密

    ' "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes.

数据加密
数据加密

    ' <param name="passwordIterations">

数据加密

    ' Number of iterations used to generate password. One or two iterations

数据加密

    ' should be enough.

数据加密
数据加密

    ' <param name="initVector">

数据加密

    ' Initialization vector (or IV). This value is required to encrypt the 

数据加密

    ' first block of plaintext data. For RijndaelManaged class IV must be 

数据加密

    ' exactly 16 ASCII characters long.

数据加密
数据加密

    ' <param name="keySize">

数据加密

    ' Size of encryption key in bits. Allowed values are: 128, 192, and 256. 

数据加密

    ' Longer keys are more secure than shorter keys.

数据加密
数据加密

    ' <returns>

数据加密

    ' Encrypted value formatted as a base64-encoded string.

数据加密

    ' </returns>

数据加密

    Public Shared Function Encrypt(ByVal plainText           As String,  _

数据加密

                                   ByVal passPhrase          As String,  _

数据加密

                                   ByVal saltValue           As String,  _

数据加密

                                   ByVal hashAlgorithm       As String,  _

数据加密

                                   ByVal passwordIterations  As Integer, _

数据加密

                                   ByVal initVector          As String,  _

数据加密

                                   ByVal keySize             As Integer) _

数据加密

                           As String

数据加密
数据加密

        ' Convert strings into byte arrays.

数据加密

        ' Let us assume that strings only contain ASCII codes.

数据加密

        ' If strings include Unicode characters, use Unicode, UTF7, or UTF8 

数据加密

        ' encoding.

数据加密

        Dim initVectorBytes As Byte() 

数据加密

        initVectorBytes = Encoding.ASCII.GetBytes(initVector)

数据加密
数据加密

        Dim saltValueBytes As Byte()

数据加密

        saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

数据加密
数据加密

        ' Convert our plaintext into a byte array.

数据加密

        ' Let us assume that plaintext contains UTF8-encoded characters.

数据加密

        Dim plainTextBytes As Byte()

数据加密

        plainTextBytes = Encoding.UTF8.GetBytes(plainText)

数据加密
数据加密

        ' First, we must create a password, from which the key will be derived.

数据加密

        ' This password will be generated from the specified passphrase and 

数据加密

        ' salt value. The password will be created using the specified hash 

数据加密

        ' algorithm. Password creation can be done in several iterations.

数据加密

        Dim password As PasswordDeriveBytes

数据加密

        password = new PasswordDeriveBytes(passPhrase,     _

数据加密

                                           saltValueBytes, _ 

数据加密

                                           hashAlgorithm,  _

数据加密

                                           passwordIterations)

数据加密
数据加密

        ' Use the password to generate pseudo-random bytes for the encryption

数据加密

        ' key. Specify the size of the key in bytes (instead of bits).

数据加密

        Dim keyBytes As Byte()

数据加密

        keyBytes = password.GetBytes(keySize / 8)

数据加密
数据加密

        ' Create uninitialized Rijndael encryption object.

数据加密

        Dim symmetricKey As RijndaelManaged 

数据加密

        symmetricKey = new RijndaelManaged()

数据加密
数据加密

        ' It is reasonable to set encryption mode to Cipher Block Chaining

数据加密

        ' (CBC). Use default options for other symmetric key parameters.

数据加密

        symmetricKey.Mode = CipherMode.CBC        

数据加密
数据加密

        ' Generate encryptor from the existing key bytes and initialization 

数据加密

        ' vector. Key size will be defined based on the number of the key 

数据加密

        ' bytes.

数据加密

        Dim encryptor As ICryptoTransform 

数据加密

        encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

数据加密
数据加密

        ' Define memory stream which will be used to hold encrypted data.

数据加密

        Dim memoryStream As MemoryStream 

数据加密

        memoryStream = new MemoryStream()        

数据加密
数据加密

        ' Define cryptographic stream (always use Write mode for encryption).

数据加密

        Dim cryptoStream As CryptoStream

数据加密

        cryptoStream = new CryptoStream(memoryStream, _ 

数据加密

                                        encryptor,    _

数据加密

                                        CryptoStreamMode.Write)

数据加密

        ' Start encrypting.

数据加密

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)

数据加密
数据加密

        ' Finish encrypting.

数据加密

        cryptoStream.FlushFinalBlock()

数据加密
数据加密

        ' Convert our encrypted data from a memory stream into a byte array.

数据加密

        Dim cipherTextBytes As Byte() 

数据加密

        cipherTextBytes = memoryStream.ToArray()

数据加密
数据加密

        ' Close both streams.

数据加密

        memoryStream.Close()

数据加密

        cryptoStream.Close()

数据加密
数据加密

        ' Convert encrypted data into a base64-encoded string.

数据加密

        Dim cipherText As String 

数据加密

        cipherText = Convert.ToBase64String(cipherTextBytes)

数据加密
数据加密

        ' Return encrypted string.

数据加密

        Encrypt = cipherText

数据加密

    End Function

数据加密
数据加密
数据加密

    ' Decrypts specified ciphertext using Rijndael symmetric key algorithm.

数据加密
数据加密

    ' <param name="cipherText">

数据加密

    ' Base64-formatted ciphertext value.

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

    ' Decrypted string value.

数据加密
数据加密

    ' <remarks>

数据加密

    ' Most of the logic in this function is similar to the Encrypt 

数据加密

    ' logic. In order for decryption to work, all parameters of this function

数据加密

    ' - except cipherText value - must match the corresponding parameters of 

数据加密

    ' the Encrypt function which was called to generate the 

数据加密

    ' ciphertext.

数据加密

    ' </remarks>

数据加密

    Public Shared Function Decrypt(ByVal cipherText          As String,  _

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        ' Convert strings defining encryption key characteristics into byte

数据加密

        ' arrays. Let us assume that strings only contain ASCII codes.

数据加密

        ' If strings include Unicode characters, use Unicode, UTF7, or UTF8

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        ' Convert our ciphertext into a byte array.

数据加密
数据加密

        cipherTextBytes = Convert.FromBase64String(cipherText)

数据加密
数据加密

        ' First, we must create a password, from which the key will be 

数据加密

        ' derived. This password will be generated from the specified 

数据加密

        ' passphrase and salt value. The password will be created using

数据加密

        ' the specified hash algorithm. Password creation can be done in

数据加密

        ' several iterations.

数据加密

        Dim password As PasswordDeriveBytes 

数据加密
数据加密

                                           saltValueBytes, _

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        Dim keyBytes As Byte() 

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        symmetricKey.Mode = CipherMode.CBC

数据加密
数据加密

        ' Generate decryptor from the existing key bytes and initialization 

数据加密
数据加密
数据加密

        Dim decryptor As ICryptoTransform 

数据加密

        decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

数据加密
数据加密
数据加密

        Dim memoryStream As MemoryStream  

数据加密

        memoryStream = new MemoryStream(cipherTextBytes)

数据加密
数据加密
数据加密

        Dim cryptoStream As CryptoStream  

数据加密

        cryptoStream = new CryptoStream(memoryStream, _

数据加密

                                        decryptor,    _

数据加密

                                        CryptoStreamMode.Read)

数据加密
数据加密

        ' Since at this point we don't know what the size of decrypted data

数据加密

        ' will be, allocate the buffer long enough to hold ciphertext;

数据加密

        ' plaintext is never longer than ciphertext.

数据加密

        Dim plainTextBytes As Byte() 

数据加密

        ReDim plainTextBytes(cipherTextBytes.Length)

数据加密
数据加密

        ' Start decrypting.

数据加密

        Dim decryptedByteCount As Integer 

数据加密

        decryptedByteCount = cryptoStream.Read(plainTextBytes, _

数据加密

                                               0,              _

数据加密

                                               plainTextBytes.Length)

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

        ' Convert decrypted data into a string. 

数据加密

        ' Let us assume that the original plaintext string was UTF8-encoded.

数据加密

        Dim plainText As String 

数据加密

        plainText = Encoding.UTF8.GetString(plainTextBytes, _

数据加密

                                            0, _

数据加密

                                            decryptedByteCount)

数据加密
数据加密

        ' Return decrypted string.

数据加密

        Decrypt = plainText

数据加密
数据加密

End Class

数据加密
数据加密
数据加密

' The main entry point for the application.

数据加密
数据加密

Sub Main()

数据加密

    Dim plainText          As String

数据加密

    Dim cipherText         As String

数据加密
数据加密

    Dim passPhrase         As String

数据加密

    Dim saltValue          As String

数据加密

    Dim hashAlgorithm      As String

数据加密

    Dim passwordIterations As Integer

数据加密

    Dim initVector         As String

数据加密

    Dim keySize            As Integer

数据加密
数据加密

    plainText          = "Hello, World!"    ' original plaintext

数据加密
数据加密

    passPhrase         = "Pas5pr@se"        ' can be any string

数据加密

    saltValue          = "s@1tValue"        ' can be any string

数据加密

    hashAlgorithm      = "SHA1"             ' can be "MD5"

数据加密

    passwordIterations = 2                  ' can be any number

数据加密

    initVector         = "@1B2c3D4e5F6g7H8" ' must be 16 bytes

数据加密

    keySize            = 256                ' can be 192 or 128

数据加密
数据加密

    Console.WriteLine(String.Format("Plaintext : {0}", plainText))

数据加密
数据加密

    cipherText = RijndaelSimple.Encrypt(plainText,          _

数据加密

                                        passPhrase,         _

数据加密

                                        saltValue,          _

数据加密

                                        hashAlgorithm,      _

数据加密

                                        passwordIterations, _

数据加密

                                        initVector,         _

数据加密

                                        keySize)

数据加密
数据加密

    Console.WriteLine(String.Format("Encrypted : {0}", cipherText))

数据加密
数据加密

    plainText  = RijndaelSimple.Decrypt(cipherText,         _

数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密
数据加密

    Console.WriteLine(String.Format("Decrypted : {0}", plainText))

数据加密

End Sub

数据加密
数据加密

End Module

数据加密
数据加密

' END OF FILE

数据加密
数据加密

本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2004/08/25/36320.html,如需转载请自行联系原作者

继续阅读