一、Java常用加密方式
- Base64加密算法(編碼方式)
- MD5加密(消息摘要算法,驗證信息完整性)
- 對稱加密算法
- 非對稱加密算法
- 數字簽名算法
- 數字證書
二、分類
按加密算法是否需要key被分為兩類:
- 不基於key的有: Base64算法、MD5
- 基於key的有: 對稱加密算法、非對稱加密算法、數字簽名算法、數字證書、HMAC、RC4(對稱加密)
按加密算法是否可逆被分為兩類:
- 單向加密算法(不可解密):MD5、SHA、HMAC
- 非單項加密算法(可解密):BASE64、對稱加密算法、非對稱加密算法、數字簽名算法、數字證書
三、算法介紹
1.對稱加密
對稱加密是最快速、最簡單的一種加密方式,加密(encryption)與解密(decryption)用的是同樣的密鑰(secret key)。對稱加密有很多種算法,由於它效率很高,所以被廣泛使用在很多加密協議的核心當中。
對稱加密通常使用的是相對較小的密鑰,一般小於256 bit。因為密鑰越大,加密越強,但加密與解密的過程越慢。如果你只用1 bit來做這個密鑰,那黑客們可以先試着用0來解密,不行的話就再用1解;但如果你的密鑰有1 MB大,黑客們可能永遠也無法破解,但加密和解密的過程要花費很長的時間。密鑰的大小既要照顧到安全性,也要照顧到效率,是一個trade-off。
DES(Data Encryption Standard)和TripleDES是對稱加密的兩種實現。
DES和TripleDES基本算法一致,只是TripleDES算法提供的key位數更多,加密可靠性更高。
DES使用的密鑰key為8字節,初始向量IV也是8字節。
TripleDES使用24字節的key,初始向量IV也是8字節。
兩種算法都是以8字節為一個塊進行加密,一個數據塊一個數據塊的加密,一個8字節的明文加密后的密文也是8字節。如果明文長度不為8字節的整數倍,添加值為0的字節湊滿8字節整數倍。所以加密后的密文長度一定為8字節的整數倍
下面舉個例子:
import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import org.apache.commons.codec.binary.Base64; public class DESDemo { // 算法名稱 public static final String KEY_ALGORITHM = "DES"; // 算法名稱/加密模式/填充方式 // DES共有四種工作模式-->>ECB:電子密碼本模式、CBC:加密分組鏈接模式、CFB:加密反饋模式、OFB:輸出反饋模式 public static final String CIPHER_ALGORITHM = "DES/ECB/NoPadding"; /** * * 生成密鑰key對象 * * @param KeyStr * 密鑰字符串 * @return 密鑰對象 * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws Exception */ private static SecretKey keyGenerator(String keyStr) throws Exception { byte input[] = HexString2Bytes(keyStr); DESKeySpec desKey = new DESKeySpec(input); // 創建一個密匙工廠,然后用它把DESKeySpec轉換成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); return securekey; } private static int parse(char c) { if (c >= 'a') return (c - 'a' + 10) & 0x0f; if (c >= 'A') return (c - 'A' + 10) & 0x0f; return (c - '0') & 0x0f; } // 從十六進制字符串到字節數組轉換 public static byte[] HexString2Bytes(String hexstr) { byte[] b = new byte[hexstr.length() / 2]; int j = 0; for (int i = 0; i < b.length; i++) { char c0 = hexstr.charAt(j++); char c1 = hexstr.charAt(j++); b[i] = (byte) ((parse(c0) << 4) | parse(c1)); } return b; } /** * 加密數據 * * @param data * 待加密數據 * @param key * 密鑰 * @return 加密后的數據 */ public static String encrypt(String data, String key) throws Exception { Key deskey = keyGenerator(key); // 實例化Cipher對象,它用於完成實際的加密操作 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); SecureRandom random = new SecureRandom(); // 初始化Cipher對象,設置為加密模式 cipher.init(Cipher.ENCRYPT_MODE, deskey, random); byte[] results = cipher.doFinal(data.getBytes()); // 該部分是為了與加解密在線測試網站(http://tripledes.online-domain-tools.com/)的十六進制結果進行核對 for (int i = 0; i < results.length; i++) { System.out.print(results[i] + " "); } System.out.println(); // 執行加密操作。加密后的結果通常都會用Base64編碼進行傳輸 return Base64.encodeBase64String(results); } /** * 解密數據 * * @param data * 待解密數據 * @param key * 密鑰 * @return 解密后的數據 */ public static String decrypt(String data, String key) throws Exception { Key deskey = keyGenerator(key); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // 初始化Cipher對象,設置為解密模式 cipher.init(Cipher.DECRYPT_MODE, deskey); // 執行解密操作 return new String(cipher.doFinal(Base64.decodeBase64(data))); } public static void main(String[] args) throws Exception { String source = "helloittx"; System.out.println("原文: " + source); String key = "A1B2C3D4E5F60708"; String encryptData = encrypt(source, key); System.out.println("加密后: " + encryptData); String decryptData = decrypt(encryptData, key); System.out.println("解密后: " + decryptData); } }
2.非對稱加密
非對稱加密為數據的加密與解密提供了一個非常安全的方法,它使用了一對密鑰,公鑰(public key)和私鑰(private key)。私鑰只能由一方安全保管,不能外泄,而公鑰則可以發給任何請求它的人。非對稱加密使用這對密鑰中的一個進行加密,而解密則需要另一個密鑰。比如,你向銀行請求公鑰,銀行將公鑰發給你,你使用公鑰對消息加密,那么只有私鑰的持有人–銀行才能對你的消息解密。與對稱加密不同的是,銀行不需要將私鑰通過網絡發送出去,因此安全性大大提高。
目前最常用的非對稱加密算法是RSA算法,是Rivest, Shamir, 和Adleman於1978年發明,他們那時都是在MIT。請看下面的例子:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.spec.RSAPrivateCrtKeySpec; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import com.lxh.rsatest.HexUtil; import Decoder.BASE64Decoder; import Decoder.BASE64Encoder; public class RSAEncrypt { /** 指定加密算法為DESede */ private static String ALGORITHM = "RSA"; /** 指定key的大小 */ private static int KEYSIZE = 1024; /** 指定公鑰存放文件 */ private static String PUBLIC_KEY_FILE = "public.keystore"; /** 指定私鑰存放文件 */ private static String PRIVATE_KEY_FILE = "private.keystore"; /** * 生成密鑰對 */ private static void generateKeyPair() throws Exception { /** RSA算法要求有一個可信任的隨機數源 */ SecureRandom sr = new SecureRandom(); /** 為RSA算法創建一個KeyPairGenerator對象 */ KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM); /** 利用上面的隨機數據源初始化這個KeyPairGenerator對象 */ kpg.initialize(KEYSIZE, sr); /** 生成密匙對 */ KeyPair kp = kpg.generateKeyPair(); /** 得到公鑰 */ Key publicKey = kp.getPublic(); /** 得到私鑰 */ Key privateKey = kp.getPrivate(); /** 用對象流將生成的密鑰寫入文件 */ ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(PUBLIC_KEY_FILE)); ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(PRIVATE_KEY_FILE)); oos1.writeObject(publicKey); oos2.writeObject(privateKey); /** 清空緩存,關閉文件輸出流 */ oos1.close(); oos2.close(); } /** * 生成密鑰對字符串 */ private static void generateKeyPairString() throws Exception { /** RSA算法要求有一個可信任的隨機數源 */ SecureRandom sr = new SecureRandom(); /** 為RSA算法創建一個KeyPairGenerator對象 */ KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM); /** 利用上面的隨機數據源初始化這個KeyPairGenerator對象 */ kpg.initialize(KEYSIZE, sr); /** 生成密匙對 */ KeyPair kp = kpg.generateKeyPair(); /** 得到公鑰 */ Key publicKey = kp.getPublic(); /** 得到私鑰 */ Key privateKey = kp.getPrivate(); /** 用字符串將生成的密鑰寫入文件 */ String algorithm = publicKey.getAlgorithm(); // 獲取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); BigInteger prime = null; BigInteger exponent = null; RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class); prime = keySpec.getModulus(); exponent = keySpec.getPublicExponent(); System.out.println("公鑰模量:" + HexUtil.bytes2Hex(prime.toByteArray())); System.out.println("公鑰指數:" + HexUtil.bytes2Hex(exponent.toByteArray())); System.out.println(privateKey.getAlgorithm()); RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(privateKey, RSAPrivateCrtKeySpec.class); BigInteger privateModulus = privateKeySpec.getModulus(); BigInteger privateExponent = privateKeySpec.getPrivateExponent(); System.out.println("私鑰模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray())); System.out.println("私鑰指數:" + HexUtil.bytes2Hex(privateExponent.toByteArray())); } /** * 加密方法 source: 源數據 */ public static String encrypt(String source) throws Exception { generateKeyPair(); /** 將文件中的公鑰對象讀出 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE)); Key key = (Key) ois.readObject(); ois.close(); String algorithm = key.getAlgorithm(); // 獲取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); BigInteger prime = null; BigInteger exponent = null; if ("RSA".equals(algorithm)) { // 如果是RSA加密 RSAPublicKeySpec keySpec = (RSAPublicKeySpec) keyFact.getKeySpec(key, RSAPublicKeySpec.class); prime = keySpec.getModulus(); exponent = keySpec.getPublicExponent(); // System.out.println("公鑰模量:" + HexUtil.bytes2Hex(prime.toByteArray())); // System.out.println("公鑰指數:" + HexUtil.bytes2Hex(exponent.toByteArray())); } /** 得到Cipher對象來實現對源數據的RSA加密 */ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] b = source.getBytes(); /** 執行加密操作 */ byte[] b1 = cipher.doFinal(b); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(b1); } /** * 解密算法 cryptograph:密文 */ public static String decrypt(String cryptograph) throws Exception { /** 將文件中的私鑰對象讀出 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE)); Key key = (Key) ois.readObject(); String algorithm = key.getAlgorithm(); // 獲取算法 KeyFactory keyFact = KeyFactory.getInstance(algorithm); RSAPrivateCrtKeySpec privateKeySpec = (RSAPrivateCrtKeySpec) keyFact.getKeySpec(key, RSAPrivateCrtKeySpec.class); BigInteger privateModulus = privateKeySpec.getModulus(); BigInteger privateExponent = privateKeySpec.getPrivateExponent(); // System.out.println("私鑰模量:" + HexUtil.bytes2Hex(privateModulus.toByteArray())); // System.out.println("私鑰指數:" + HexUtil.bytes2Hex(privateExponent.toByteArray())); /** 得到Cipher對象對已用公鑰加密的數據進行RSA解密 */ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); BASE64Decoder decoder = new BASE64Decoder(); byte[] b1 = decoder.decodeBuffer(cryptograph); /** 執行解密操作 */ byte[] b = cipher.doFinal(b1); return new String(b); } public static void main(String[] args) throws Exception { generateKeyPair(); //生成文件形式公鑰和私鑰 //generateKeyPairString();//生成字符串形式公鑰和私鑰 String source = "非對稱加密RSA";// 要加密的字符串 String cryptograph = encrypt(source);// 生成的密文 String hexCrypt = HexUtil.bytes2Hex(cryptograph.getBytes(), false); System.out.println("生成的密文--->" + hexCrypt); String target = decrypt(HexUtil.hex2String(hexCrypt));// 解密密文 System.out.println("解密密文--->" + target); } }
雖然非對稱加密很安全,但是和對稱加密比起來,它非常的慢,所以我們還是要用對稱加密來傳送消息,但對稱加密所使用的密鑰我們可以通過非對稱加密的方式發送出去。
(1) 對稱加密加密與解密使用的是同樣的密鑰,所以速度快,但由於需要將密鑰在網絡傳輸,所以安全性不高。
(2) 非對稱加密使用了一對密鑰,公鑰與私鑰,所以安全性高,但加密與解密速度慢。
(3) 解決的辦法是將對稱加密的密鑰使用非對稱加密的公鑰進行加密,然后發送出去,接收方使用私鑰進行解密得到對稱加密的密鑰,然后雙方可以使用對稱加密來進行溝通。
3.Base64編碼
Base 64 Encoding有什么用?舉個簡單的例子,你使用SMTP協議 (Simple Mail Transfer Protocol 簡單郵件傳輸協議)來發送郵件。因為這個協議是基於文本的協議,所以如果郵件中包含一幅圖片,我們知道圖片的存儲格式是二進制數據(binary data),而非文本格式,我們必須將二進制的數據編碼成文本格式,這時候Base 64 Encoding就派上用場了。
public void testJDKBase64(){ String encoderStr = java.util.Base64.getEncoder().encodeToString(s.getBytes()); System.out.println("encode :"+encoderStr); String decodeStr = new String(java.util.Base64.getDecoder().decode(encoderStr)); System.out.println("decodeStr :"+decodeStr); } public void testCodecBase64(){ String encoderStr = org.apache.commons.codec.binary.Base64.encodeBase64String(s.getBytes()); System.out.println("encode :"+encoderStr); String decodeStr = new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoderStr)); System.out.println("decodeStr :"+decodeStr); }
附工具類
package com.hl.bluetooth.util; import org.springframework.boot.system.ApplicationHome; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; import java.util.Objects; /** * @DateL 2021/11/16 15:09 * @ClassName: FileUtils **/ @Component public class FileUtils { public static File multipartFileToFile(MultipartFile multipartFile) { File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename())); try { InputStream ins = null; ins = multipartFile.getInputStream(); OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } return file; } /** * 圖片轉化成base64字符串 * */ public static String getImageStr(String imgPath) { InputStream in = null; byte[] data = null; String encode = null; // 對字節數組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); try { // 讀取圖片字節數組 in = new FileInputStream(imgPath); data = new byte[in.available()]; in.read(data); encode = encoder.encode(data); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return encode; } /** * base64字符串轉化成圖片 * * @param imgData 圖片編碼 */ public static String generateImage(String imgData, String fileName) { if (imgData == null) { // 圖像數據為空 return "null"; } ApplicationHome applicationHome = new ApplicationHome(FileUtils.class); File source = applicationHome.getSource(); String dirPath = source.getParentFile().toString() + "/upload"; BASE64Decoder decoder = new BASE64Decoder(); File dir = new File(dirPath); if (!dir.exists()){ dir.mkdirs(); } File file = new File(dirPath+"/"+fileName); if (file.exists()){ file.delete(); } try { // Base64解碼 byte[] b = decoder.decodeBuffer(imgData); for (int i = 0; i < b.length; ++i) { // 調整異常數據 if (b[i] < 0) { b[i] += 256; } } OutputStream out = new FileOutputStream(dirPath+"\\"+fileName); out.write(b); out.flush(); out.close(); return dirPath+"\\"+fileName; } catch (Exception e) { e.printStackTrace(); return "null"; } } }