C # symmetric algorithm Encryption

Source: Internet
Author: User
Content Overview: Title
Language used: C #
Difficulty: Medium
Using system;
Using system. Security. cryptography;
Using system. IO;
Using system. text;

Namespace netoffice. Component
{
/// <Summary>
/// Summary of encryt.
/// </Summary>
Public class encryt
{
Private response ricalgorithm MCSP = new descryptoserviceprovider ();
// Public encryt ()
//{
//
//}
/// <Summary>
/// Constructor with Parameters
/// </Summary>
/// <Param name = "key"> set the secret key of the symmetric algorithm </param>
/// <Param name = "IV"> set the initialization vector of the symmetric algorithm </param>
Public encryt (string key, string IV)
{
MCSP. Key = convert. frombase64string (key );
MCSP. IV = convert. frombase64string (IV );
}

// Encryption function
Public String encrystr (string encryed)
{

Icryptotransform CT; // defines the basic encryption and conversion operations
Memorystream MS; // creates a stream that supports the storage as memory
Cryptostream Cs; // defines the stream that links the data stream to the encrypted conversion stream.
Byte [] BYT;

// Create a symmetric encryptor object using the specified key and initialization Vector
Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );

// Convert incoming values into byte Arrays
Byt = encoding. utf8.getbytes (encryed );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );

CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return convert. tobase64string (Ms. toarray ());

}

// Decryption Function
Public String denstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
// Create a symmetric cipher object
Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
Byt = convert. frombase64string (value );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return encoding. utf8.getstring (Ms. toarray ());

}

}
}

<Deleetask>
<Add key = "connectionstring" value = "Server =.; uid = sa; database = netoffice"/>
<Add key = "mykey" value = "Til + b4fj3g4 ="/>
<Add key = "myiv" value = "+ hz8mzpg5c ="/>

</Appsettings>

 

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.