一、对称加密
对称加密是指信息发送者和接收者使用同一个密钥进行加密和解密的过程,因此需要在通讯开始之前将密钥共享给对方。常见的对称加密算法有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/n/333757.html
微信扫一扫
支付宝扫一扫