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:
- 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.
- [Solved] Flowable Start Error: ClassCastException: java.time.LocalDateTime cannot be cast to java.lang.String
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- On the intern () method of string class in Java
- In Java, int is converted to string, and zero is added before the number of bits is insufficient
- Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Java String.split () special character processing
- java.sql.SQLException: Incorrect string value:
- Solve the problem of testing redis cluster“ java.lang.NumberFormatException : For input string: “ [email protected]@17002 “And so on
- Method of generating MD5 file checksums under Windows
- Unity short string intercepts long string
- TSLint:object access via string literals is disallowedtslint(no-string-literal)
- python root:code for Hash MD5 was not found. Error
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- Viewing MD5 value of file under Windows
- How to get the current time in java time string
- build.gradle Warming: cannot be applied to ‘(groovy.lang.Closure)’/'(java.lang.String)’
- error C1189: #error : Please use the /MD switch for _ Afxdll builds — solutions
- Git error: no permission remote: error: unable to unlink old ‘ README.md ‘ (Permission denied)
- Python: How to Fix “Ord() expected string of length 1, but int found”