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
}
}