Database username and password encryption and decryption application Series

Source: Internet
Author: User

Public class desencrypt
{
Public desencrypt ()
{
}

# Region ========= encryption ========
 
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static string encrypt (string text)
{
Return encrypt (text, "litianping ");
}
/// <Summary>
/// Encrypt data
/// </Summary>
/// <Param name = "text"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string encrypt (string text, string key)
{
// Define data encryption standards
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray;
Des. Key = asciiencoding. ASCII. getbytes (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (key, "MD5"). substring (0, 8 ));
Des. IV = asciiencoding. ASCII. getbytes (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (key, "MD5"). substring (0, 8 ));
System. Io. memorystream MS = new system. Io. memorystream ();
// Encrypted stream: written
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}
Return ret. tostring ();
}

# Endregion

# Region ========= decryption ========

 
/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static string decrypt (string text)
{
Return decrypt (text, "litianping ");
}
/// <Summary>
/// Decrypt data
/// </Summary>
/// <Param name = "text"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string decrypt (string text, string skey)
{
// Encryption/Decryption class
Descryptoserviceprovider des = new descryptoserviceprovider ();
Int Len;
Len = text. Length/2;
Byte [] inputbytearray = new byte [Len];
Int X, I;
For (x = 0; x <Len; X ++)
{
I = convert. toint32 (text. substring (x * 2, 2), 16 );
Inputbytearray [x] = (byte) I;
}
Des. Key = asciiencoding. ASCII. getbytes (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"). substring (0, 8 ));
Des. IV = asciiencoding. ASCII. getbytes (system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (skey, "MD5"). substring (0, 8 ));
System. Io. memorystream MS = new system. Io. memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return encoding. Default. getstring (Ms. toarray ());
}
 
# Endregion

}
}

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.