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