Java MD5 encryption console incoming and Web incoming parameter results do not match | | Same string encryption result is different

Source: Internet
Author: User
Tags md5 encryption

Development encountered MD5 encryption inconsistency problem, the exclusion of context encoding, encryption content issues.

Crawled all kinds of information, and finally found the reason.

/**MD5 encryption of strings*/    Private Staticstring encodeByMD5 (String originstring) {if(Originstring! =NULL) {            Try{//creates a summary of information with the specified algorithm nameMessageDigest MD = messagedigest.getinstance ("MD5"); //The summary is last updated with the specified byte array, and the summary calculation is completed                byte[] results = Md.digest (Originstring.getbytes ("UTF-8")); //turns the resulting byte array into a string returnString resultstring =bytearraytohexstring (results); returnresultstring.touppercase (); } Catch(Exception ex) {ex.printstacktrace (); }        }        return NULL; }

Need to give encrypted content to the specified encoding format to get, compared to PHP MD5, really drunk.

It is simple to conclude that PHP does not encrypt characters according to the context-coded format. In contrast to Java encryption, which is encrypted according to the encoded format of the encrypted content, you need to specify the encoding format corresponding to the current context in order to obtain the desired encryption result.

Examples of causes:

This type of error is usually an issue with the encoding format if our console input characters are in BGK format and the Web is passed UTF-8 our toString () view content indicates that there is no difference, this results in the same string encryption after the result is different as the following small example

Import Static  = "[Email protected]| | [Email protected]$%&1000]//  b08f601a7b6b440dfa3ed6eb353c2d24//  //

ps:http://blog.csdn.net/w627782664/article/details/7064273

Java MD5 encryption console incoming and Web incoming parameter results do not match | | Same string encryption result is different

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.