Asp.net, C # detailed description of the use of encryption and decryption strings

Source: Internet
Author: User

First, add the following code under the web. config | app. config file:

Copy codeThe Code is as follows: <? Xml version = "1.0"?>
<Configuration>
<Deleetask>
<Add key = "IV" value = "SuFjcEmp/TE ="/>
<Add key = "Key" value = "KIPSToILGp6fl + javasxjvmsn4iajizybbt"/>
</AppSettings>
</Configuration>

IV: the initial vector of the encryption algorithm.

Key: the Key of the encryption algorithm.

Next, create a class.CryptoHelperAs the encryption help class.

Obtain the IV and Key from the configuration file. The basic code is as follows:

Copy codeThe Code is as follows: public class CryptoHelper
{
// Private readonly string IV = "SuFjcEmp/TE = ";
Private readonly string IV = string. Empty;
// Private readonly string Key = "KIPSToILGp6fl + javasxjvmsn4iajizybbt ";
Private readonly string Key = string. Empty;

/// <Summary>
/// Constructor
/// </Summary>
Public CryptoHelper ()
{
IV = ConfigurationManager. receivettings ["IV"];
Key = ConfigurationManager. receivettings ["Key"];
}
}

Note: add System. Configuration. dll Assembly reference.
After obtaining the IV and Key, you need to obtain the Service class that provides the encryption Service.

Here, we use the TripleDESCryptoServiceProvider class under the System. Security. Cryptography; namespace.

The method for getting TripleDESCryptoServiceProvider is as follows:

Copy codeThe Code is as follows: // <summary>
/// Obtain the encryption service class
/// </Summary>
/// <Returns> </returns>
Private TripleDESCryptoServiceProvider GetCryptoProvider ()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider ();

Provider. IV = Convert. FromBase64String (IV );
Provider. Key = Convert. FromBase64String (Key );

Return provider;
}

TripleDESCryptoServiceProvider two useful methods

CreateEncryptor: Creates the symmetric encryptor object ICryptoTransform.

CreateDecryptor: Create the symmetric encryptor object ICryptoTransform

The encryptor object and the encryptor object can be used by the CryptoStream object. To encrypt and decrypt streams.

The constructor of cryptoStream is as follows:

Public CryptoStream (Stream stream, ICryptoTransform transform, CryptoStreamMode mode );

Use a transform object to convert streams.

The complete encryption string code is as follows:

Copy codeThe Code is as follows: // <summary>
/// Obtain the encrypted string
/// </Summary>
/// <Param name = "inputValue"> input value. </param>
/// <Returns> </returns>
Public string GetEncryptedValue (string inputValue)
{
TripleDESCryptoServiceProvider provider = this. GetCryptoProvider ();

// Create a memory stream to save the encrypted stream
MemoryStream mStream = new MemoryStream ();

// Create an encrypted conversion stream
CryptoStream cStream = new CryptoStream (mStream,
Provider. CreateEncryptor (), CryptoStreamMode. Write );

// Use UTF8 encoding to obtain the bytes of the input string.
Byte [] toEncrypt = new UTF8Encoding (). GetBytes (inputValue );

// Write the bytes to the conversion stream.
CStream. Write (toEncrypt, 0, toEncrypt. Length );
CStream. FlushFinalBlock ();

// After the FlushFinalBlock method of the conversion stream is called, the conversion is performed internally. In this case, mStream is the encrypted stream.
Byte [] ret = mStream. ToArray ();

// Close the streams.
CStream. Close ();
MStream. Close ();

// 64 encode the encrypted bytes.
Return Convert. ToBase64String (ret );
}

The decryption method is also similar:Copy codeThe Code is as follows: // <summary>
/// Obtain the decrypted Value
/// </Summary>
/// <Param name = "inputValue"> encrypted string. </param>
/// <Returns> </returns>
Public string GetDecryptedValue (string inputValue)
{
TripleDESCryptoServiceProvider provider = this. GetCryptoProvider ();

Byte [] inputEquivalent = Convert. FromBase64String (inputValue );

// Create a memory stream to save the decrypted data
MemoryStream msDecrypt = new MemoryStream ();

// Create a conversion stream.
CryptoStream csDecrypt = new CryptoStream (msDecrypt,
Provider. CreateDecryptor (),
CryptoStreamMode. Write );

CsDecrypt. Write (inputEquivalent, 0, inputEquivalent. Length );

CsDecrypt. FlushFinalBlock ();
CsDecrypt. Close ();

// Obtain the string.
Return new UTF8Encoding (). GetString (msDecrypt. ToArray ());
}

The complete CryptoHelper code is as follows:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Security. Cryptography;
Using System. IO;
Using System. Configuration;

Namespace WindowsFormsApplication1
{
Public class CryptoHelper
{
// Private readonly string IV = "SuFjcEmp/TE = ";
Private readonly string IV = string. Empty;
// Private readonly string Key = "KIPSToILGp6fl + javasxjvmsn4iajizybbt ";
Private readonly string Key = string. Empty;

Public CryptoHelper ()
{
IV = ConfigurationManager. receivettings ["IV"];
Key = ConfigurationManager. receivettings ["Key"];
}

/// <Summary>
/// Obtain the encrypted string
/// </Summary>
/// <Param name = "inputValue"> input value. </param>
/// <Returns> </returns>
Public string GetEncryptedValue (string inputValue)
{
TripleDESCryptoServiceProvider provider = this. GetCryptoProvider ();

// Create a memory stream to save the encrypted stream
MemoryStream mStream = new MemoryStream ();

// Create an encrypted conversion stream
CryptoStream cStream = new CryptoStream (mStream,

Provider. CreateEncryptor (), CryptoStreamMode. Write );
// Use UTF8 encoding to obtain the bytes of the input string.
Byte [] toEncrypt = new UTF8Encoding (). GetBytes (inputValue );

// Write the bytes to the conversion stream.
CStream. Write (toEncrypt, 0, toEncrypt. Length );
CStream. FlushFinalBlock ();

// After the FlushFinalBlock method of the conversion stream is called, the conversion is performed internally. In this case, mStream is the encrypted stream.
Byte [] ret = mStream. ToArray ();

// Close the streams.
CStream. Close ();
MStream. Close ();

// 64 encode the encrypted bytes.
Return Convert. ToBase64String (ret );
}

/// <Summary>
/// Obtain the encryption service class
/// </Summary>
/// <Returns> </returns>
Private TripleDESCryptoServiceProvider GetCryptoProvider ()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider ();

Provider. IV = Convert. FromBase64String (IV );
Provider. Key = Convert. FromBase64String (Key );

Return provider;

}

/// <Summary>
/// Obtain the decrypted Value
/// </Summary>
/// <Param name = "inputValue"> encrypted string. </param>
/// <Returns> </returns>
Public string GetDecryptedValue (string inputValue)
{
TripleDESCryptoServiceProvider provider = this. GetCryptoProvider ();
Byte [] inputEquivalent = Convert. FromBase64String (inputValue );

// Create a memory stream to save the decrypted data
MemoryStream msDecrypt = new MemoryStream ();

// Create a conversion stream.
CryptoStream csDecrypt = new CryptoStream (msDecrypt,
Provider. CreateDecryptor (),
CryptoStreamMode. Write );

CsDecrypt. Write (inputEquivalent, 0, inputEquivalent. Length );
CsDecrypt. FlushFinalBlock ();

CsDecrypt. Close ();

// Obtain the string.
Return new UTF8Encoding (). GetString (msDecrypt. ToArray ());
}
}
}

Example:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.