Encrypt (encryption and decryption)

Source: Internet
Author: User

Using system;
Using system. Security. cryptography;
Using system. text;
Using system. IO;

Public class Encrypt
{
Public encrypt ()
{}

# Region MD5 Encryption
/// <Summary>
/// MD5 Encryption
/// </Summary>
Public static string md5encrypt (string text)
{
Return md5encrypt (text, "litianping ");
}

/// <Summary>
/// MD5 Encryption
/// </Summary>
Public static string md5encrypt (string text, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] 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 MD5 decryption
/// <Summary>
/// MD5 decryption
/// </Summary>
Public static string md5decrypt (string text)
{
Return md5decrypt (text, "litianping ");
}

/// <Summary>
/// MD5 decryption
/// </Summary>
Public static string md5decrypt (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

# Region tripledes Encryption
/// <Summary>
/// Tripledes Encryption
/// </Summary>
Public static string tripledesencrypting (string strsource)
{
Try
{
Byte [] bytin = encoding. Default. getbytes (strsource );
Byte [] Key = {42, 16, 93,156, 78, 4,218, 32, 15,167, 44, 80, 26, 20,155,112, 2, 94, 11,204,119, 35,184,197 }; // define the key
Byte [] IV = {55,103,246, 79, 36, 99,167, 3}; // defines the offset.
Tripledescryptoserviceprovider tripledes = new tripledescryptoserviceprovider ();
Tripledes. IV = IV;
Tripledes. Key = key;
Icryptotransform encrypto = tripledes. createencryptor ();
System. Io. memorystream MS = new system. Io. memorystream ();
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Write );
CS. Write (bytin, 0, bytin. Length );
CS. flushfinalblock ();
Byte [] bytout = Ms. toarray ();
Return System. Convert. tobase64string (bytout );
}
Catch (exception ex)
{
Throw new exception ("an error occurred during encryption! Error message: \ n "+ ex. Message );
}
}
# Endregion

# Region tripledes decryption
/// <Summary>
/// Tripledes decryption
/// </Summary>
Public static string tripledesdecrypting (string source)
{
Try
{
Byte [] bytin = system. Convert. frombase64string (source );
Byte [] Key = {42, 16, 93,156, 78, 4,218, 32, 15,167, 44, 80, 26, 20,155,112, 2, 94, 11,204,119, 35,184,197 }; // define the key
Byte [] IV = {55,103,246, 79, 36, 99,167, 3}; // defines the offset.
Tripledescryptoserviceprovider tripledes = new tripledescryptoserviceprovider ();
Tripledes. IV = IV;
Tripledes. Key = key;
Icryptotransform encrypto = tripledes. createdecryptor ();
System. Io. memorystream MS = new system. Io. memorystream (bytin, 0, bytin. Length );
Cryptostream cs = new cryptostream (MS, encrypto, cryptostreammode. Read );
Streamreader strd = new streamreader (CS, encoding. Default );
Return strd. readtoend ();
}
Catch (exception ex)
{
Throw new exception ("an error occurred during decryption! Error message: \ n "+ ex. Message );
}
}
# Endregion
}

Encrypt (encryption and decryption)

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.