MD5 Encryption Algorithm (Information digest algorithm), BASE64 algorithm

Source: Internet
Author: User
Tags base64 md5 encryption

1 What is MD5

Information Digest algorithm, you can encrypt characters, each encrypted object after encryption is equal to the length of

Application scenario: The user password is MD5 encrypted and then stored in the database, so that even the Super administrator does not have the ability to know the user's specific password, because the MD5 encrypted data can not be decrypted

Steps:

"Gets the MessageDigest object

MessageDigest MD5 = messagedigest.getinstance ("MD5");

Note: import java.security.MessageDigest;

The character array is encrypted by means of the MessageDigest object

byte [] output = Md5.digest (Str.getbytes ());

Note: The parameters and return values of the digest method are character array types

"Problem: By adding MD5 encrypted data when garbled, how to let him become not garbled

"Solution: Through the BASE64 algorithm

2 What is Base64

The display processing of the string, that is: with the common 64 characters to display the character, so as to effectively avoid the garbled problem

Application scenario: Prevent garbled characters from producing

Steps:

"Uses static methods in the Base64 class directly

Str_output = base64.encodebase64string (output);

Note: The parameter of the Encodebase64string method is a character array, and the return value is a string

Note: import org.apache.tomcat.util.codec.binary.Base64;

3 MD5 and Base64 comprehensive application Simple example
1  Packagecn.xiangxu.testNote.test;2 3 Importjava.security.MessageDigest;4 5 Importorg.apache.tomcat.util.codec.binary.Base64;6 7  Public classTest {8      Public StaticString MD5 (String str)throwsException {9         Try {TenMessageDigest MD5 = messagedigest.getinstance ("MD5"); OneSYSTEM.OUT.PRINTLN ("MD5 object:" +MD5); ASYSTEM.OUT.PRINTLN ("raw data:" +str); -              -             byte[] Output =md5.digest (Str.getbytes ()); theString Str_output =NewString (output); -SYSTEM.OUT.PRINTLN ("Encrypted data:" +str_output); -              -Str_output =base64.encodebase64string (output); +System.out.println ("Number of processed encryption:" +str_output); -}Catch(Exception e) { + e.printstacktrace (); A             Throw NewException (e); at         } -         return NULL; -     } -      -      Public Static voidMain (string[] args)throwsException { -TEST.MD5 ("DD"); in     } -      to      +}
View Code

MD5 Encryption Algorithm (Information digest algorithm), BASE64 algorithm

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.