Use MD5 and hash encryption in Java

Source: Internet
Author: User

During development, we often encrypt the password information. On the Internet, we can find many MD5 encryption methods. Here we use the built-in classes of javase.

 
1:Import java. Security. messagedigest;
2:Import java. Security. nosuchalgorithmexception;
 
3:Import sun. Misc. base64encoder;
4:
 
5:
6: Public ClassTest {
 
7:
  8:   Public   static  string getmd5 (string PWD) throws nosuchalgorithmexception 
9:{
 
10:Messagedigest MD = messagedigest. getinstance ("Sha");// Sha or MD5
  11:  base64encoder  base  =  New  base64encoder (); 
  12:  string pwdafter =  base . encode (MD. digest (PWD. getbytes (); 
13:ReturnPwdafter;
 
14:}
  15:  
  16:   Public   static   void  main (string [] ARGs) throws nosuchalgorithmexception {
17:String Pwd = getmd5 ("123456");
 
18:System.Out. Println (PWD );
19:}
 
20:}

Output:

Feqncco3yq9h5zugld3czjt4lbs =

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.