Use of MD5 encryption

Source: Internet
Author: User

Today, data security is especially important in any area.
MD5 encryption is an irreversible operation, of course, if you do a simple MD5 encryption of a data, there are some websites can be decrypted according to your ciphertext. If you want to be more secure, you can encrypt your data multiple times. I'll just encrypt it once to say:

MD5 encryption of data
>
* @param password the password to encrypt
* @return Ciphertext after encryption
* @throws nosuchalgorithmexception

public static String md5Password(String password) throws NoSuchAlgorithmException{    //获取实例:传进去的 加密方式我们要进行的是md5,所以传进去的是md5字符串    MessageDigest degiest = MessageDigest.getInstance("md5");    byte[] result =degiest.digest(password.getBytes());    StringBuffer sb = new StringBuffer();    for (byte b : result) {        int number = b&0xff+1;        String hex=Integer.toHexString(number);        if (hex.length()==1) {            sb.append("0"+hex);        }        else {            sb.append(hex);        }    }    return sb.toString();}

MD5 can also query the file's signature, download a thing on the internet, sometimes provide a MD5 value, what is the use of it, mainly to see whether it has been modified, different file signatures. If you use the following code, the signature of the query file is consistent with the MD5 provided, and it has not been modified to get this value to see what you need to do other things. Here's a look at the code:

>
* @ Query the signature of the file
* Path to @param path file
* @return NULL Query exception occurred

public static String checkMd5file(String  path){    try {        MessageDigest digest =MessageDigest.getInstance("md5");        File file = new File(path);        FileInputStream fis = new FileInputStream(file);        int length = -1;        byte[] buffer = new byte[1024];        while((length = fis.read(buffer))!=-1){            digest.update(buffer,0,length);        }        byte[] result = digest.digest();        StringBuilder sb= new StringBuilder();        for (byte b : result) {            int number= b&0xff;            String hex=Integer.toHexString(number);            if (hex.length()==1) {                sb.append("0"+hex);            }            else {                sb.append(hex);            }        }        return sb.toString();    } catch (Exception e) {        // TODO Auto-generated catch block        e.printStackTrace();        return null;    }}

Use of MD5 encryption

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.