Get the App
SLTechnology News&Howtos  ›  Development  › 

Implementation of java encryption MD5 and an example of password Verification Code

Shulou Source: shulou.com Published: 2022-06-03 14:06:05 10月25日 Update

This article mainly explains "java encryption MD5 implementation and password verification code example", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "java encryption MD5 implementation and password verification code example" bar!

MD5 algorithm has the following characteristics:

Compressibility: Any length of data, the calculated MD5 value length is fixed.

Easy to calculate: MD5 values are easy to calculate from raw data.

Anti-modification: Any change to the original data, even if only 1 byte is modified, the MD5 value obtained is very different.

Strong anti-collision: Given the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (i.e. fake data).

MD5 allows large volumes of information to be "compressed" into a secure format (i.e., a string of bytes of arbitrary length is converted into a string of hexadecimal digits of a certain length) before being signed with a private key by digital signature software. In addition to MD5, some of the more famous ones are sha-1, RIPEMD and Haval.

package test; import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import org.junit.Test;public class Teste { @Test public void testMd5() { System.out.println (encrypt("1234567")); } @Test public void testlogin() { String password = encrypt ("123456adfaf"); if (encrypt("123456adfaf").equals (password)) { System.out.println("Password correct"); } else { System.out.println("Password incorrect"); } } private String encrypt (String password) { String passwordMd5 = null; try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(password.getBytes("utf-8")); passwordMd5 = toHex(bytes); } catch (NoSuchAlgorithmException| UnsupportedEncodingException e) { e.printStackTrace(); } return passwordMd5; } private static String toHex (byte[] bytes) { final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray(); StringBuilder ret = new StringBuilder (bytes.length * 2); for (int i=0; i> 4) & 0x0f]); ret.append(HEX_DIGITS[bytes[i] & 0x0f]); } return ret.toString(); }}

Thank you for reading, the above is the "java encryption MD5 implementation and password verification code example" content, after learning this article, I believe we have a deeper understanding of java encryption MD5 implementation and password verification code example, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Passwords data validation code instances encryption length learning content bytes that is numbers large famous same difficult useful information hexadecimal compression Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn Redmi Xiaomi Shulou Information