Java-based Encryption

Source: Internet
Author: User

You can put this in a tool class. When we need to encrypt and transmit user passwords or other important information, call the methods in this tool class. The following is a simple source code:

Import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; /*** an encrypted class ** @ author way **/public class encode {/*** static encryption method ** @ Param codetype * Incoming Encryption Method * @ Param content * pass in the encrypted content * @ return returns the encrypted result */public static string getencode (string codetype, string content) {try {messagedigest digest = messagedigest. getinstance (codetype); // get an instance and pass in the Encryption Method Digest. reset (); // clear digest. updat E (content. getbytes (); // write content. You can specify the encoding method content. getbytes ("UTF-8"); stringbuilder builder = new stringbuilder (); For (byte B: Digest. digest () {builder. append (integer. tohexstring (B> 4) & 0xf); builder. append (integer. tohexstring (B & 0xf);} return builder. tostring ();} catch (nosuchalgorithmexception e) {e. printstacktrace ();} return NULL;} public static void main (string [] ARGs) {string STR = encode. gete Ncode ("MD5", "Hello world! "); // Use MD5 to encrypt system. Out. println (STR); // fc3ff98e8c6a0d3087d515c0473f8677string str1 = encode. getencode (" Sha "," Hello world! "); // Use SHA to encrypt system. Out. println (str1); // performance34d020724ed75a196dfc2ad67c77772d169 }}

 

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.