Common encryption methods

Source: Internet
Author: User
Tags hmac
Two common encryption methods on the Web: MD5 and sha512.
Here are two methods to use.

The following two spaces of. NET are required.
Using system. Text
Using system. Security. Cryptography

1 MD5
(Without a key, the MD5 encryption results for any text are consistent and there is a security risk) Public   String Pwdbymd5 ( String Stext)
{
String PWD =   "" ;
MD5 = Md5.create ();
// Encrypted is a byte array.
Byte [] S = Md5.computehash (encoding. Unicode. getbytes (stext ));
// Converts an array of the byte type to a string through a loop. This string is obtained by regular character formatting.
For ( Int I =   0 ; I < S. length; I ++ )
{< br> // use a hexadecimal string format. The characters in the format are lowercase letters. If uppercase letters (x) are used, the characters in the format are uppercase letters.
pwd = pwd + S [I]. tostring ( " x " );
}
Return PWD;
}

2 sha512
(Text encryption with a key must be consistent only when the same key is used) Public   Static   String Pwdbysha512 (
String Skey, // Key
String Stext // Text to be encrypted
)
{
Byte [] Hmackey = System. Text. encoding. utf8.getbytes (skey );
Byte [] Hmacdata = System. Text. encoding. utf8.getbytes (stext );

Hmacsha512 HMAC =   New Hmacsha512 (hmackey );

Cryptostream CS =   New Cryptostream (stream. null, HMAC, cryptostreammode. Write );
CS. Write (hmacdata, 0 , Hmacdata. Length );
CS. Close ();

Byte [] Result = HMAC. Hash;

Return Convert. tobase64string (result ); // Returns a string of 28 bytes.
}

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.