天天看點

資料加密

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,如需轉載請自行聯系原作者

繼續閱讀