C # encryption/decryption process encapsulated source code

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Security. cryptography;
Using system. IO;
Using system. xml;

Namespace myquery. utils
{
/// <Summary>
/// Encryption/Decryption processing tool
/// By Jia Shiyi 2009-3-5
/// </Summary>
Public static class encrypthelper
{
/// <Summary>
/// Obtain the MD5 encrypted string (irreversible)
/// </Summary>
/// <Param name = "password"> plaintext </param>
/// <Returns> </returns>
Public static string GetPassword (string password)
{
String result = "";
MD5 MD5 = md5.create (); // instantiate an MD5 object
// After encryption, it is an array of the byte type. Pay attention to the selection of UTF-8/Unicode encoding.
Byte [] S = md5.computehash (encoding. utf8.getbytes (password ));
// Convert an array of the byte type into a string by using a loop. This string is obtained by regular character formatting.
For (INT I = 0; I <S. length; I ++)
{
// Use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (x) are used, the characters in the format are uppercase letters.
Result = Result + s [I]. tostring ("X ");
}
Return result;
}
/// <Summary>
/// Obtain the DES encrypted string
/// </Summary>
/// <Param name = "data"> characters to be encrypted </param>
/// <Param name = "keycode1"> key </param>
/// <Param name = "keycode2"> key </param>
/// <Returns> </returns>
Public static string desencode (string data, string keycode1, string keycode2)
{
Byte [] bykey = getkey (keycode1 );
Byte [] byiv = getkey (keycode2 );

Descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
Int I = cryptoprovider. keysize;
Memorystream MS = new memorystream ();
Cryptostream CST = new cryptostream (MS, cryptoprovider. createencryptor (bykey, byiv), cryptostreammode. Write );

Streamwriter Sw = new streamwriter (Cst );
Sw. Write (data );
Sw. Flush ();
CST. flushfinalblock ();
Sw. Flush ();
Return convert. tobase64string (Ms. getbuffer (), 0, (INT) ms. Length );

}

/// <Summary>
/// Obtain the decrypted des string
/// </Summary>
/// <Param name = "data"> string to be decrypted </param>
/// <Param name = "keycode1"> key </param>
/// <Param name = "keycode2"> key </param>
/// <Returns> </returns>
Public static string desdecode (string data, string keycode1, string keycode2)
{
Byte [] bykey = getkey (keycode1 );
Byte [] byiv = getkey (keycode2 );

Byte [] byenc;
Try
{
Byenc = convert. frombase64string (data );
}
Catch
{
Return NULL;
}

Descryptoserviceprovider cryptoprovider = new descryptoserviceprovider ();
Memorystream MS = new memorystream (byenc );
Cryptostream CST = new cryptostream (MS, cryptoprovider. createdecryptor (bykey, byiv), cryptostreammode. Read );
Streamreader sr = new streamreader (Cst );
Return Sr. readtoend ();
}
}
}

Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.

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.