for a plaintext, for security, sometimes we need to do MD5 encryption, the following provides a Java tool method, directly called.
p>
p>
/**
* MD5加密
*/
public class MD5Util {
/**
* Encodes a string 2 MD5
*
* @param str String to encode
* @return Encoded String
* @throws NoSuchAlgorithmException
*/
public static String crypt(String str) {
if (str == null || str.length() == 0) {
throw new IllegalArgumentException("String to encript cannot be null or zero length");
}
StringBuffer hexString = new StringBuffer();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[] hash = md.digest();
for (int i = 0; i < hash.length; i++) {
if ((0xff & hash[i]) < 0x10) {
hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
} else {
hexString.append(Integer.toHexString(0xFF & hash[i]));
}
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hexString.toString();
}
}
p>
p>
p>
p>
p>
p>
p>
p>
p>
p>
p>
div>
Read More:
- In Java, int is converted to string, and zero is added before the number of bits is insufficient
- Python: How to Fix “Ord() expected string of length 1, but int found”
- [Two Sigma OA] Longest Chain
- Nanny level solutions use the enterprise version of MyEclipse to show the MyEclipse trial expired solution and activation
- Mybatis custom list collection parser
- How to display the Chinese name and installation time of installed software in snmp4j walk Windows
- How to use C # to get image format without system. Drawing. Common
- Differences between length() size() and C strlen() of C + + string member functions
- LeetCode 23. Merge k Sorted Lists(java)
- SQLServerException: The server failed to resume the transaction. Desc:ab00000002
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- 43. Implementation of the shortest code of multiply strings | Java
- Springboot integration redis reports non null key required (solved)
- Encapsulation of adding, deleting and modifying database by JDBC
- Random number random reports an error. Illegalargumentexception: bound must be positive
- Leetcode: 7. Reverse Integer(JAVA)
- Difference between isempty method and isblank method in stringutils
- Asynchronous Future Parallel processing request code example
- Example of public key signature and verification generated with fabric case
- Regarding [java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Boolean], combined with examples