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