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