Information digest encryption MD2, MD4, MD5, md2md4
The confidentiality of user data has always been a headache for Internet companies. How can we prevent the disclosure of users' personal information? Today we will introduce you to the information digest algorithm MD, the simplest encryption method. How does it protect users' personal information? In fact, it is very simple. After obtaining the user information, encrypt it first, and then save the encrypted result to the database, so that even if the user's data is stolen, it will not be lost. The code below:
JAVA jdk provides MD2 and MD5 encryption methods. JAVA does not support MD4 jdk encryption. The implementation of jdk is as follows:
Private static void MD2_jdk () {try {MessageDigest digest = MessageDigest. getInstance ("MD2"); // get the message digest MD2 object byte [] md2Byte = digest. digest (src. getBytes (); System. out. println ("md2Byte:" + md2Byte. toString (); System. out. println ("md2Byte:" + Hex. encodeHexString (md2Byte);} catch (NoSuchAlgorithmException e) {e. printStackTrace () ;}} private static void MD5_jdk () {try {MessageDigest digest = MessageDigest. getInstance ("MD5"); // obtain the message digest MD5 object byte [] md5Byte = digest. digest (src. getBytes (); System. out. println ("md5Byte:" + md5Byte. toString (); System. out. println ("md5Byte:" + Hex. encodeHexString (md5Byte);} catch (NoSuchAlgorithmException e) {e. printStackTrace ();}}
Bc provides MD2 \ 4 \ 5 encryption (bc uses the jar package to add bc ):
Private static void MD2_bc () {Digest digest = new MD2Digest (); // obtain the message digest MD2 object Digest through BC. update (src. getBytes (), 0, src. getBytes (). length); byte [] md2Byte = new byte [digest. getDigestSize ()]; digest. doFinal (md2Byte, 0); System. out. println ("md2Byte:" + md2Byte. toString (); System. out. println ("md2Byte:" + org. bouncycastle. util. encoders. hex. toHexString (md2Byte);} private static void MD4_bc () {Digest digest = new MD4Digest (); // obtain the message digest MD4 object Digest through BC. update (src. getBytes (), 0, src. getBytes (). length); byte [] md4Byte = new byte [digest. getDigestSize ()]; digest. doFinal (md4Byte, 0); System. out. println ("md4Byte:" + md4Byte. toString (); System. out. println ("md4Byte:" + org. bouncycastle. util. encoders. hex. toHexString (md4Byte);} private static void MD5_bc () {Digest digest = new MD5Digest (); // obtain the message digest MD5 object Digest through BC. update (src. getBytes (), 0, src. getBytes (). length); byte [] md5Byte = new byte [digest. getDigestSize ()]; digest. doFinal (md5Byte, 0); System. out. println ("md5Byte:" + md5Byte. toString (); System. out. println ("md5Byte:" + org. bouncycastle. util. encoders. hex. toHexString (md5Byte ));}
Add a dynamic MD4 Method for jdk through bc:
private static void MD4_bc_jdk(){ try { Security.addProvider(new BouncyCastleProvider()); MessageDigest md = MessageDigest.getInstance("MD4"); byte[] md4Byte = md.digest(src.getBytes()); System.out.println("md4Byte :"+md4Byte.toString()); System.out.println("md4Byte :"+Hex.encodeHexString(md4Byte)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
Finally, we will introduce the implementation of CC MD2 \ 5:
DigestUtils.md2Hex(src.getBytes());DigestUtils.md5Hex(src.getBytes());
Note: src is a set string.
At this point, the implementation of the message digest encryption algorithm MD2 \ 4 \ 5 has been explained. If you are interested in Base64 and symmetric encryption algorithms, you can read my previous blog. Xiaosheng is very fond of Information Security. He is interested. You are welcome to contact us. 1453296946@qq.com)