SHA1
package com.stone.security; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.DigestInputStream; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.util.Arrays; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; public class SHA { public static void main(String[] args) throws Exception { encodeByMAC("China oP...&*()...&802134..."); encodeBySHA("China oP...&*()...&802134..."); shaFile(); } /** * Message summary using the MAC algorithm* @param data * @throws Exception */ public static void encodeByMAC(String data) throws Exception{ // KeyGenerator keyGen = KeyGenerator.getInstance("HmacSHA1"); // SecretKey key = keyGen.generateKey(); // This key generated each time is different, PBEKeySpec keySpec = new PBEKeySpec("randomkey^(^&*^%$".toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(keySpec); /* * This class provides the functions of the "Message Authentication Code" (MAC) algorithm. * MAC provides a way to check the integrity of information transmitted or stored on unreliable media based on secret keys. * Typically, message verification codes are used between two participants who share a secret key to verify the information transmitted between the two. * The MAC mechanism based on cryptographic hash functions is also called HMAC. Combined with the secret shared key, * HMAC can be used for any cryptographic hash function (such as MD5 or SHA-1) */ Mac mac = Mac.getInstance("HmacSHA1"); // All three of the following are available // Mac mac = Mac.getInstance("HmacSHA256"); // Mac mac = Mac.getInstance("HmacSHA384"); // Mac mac = Mac.getInstance("HmacSHA512"); mac.init(key); byte[] dest = mac.doFinal(data.getBytes()); System.out.println(dest.length); System.out.println("MAC Summary:" + Arrays.toString(dest)); } /** * SHA1 encryption uses message digest MessageDigest to handle* @throws Exception */ public static String encodeBySHA(String str) throws Exception{ MessageDigest sha1; sha1 = MessageDigest.getInstance("SHA1"); // The following three are not available// sha1 = MessageDigest.getInstance("SHA256"); // sha1 = MessageDigest.getInstance("SHA384"); // sha1 = MessageDigest.getInstance("SHA512"); sha1.update(str.getBytes()); //Update the summary first byte[] digest = sha1.digest(); //The hash calculation is completed by performing final operations such as padding. After this method is called, the summary is reset. /* * Use the specified byte array to make the final update of the summary and complete the summary calculation. * That is, this method first calls update(input), * passes input array to the update method, and then calls digest(). */ // byte[] digest = sha1.digest(str.getBytes()); String hex = toHex(digest); System.out.println("SHA1 Summary:" + hex); return hex; } /** * File Data Summary* @throws Exception */ public static void shaFile() throws Exception { MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); DigestOutputStream dos = new DigestOutputStream(new FileOutputStream(new File("abc.txt")), messageDigest); dos.write("Chinese People...&())f*(214)admin*".getBytes()); dos.close(); byte[] digest = messageDigest.digest(); System.out.println("Write a file using stream, the summary of the file is:" + toHex(digest)); DigestInputStream dis = new DigestInputStream(new FileInputStream(new File("abc.txt")), messageDigest); byte[] buf = new byte[100]; int len; while ((len = dis.read(buf)) != -1) { System.out.println("The data read is: " + new String(buf, 0, len)); } dis.close(); byte[] digest2 = messageDigest.digest(); //When the stream is read, the file is finished, and the digest is the same as when it is written System.out.println("Use stream to read the file, the digest of the file is:" + toHex(digest2)); } /** * sha1 digest is to hexadecimal* @param digest * @return */ private static String toHex(byte[] digest) { StringBuilder sb = new StringBuilder(); int len = digest.length; String out = null; for (int i = 0; i < len; i++) { // out = Integer.toHexString(0xFF & digest[i] + 0xABCDEF); //Add any salt out = Integer.toHexString(0xFF & digest[i]);//Original method if (out.length() == 1) { sb.append("0");//If you add 0 in front of 1 bit } sb.append(out); } return sb.toString(); } }
MD5
MD5 (Message Digest Algorithm 5), the fifth edition of the Message Digest Algorithm. Message digest is an algorithm: no matter how long the original data is, the result of the message digest is of a fixed length; it is an irreversible algorithm. Changes in any bit bit of the original data will cause great differences in the results of the message digest, and the probability of calculating the original data based on the results is extremely low.
The message digest can be regarded as the fingerprint of the original data. Different fingerprints will make the original data different.
package com.stone.security; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.DigestInputStream; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.util.Arrays; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; public class MD5 { public static void main(String[] args) throws Exception { encodeByMAC("China oP...&*()...&802134..."); encodeByMd5("China oP...&*()...&802134..."); md5File(); } /** * Message summary using the MAC algorithm* @param data * @throws Exception */ public static void encodeByMAC(String data) throws Exception{ // KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); // SecretKey key = keyGen.generateKey(); // This key generated each time is different, PBEKeySpec keySpec = new PBEKeySpec("randomkey^(^&*^%$".toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(keySpec); /* * This class provides the functions of the "Message Authentication Code" (MAC) algorithm. * MAC provides a way to check the integrity of information transmitted or stored on unreliable media based on secret keys. * Typically, message verification codes are used between two participants who share a secret key to verify the information transmitted between the two. * The MAC mechanism based on cryptographic hash functions is also called HMAC. Combined with a secret shared key, * HMAC can be used for any cryptographic hash function (such as MD5 or SHA-1) */ Mac mac = Mac.getInstance("HmacMD5"); mac.init(key); byte[] dest = mac.doFinal(data.getBytes()); System.out.println(dest.length); System.out.println("MAC Summary:" + Arrays.toString(dest)); } /** * md5 encryption uses message digest MessageDigest to handle* @throws Exception */ public static String encodeByMd5(String str) throws Exception{ MessageDigest md5; md5 = MessageDigest.getInstance("MD5"); md5.update(str.getBytes()); //Update the summary first byte[] digest = md5.digest(); //The hash calculation is completed by performing final operations such as padding. After this method is called, the summary is reset. /* * Use the specified byte array to make the final update of the summary and complete the summary calculation. * That is, this method first calls update(input), * passes input array to the update method, and then calls digest(). */ // byte[] digest = md5.digest(str.getBytes()); String hex = toHex(digest); System.out.println("MD5 summary:" + hex); return hex; } /** * File data summary* @throws Exception */ public static void md5File() throws Exception { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); DigestOutputStream dos = new DigestOutputStream(new FileOutputStream(new File("abc.txt")), messageDigest); dos.write("Chinese People...&())f*(214)admin*".getBytes()); dos.close(); byte[] digest = messageDigest.digest(); System.out.println("Write a file using stream, the summary of the file is:" + toHex(digest)); DigestInputStream dis = new DigestInputStream(new FileInputStream(new File("abc.txt")), messageDigest); byte[] buf = new byte[100]; int len; while ((len = dis.read(buf)) != -1) { System.out.println("The data read is: " + new String(buf, 0, len)); } dis.close(); byte[] digest2 = messageDigest.digest(); //When the stream is read, the file is finished, and the digest is the same as when it is written System.out.println("Use stream to read the file, the digest of the file is:" + toHex(digest2)); } /** * md5 digest is to hexadecimal* @param digest * @return */ private static String toHex(byte[] digest) { StringBuilder sb = new StringBuilder(); int len = digest.length; String out = null; for (int i = 0; i < len; i++) { // out = Integer.toHexString(0xFF & digest[i] + 0xABCDEF); //Add any salt out = Integer.toHexString(0xFF & digest[i]);//Original method if (out.length() == 1) { sb.append("0");//If you add 0 in front of 1 bit } sb.append(out); } return sb.toString(); } }PS: Here are two more MD5 encryption tools for you. Interested friends can refer to it:
MD5 online encryption tool:
http://tools.VeVB.COM/password/CreateMD5Password
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha