4.MD5加密
Message Digest Algorithm MD5(中文名為消息摘要算法第五版)為計算機安全領域廣泛使用的一種散列函數,用以提供消息的完整性保護。該算法的文件號為RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992).
MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest開發出來,經MD2、MD3和MD4發展而來。
MD5用於確保信息傳輸完整一致。是計算機廣泛使用的雜湊算法之一(又譯摘要算法、哈希算法),主流編程語言普遍已有MD5實現。將數據(如漢字)運算為另一固定長度值,是雜湊算法的基礎原理,MD5的前身有MD2、MD3和MD4。
MD5的作用是讓大容量信息在用數字簽名軟件簽署私人密鑰前被”壓縮”成一種保密的格式(就是把一個任意長度的字節串變換成一定長的十六進制數字串)。
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; /** * Java消息摘要算法 MD5 工具類,其實其他摘要算法的實現也類似 */ public class MD5Util { /** * 對文本執行 md5 摘要加密, 此算法與 mysql,JavaScript生成的md5摘要進行過一致性對比. * @param plainText * @return 返回值中的字母為小寫 */ public static String md5(String plainText) { if (null == plainText) { plainText = ""; } String MD5Str = ""; try { // JDK 6 支持以下6種消息摘要算法,不區分大小寫 // md5,sha(sha-1),md2,sha-256,sha-384,sha-512 MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; StringBuilder builder = new StringBuilder(32); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) builder.append("0"); builder.append(Integer.toHexString(i)); } MD5Str = builder.toString(); // LogUtil.println("result: " + buf.toString());// 32位的加密 } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return MD5Str; } // 一個簡版測試 public static void main(String[] args) { String m1 = md5("1"); String m2 = md5(m1); /* 輸出為 * m1=c4ca4238a0b923820dcc509a6f75849b * m2=28c8edde3d61a0411511d3b1866f0636 */ System.out.println("m1="+m1); System.out.println("m2="+m2); } }
通常我們不直接使用上述MD5加密。通常將MD5產生的字節數組交給Base64再加密一把,得到相應的字符串。
5.數字簽名算法
簽名:就有安全性,抗否認性
數字簽名:帶有密鑰(公鑰,私鑰)的消息摘要算法
作用:
1. 驗證數據的完整性
2. 認證數據來源
3. 抗否認
數字簽名遵循:私鑰簽名,公鑰驗證
常用的數字簽名算法:RSA,DSA,ECDSA
RSA介紹:
是經典算法,是目前為止使用最廣泛的數字簽名算法。
RSA數字簽名算法的密鑰實現與RSA的加密算法是一樣的,算法的名稱都叫RSA。密鑰的產生和轉換都是一樣的。
RSA數字簽名算法主要包括MD和SHA兩類。
import java.security.KeyFactory;
import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import org.apache.commons.codec.binary.Hex; public class RSATest { public static final String src = "hello world"; public static void main(String[] args) { jdkRSA(); } /** * 說明: 用java的jdk里面相關方法實現rsa的簽名及簽名驗證 */ public static void jdkRSA() { try { // 1.初始化密鑰 KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance("RSA"); //設置KEY的長度 keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); //得到公鑰 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); //得到私鑰 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); // 2.進行簽名 //用私鑰進行簽名 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( rsaPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); //構造一個privateKey PrivateKey privateKey = keyFactory .generatePrivate(pkcs8EncodedKeySpec); //聲明簽名的對象 Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(src.getBytes()); //進行簽名 byte[] result = signature.sign(); System.out.println("jdk rsa sign:" + Hex.encodeHexString(result)); // 3.驗證簽名 //用公鑰進行驗證簽名 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( rsaPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("RSA"); //構造一個publicKey PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); //聲明簽名對象 signature = Signature.getInstance("MD5withRSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); //驗證簽名 boolean bool = signature.verify(result); System.out.println("jdk rsa verify:" + bool); } catch (Exception e) { System.out.println(e.toString()); } } }
四、應用場景
Base64應用場景:圖片轉碼(應用於郵件,img標簽,http加密)
MD5應用場景:密碼加密、imei加密、文件校驗
非對稱加密:電商訂單付款、銀行相關業務
五、附多個工具類
AES
package com.hl.bluetooth.util;
import org.apache.commons.lang3.StringUtils; import org.apache.tomcat.util.codec.binary.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AesEncryptUtil { //使用AES-128-CBC加密模式,key需要為16位,key和iv可以相同! private final static String KEY = "ABCDEF1234432100"; private final static String IV = "43211234DCAB6789"; /** * 加密方法 * @param data 要加密的數據 * @param key 加密key * @param iv 加密iv * @return 加密的結果 * @throws Exception */ public static String encrypt(String data, String key, String iv) throws Exception { try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/補碼方式"NoPadding PkcsPadding int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new Base64().encodeToString(encrypted); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解密方法 * @param data 要解密的數據 * @param key 解密key * @param iv 解密iv * @return 解密的結果 * @throws Exception */ public static String desEncrypt(String data, String key, String iv) throws Exception { try { // byte[] encrypted1 = new Base64().decode(data); byte[] encrypted1 = parseHexStr2Byte(data); // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 使用默認的key和iv加密 * @param data * @return * @throws Exception */ public static String encrypt(String data) throws Exception { return encrypt(data, KEY, IV); } /** * 使用默認的key和iv解密 * @param data * @return * @throws Exception */ public static String desEncrypt(String data) throws Exception { if (StringUtils.isEmpty(data)) { return null; } return desEncrypt(data, KEY, IV); } /** * @Author wdc * @Description 16進制轉byte數組 * @Date 2021/4/19 11:14 * @Param [hexStr] * @return byte[] **/ public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length()/2]; for (int i = 0;i< hexStr.length()/2; i++) { int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); result[i] = (byte) (high * 16 + low); } return result; } public static void main(String[] args) { // String test = "{'admin','admin'}"; //// String test =new String(test1.getBytes(),"UTF-8"); // String data = null; // data = AesEncryptUtil.encrypt(test); // System.out.println("數據:"+test); // System.out.println("加密:"+data); // String jiemi = AesEncryptUtil.desEncrypt(data); // System.out.println("解密:"+jiemi); } }
base64
package com.sgitg.util;
import java.io.UnsupportedEncodingException; import org.springframework.util.Base64Utils; public class Base64Util { /** * 解碼base64編碼的字符串 * * @param source * @return * @throws UnsupportedEncodingException */ public static String decodeFromString(String source) { String str = ""; try { byte[] bt = Base64Utils.decodeFromString(source); str = new String(bt, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); // To change body of catch statement use File | // Settings | File Templates. } return str; } /** * 對字符串進行base64編碼 * * @param source * @return * @throws UnsupportedEncodingException */ public static String encodeToString(String source) { byte[] bt = new byte[0]; try { bt = source.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); // To change body of catch statement use File | // Settings | File Templates. } return Base64Utils.encodeToString(bt); } }
EncryptUtils (編碼集合)
* 1、Base64編碼
* 1、AES、DES可逆算法
* 2、md5,Hex,Sha不可逆算法加密
package com.sgitg.util;
import org.apache.commons.codec.digest.DigestUtils; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; import java.util.zip.CRC32; /** * 數據加密 * 繼承org.apache.commons.codec.digest.DigestUtils * 1、Base64編碼 * 1、AES、DES可逆算法 * 2、md5,Hex,Sha不可逆算法加密 * * @author liuyadu */ public class EncryptUtils extends DigestUtils { /** * 計算大文件 md5獲取getMD5(); SHA1獲取getSha1() CRC32獲取 getCRC32() */ private static char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; // 測試 public static void main(String[] args) { String en = encryptDES("hahahaha", "yaer"); String de = decryptDES("kzWPLLyAsDeBr84lL2COsA==", "yaer"); System.out.println(de); System.out.println(en); en = encryptAES("hahahaha", "yaer"); de = decryptAES("FBC82B89BAA1FBBDF3AE086A09D57E7C", "yaer"); System.out.println(de); System.out.println(en); } /** * AES加密(可逆) * * @param plainText 明文 * @param privateKey 密鑰 * @return * @throws NoSuchAlgorithmException */ public static String encryptAES(String plainText, String privateKey) { try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(privateKey.getBytes()); kgen.init(128, random); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); byte[] byteContent = plainText.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] byteRresult = cipher.doFinal(byteContent); String sb = new String(""); for (int i = 0; i < byteRresult.length; i++) { String hex = Integer.toHexString(byteRresult[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb = sb.concat(hex.toUpperCase()); } return sb; } catch (Exception e) { return null; } } /** * AES解密 * * @param cipherText 密文 * @param privateKey 密鑰 * @return * @throws Exception */ public static String decryptAES(String cipherText, String privateKey) { try { if (cipherText.length() < 1) { return null; } byte[] byteRresult = new byte[cipherText.length() / 2]; for (int i = 0; i < cipherText.length() / 2; i++) { int high = Integer.parseInt(cipherText.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(cipherText.substring(i * 2 + 1, i * 2 + 2), 16); byteRresult[i] = (byte) (high * 16 + low); } KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(privateKey.getBytes()); kgen.init(128, random); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] result = cipher.doFinal(byteRresult); return new String(result); } catch (Exception e) { return null; } } /** * 加密DES(可逆) * * @param plainText 明文 * @param privateKey 密鑰 * @return */ public static String encryptDES(String plainText, String privateKey) { try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(privateKey.getBytes()); keygen.init(56, secureRandom); SecretKey secretKey = keygen.generateKey(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] cipherBytes = cipher.doFinal(plainText.getBytes("utf-8")); byte[] plainTextBytes = Base64.getEncoder().encode(cipherBytes); return new String(plainTextBytes, "utf-8"); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解密DES * * @param cipherText 密文 * @param privateKey 密鑰 * @return */ public static String decryptDES(String cipherText, String privateKey) { try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(privateKey.getBytes()); keygen.init(56, secureRandom); SecretKey secretKey = keygen.generateKey(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] cipherTextBytes = Base64.getDecoder().decode(cipherText.getBytes("utf-8")); byte[] cipherBytes = cipher.doFinal(cipherTextBytes); return new String(cipherBytes, "utf-8"); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 獲取文件md5值 * * @return md5串 */ public static String md5(File file) { try { //encrypt MessageDigest messagedigest = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer); return bufferToHex(messagedigest.digest()); } catch (Exception e) { return null; } } /*** * 獲取文件SHA1值 * * @return String 適用於上G大的文件 */ public static String sha1(File file) { try { MessageDigest messagedigest = MessageDigest.getInstance("SHA-1"); FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer); return bufferToHex(messagedigest.digest()); } catch (Exception e) { return null; } } /** * 獲取文件SHA256值 * * @return String */ public static String sha256(File file) { try { MessageDigest messagedigest = MessageDigest.getInstance("SHA-256"); FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer); return bufferToHex(messagedigest.digest()); } catch (Exception e) { return null; } } /** * 獲取文件CRC32碼 * * @return String */ public static String crc32(File file) { CRC32 crc32 = new CRC32(); // MessageDigest.get FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); byte[] buffer = new byte[8192]; int length; while ((length = fileInputStream.read(buffer)) != -1) { crc32.update(buffer, 0, length); } return crc32.getValue() + ""; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 計算二進制數據 * * @return */ private static String bufferToHex(byte bytes[]) { return bufferToHex(bytes, 0, bytes.length); } private static String bufferToHex(byte bytes[], int m, int n) { StringBuffer stringbuffer = new StringBuffer(2 * n); int k = m + n; for (int l = m; l < k; l++) { appendHexPair(bytes[l], stringbuffer); } return stringbuffer.toString(); } private static void appendHexPair(byte bt, StringBuffer stringbuffer) { char c0 = hexDigits[(bt & 0xf0) >> 4]; char c1 = hexDigits[bt & 0xf]; stringbuffer.append(c0); stringbuffer.append(c1); } }