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.