一、對稱加密
對稱加密是指信息發送者和接收者使用同一個密鑰進行加密和解密的過程,因此需要在通訊開始之前將密鑰共享給對方。常見的對稱加密算法有DES、3DES、AES等。
對稱加密的優點在於加密解密速度快,適用於大量數據的加密處理;缺點在於密鑰的共享,如果密鑰被黑客獲取,則信息安全會受到威脅。因此,對稱加密通常應用於內部網絡、安全性要求不高的場景。
示例代碼:
// 使用Java的AES對稱加密算法加密字符串
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESUtil {
public static String encrypt(String key, String data) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
return new String(Base64.encodeBase64(encryptedData));
}
public static String decrypt(String key, String encryptedData) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getBytes());
byte[] decryptedData = cipher.doFinal(encryptedBytes);
return new String(decryptedData, "UTF-8");
}
}
二、非對稱加密
非對稱加密又稱為公開密鑰加密,是指發送方使用接收方的公鑰進行加密,接收方使用自己的私鑰進行解密的過程。因此,公鑰可以公開而私鑰需要保護。常見的非對稱加密算法有RSA、ECC等。
非對稱加密的優點在於密鑰不需要共享,信息安全受到了很好的保護;缺點在於加密解密速度較慢,適用於少量數據的加密處理。因此,非對稱加密通常應用於安全性要求較高的場景,例如數字簽名、SSL/TLS協議等。
示例代碼:
// 使用Java的RSA非對稱加密算法加密字符串
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class RSAUtil {
// 生成公鑰和私鑰對
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
// 使用公鑰加密
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return new String(Base64.encodeBase64(encryptedData));
}
// 使用私鑰解密
public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getBytes());
byte[] decryptedData = cipher.doFinal(encryptedBytes);
return new String(decryptedData);
}
}
三、對稱加密與非對稱加密的結合
對稱加密和非對稱加密都有自己的優缺點,而實際應用中常常需要兼顧兩者的優點,因此就有了對稱加密與非對稱加密的結合。例如,前端向後端發送數據時,可以使用非對稱加密將AES密鑰加密後發送給後端,後端再使用私鑰解密得到AES密鑰,再使用AES密鑰對數據進行加密處理。
示例代碼:
// 使用Java的RSA和AES結合加密字符串
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class RSAWithAESUtil {
// 生成公鑰和私鑰對
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
// 使用公鑰加密AES密鑰
public static String encryptAesKey(String aesKey, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedAesKey = cipher.doFinal(aesKey.getBytes());
return new String(Base64.encodeBase64(encryptedAesKey));
}
// 使用私鑰解密AES密鑰
public static String decryptAesKey(String encryptedAesKey, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedBytes = Base64.decodeBase64(encryptedAesKey.getBytes());
byte[] decryptedAesKey = cipher.doFinal(encryptedBytes);
return new String(decryptedAesKey);
}
// 使用AES密鑰加密數據
public static String encryptData(String data, String aesKey) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return new String(Base64.encodeBase64(encryptedData));
}
// 使用AES密鑰解密數據
public static String decryptData(String encryptedData, String aesKey) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(aesKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getBytes());
byte[] decryptedData = cipher.doFinal(encryptedBytes);
return new String(decryptedData);
}
}
原創文章,作者:XFMIV,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/333757.html
微信掃一掃
支付寶掃一掃