Encryption and decryption problems (keys)

Source: Internet
Author: User

When configuring the website database, it is often difficult to encrypt the appsonstring of the ettings in Web. config. The common encryption method is DES encryption/decryption.

An error is always reported during website configuration today. The error is as follows:

During this period, I found a lot of information on the Internet, but I didn't want it. Later I found that the keys used for encryption and decryption were different. I was depressed for a long time.

# Region ========= encryption ========

/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public static string encrypt (string text)
{
Return encrypt (text, Key );
}
/// <Summary>
/// Encrypt data
/// </Summary>
/// <Param name = "text"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string encrypt (string text, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray;
Inputbytearray = encoding. Default. getbytes (text );
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. 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, Key );
}
/// <Summary>
/// Decrypt data
/// </Summary>
/// <Param name = "text"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public static string decrypt (string text, string skey)
{
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.