3DES, AES, MD5 encryption and decryption Methods

Source: Internet
Author: User
Tags md5 encryption pkcs7

---------- Des 8-bit key ---
/// <Summary>
/// String Encryption
/// </Summary>
/// <Param name = "strtext"> string to be encrypted </param>
/// <Param name = "strencrkey"> key </param>
/// <Returns> returns the encrypted string </returns>
Public static string desencrypt (string strtext, string strencrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
Bykey = system. Text. encoding. utf8.getbytes (strencrkey. substring (0, strencrkey. Length ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. utf8.getbytes (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (bykey, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (system. Exception error)
{
Return "error:" + error. Message + "/R ";
}
}
/// <Summary>
/// String decryption
/// </Summary>
/// <Param name = "strtext"> string to be decrypted </param>
/// <Param name = "sdecrkey"> key </param>
/// <Returns> returns the decrypted string </returns>
Public static string desdecrypt (string strtext, string sdecrkey)
{
Byte [] bykey = NULL;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Byte [] inputbytearray = new byte [strtext. Length];
Try
{
Bykey = system. Text. encoding. utf8.getbytes (sdecrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (bykey, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
System. Text. Encoding encoding = new system. Text. utf8encoding ();
Return encoding. getstring (Ms. toarray ());
}
Catch (system. Exception error)
{
Return "error:" + error. Message + "/R ";
}

}
------- 3DES 16-bit key ---------
/// <Summary>
/// 3DES encrypted string
/// </Summary>
/// <Param name = "a_strstring"> string to be encrypted </param>
/// <Param name = "a_strkey"> key </param>
/// <Returns> base64-encoded string </returns>
/// <Remarks> static method, which adopts the default ASCII encoding </remarks>
Public static string encryptdes16 (string a_strstring, string a_strkey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();

// Tripledescryptoserviceprovider des = new tripledescryptoserviceprovider ();
// Md5cryptoserviceprovider hashmd5 = new md5cryptoserviceprovider ();
Byte [] B = strtohexbyte (a_strkey );

Des. Key = B;
// Des. IV = B;
// Des. Key = B; // asciiencoding. ASCII. getbytes (a_strkey); // hashmd5.computehash (asciiencoding. ASCII. getbytes (a_strkey ));
Des. mode = ciphermode. ECB;
Des. Padding = paddingmode. pkcs7;

 

Icryptotransform desencrypt = des. createencryptor ();

Byte [] buffer = asciiencoding. utf8.getbytes (a_strstring );
Return convert. tobase64string (desencrypt. transformfinalblock (buffer, 0, buffer. Length ));
} // End Method

Private Static byte [] strtohexbyte (string hexstring)
{
Byte [] returnbytes = new byte [hexstring. Length/2];
For (INT I = 0; I <returnbytes. length; I ++)
Returnbytes [I] = convert. tobyte (hexstring. substring (I * 2, 2), 16 );
Return returnbytes;
}

/// <Summary>
/// 3DES decryption string
/// </Summary>
/// <Param name = "a_strstring"> string to be decrypted </param>
/// <Param name = "a_strkey"> key </param>
/// <Returns> decrypted string </returns>
/// <Exception CREF = ""> key error </exception>
/// <Remarks> static method, which adopts the default ASCII encoding </remarks>
Public static string decryptdes16 (string a_strstring, string a_strkey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Aescryptoserviceprovider AES = new aescryptoserviceprovider ();

Des. Key = strtohexbyte (a_strkey );
Des. mode = ciphermode. ECB;
Des. Padding = paddingmode. pkcs7;

Icryptotransform desdecrypt = des. createdecryptor ();

String result = "";
Try
{
Byte [] buffer = convert. frombase64string (a_strstring );
Result = asciiencoding. utf8.getstring (desdecrypt. transformfinalblock (buffer, 0, buffer. Length ));
}
Catch (exception E)
{

Throw (new exception ("invalid key or input string is not a valid base64 string", e ));
}

Return result;
}

---------- AES ----------
Public static byte [] aesendian (byte [] S1, byte [] key, byte [] IV, byte [] mainkey)
{

Aescryptoserviceprovider ACSP = new aescryptoserviceprovider ();
ACSP. mode = ciphermode. ECB;
ACSP. Padding = paddingmode. pkcs7;
// ACSP. Key = mainkey;
Memorystream mstream = new memorystream ();

Cryptostream cstream = new cryptostream (mstream, ACSP. createencryptor (Key, IV), cryptostreammode. Write );

Cstream. Write (S1, 0, s1.length );

Cstream. flushfinalblock ();

Byte [] outb1 = mstream. toarray ();

Cstream. Close ();

Mstream. Close ();

Return outb1;

}

Private Static byte [] _ key1 = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };

Public static byte [] aesdemo-( byte [] S2, byte [] key, byte [] IV, byte [] mainkey)
{

Aescryptoserviceprovider ACSP = new aescryptoserviceprovider ();
ACSP. mode = ciphermode. ECB;
ACSP. Padding = paddingmode. pkcs7;
// ACSP. Key = mainkey;
Memorystream mtream2 = new memorystream ();
Cryptostream destreame = new cryptostream (mtream2, ACSP. createdecryptor (Key, IV), cryptostreammode. Write );

Destreame. Write (S2, 0, s2.length );

Destreame. flushfinalblock ();

Byte [] outs2 = mtream2.toarray ();

Mtream2.close ();

Destreame. Close ();

Return outs2;

}

Call
Aescryptoserviceprovider ACSP = new aescryptoserviceprovider ();
Byte [] skey = encoding. Default. getbytes (key );
Byte [] SIV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
------------- MD5 --------------
Public static string MD5 (string STR, int code)
{
If (code = 16) // 16-bit MD5 encryption (take 32-bit encryption 9 ~ 25 characters)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5"). tolower (). substring (8, 16 );
}
Else // 32-bit encryption
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5"). tolower ();
}
}
------------------------------

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.