Based on previous experience, we usually use the MD5 encryption algorithm, although it can be cracked, but because of its cyclical longer, so I have been using, today learned a new encryption algorithm, secure hashing algorithm, specifically he is not the data encryption algorithm, but the data digital Signature Algorithm , It is not to encrypt the entire data, but rather to sign the data extraction part of the information, using the algorithm to encrypt different data, you will get a unique string, like a signature, only that data can produce such a signature string.
The following is the Java implementation of the SHA algorithm encryption , we can use this algorithm to write cryptographic tool classes:
Package Com.cloud.skater.util;import Java.security.messagedigest;import Java.util.logging.level;import Java.util.logging.logger;public class Encrypt {private static final String Key_sha = "SHA";p rivate static final Logger log = Logger.getlogger (Encrypt.class.getName ());p ublic static string Encryptsha (String data) throws Exception { MessageDigest sha = Messagedigest.getinstance (Key_sha); Sha.update (Data.getbytes ()); return bytearraytohexstring ( Sha.digest ());} Converts a byte array to a hexadecimal string private static string Bytearraytohexstring (byte[] bytearray) {String strdigest = ""; for (int i = 0; i < bytearray.length; i++) {strdigest + = bytetohexstring (Bytearray[i]);} return strdigest;} Convert bytes to hexadecimal string private static string Bytetohexstring (Byte ib) {char[] Digit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '};char[] ob = new Char[2];ob[0] = digit[(ib >>> 4) & 0x0f];ob[1] = Digit [IB & 0X0F]; string s = new string (OB); return s;} public static void Main (String argS[]) {try {System.out.println (Encrypt.encryptsha ("zhang1234")),} catch (Exception e) {log.log (Level.severe, E.getmessage (), E.getcause ());}}}
using it, you can paste directly as a tool class.
The code collation of Sha secure hashing encryption algorithm instead of MD5 encryption