In our programming, we often have to encrypt some special content. Today I have summarized a few simple encryption methods and share them with you!
How to implement simple encryption and decryption of strings with JAVA? To ensure the security of user information, when the system saves user information, it is necessary to encrypt and save its password to the database.
When you need to use a password, take out the data and decrypt it.
Avoid saving plaintext passwords.
Plan 1:
package com.tnt.util; import java.security.MessageDigest; public class StringUtil { private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; /** * Convert byte array to hex string* * @param b * Byte array* @return hex string*/ public static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); resultString = byteArrayToHexString(md.digest(resultString .getBytes())); } catch (Exception ex) { } return resultString; } public static void main(String[] args) { System.err.println(MD5Encode("123456")); } }Plan 2
package com.shangyu.core.utils;public class MD5 {public static String getMD5(byte[] source) {String s = null;char hexDigits[] = { // The characters used to convert bytes into hexadecimal representation '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd','e', 'f' };try {java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");md.update(source);byte tmp[] = md.digest(); // The calculation result of MD5 is a 128-bit long integer, // It is represented by bytes, which is 16 bytes. char str[] = new char[16 * 2]; // If each byte is represented in hexadecimal, two characters are used, // Therefore, it takes 32 characters to represent hexadecimal int k = 0; // It represents the corresponding character position in the conversion result for (int i = 0; i < 16; i++) { // Starting from the first byte, the conversion of each byte of MD5// into hexadecimal characters byte byte0 = tmp[i]; // Take the i-th byte str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // Take the numerical conversion of the upper 4 bits in byte, // >>>// For logical shift right, shift the symbol bits right together str[k++] = hexDigits[byte0 & 0xf]; // Take the numerical conversion of the lower 4 bits in byte}s = new String(str); // Convert the result after conversion to a string} catch (Exception e) {e.printStackTrace();}return s;}public static String getMD5(String str) {return getMD5(str.getBytes());}public static void main(String[] args){System.out.println(MD5.getMD5("123456"));}}Thank you for reading, I hope it can help you. Thank you for your support for this site!