java implementation of MD5 abstract algorithm complete example
- 2020-05-27 05:35:40
- OfStack
The example of this paper describes the MD5 abstract algorithm implemented by java. I will share it with you for your reference as follows:
package com.soufun.com;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author WHD
*/
public class MD5Test {
// MD5 One-way encryption
public static void main(String[] args) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
String str = "hellomd Abstract algorithm start ";
System.out.println(" The original value " + str);
System.out.println(" The encrypted " + MD5Test.afterMD5(str));
String digest = MD5Test.afterMD5(str);
System.out.println(digest.equals(MD5Test.afterMD5(str)));
}
public static String afterMD5(String str) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
// To obtain MD5 Encrypted object , You can also get SHA Encrypted object
MessageDigest md5 = MessageDigest.getInstance("MD5");
// Retrieves the bytes of the input information using the specified encoding
byte[] bytes = str.getBytes("UTF-8");
// use md5 Class to get the digest, that is, the encrypted bytes
md5.update(bytes);
byte[] md5encode = md5.digest();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < md5encode.length; i++) {
// use &0xff insufficient 24 High, because only occupied 8 low
int val = ((int) md5encode[i]) & 0xff;
if (val < 16) {
buffer.append("0");
}
// In order to 106 Base (base) 16 ) returned as an unsigned integer 1 A string representation of an integer parameter.
buffer.append(Integer.toHexString(val));
}
return buffer.toString();
}
}
Using org. apache. commons. codec. digest. DigestUtilsorg. apache. commons. codec. digest. DigestUtils to achieve md5 encryption
Configuration in maven:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
Note that there is a big difference between version 1.2 and version 1.4, because there are many methods that are extended in 1.4.
The specific code is as follows:
public static String afterMd5(String str){
try {
String md5 = DigestUtils.md5Hex(str.getBytes("UTF-8"));
return md5;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
PS: about encryption and decryption interested friends can also refer to the website online tools:
Password security online detection:
http://tools.ofstack.com/password/my_password_safe
High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword
Thunderbolt, express, and whirlwind encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder
Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt
MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:
http://tools.ofstack.com/password/hash_md5_sha
Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode
I hope this article is helpful to you java programming.