本文已收录于专栏
《运维》
目录
概念说明
数据加密
为了保障数据隐私安全,大多企业在存储和传输过程中,通常会对数据进行加密处理,在进行数据交互和共享时,将数据中的敏感信息如个人身份信息进行脱敏或匿名化处理要进行加密监管。所谓数据加密技术,是指将一个信息(或称明文,plain text)经过加密钥匙及加密函数转换,变成无意义的密文,而接收方则将此密文经过解密函数、解密钥匙还原成明文。
加密类型
不可逆加密
基本概念
不可逆加密是指通过数据计算加密后的结果,但是通过结果无法计算出加密的数据,简单来说就是对于一些数据加密之后会产生另一个结果,结果不可以根据加密的过程反推出数据,只能通过产生的结果和你知道的结果进行匹配,看看是否一致。
应用场景
包括文档、音视频文件、软件安装包等用新老摘要对比是否一样
密码加密后存到数据库中
代码实现
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashingExample {
public static void main(String[] args) throws NoSuchAlgorithmException {
String input = "Hello World";
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
String hashedValue = sb.toString();
System.out.println("Hashed Value: " + hashedValue);
}
}
对称加密
基本概念
对称加密也称为单密钥加密是指加密和解密的过程使用同一个密钥进行。简单来说就是发送数据者把数据用密钥进行加密,接收数据者用同样的密钥就可以得到加密之前的数据。
优势利弊
优点:生成密钥的算法公开、计算量小、加密速度快、加密效率高、密钥较短
缺点:双方共同的密钥,有一方密钥被窃取,双方都影响
应用场景
登录信息用户名和密码加密、传输加密、指令加密
代码实现
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class SymmetricEncryptionExample {
public static void main(String[] args) throws Exception {
String input = "Hello World";
// 生成密钥
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
// 加密
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
String encryptedValue = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted Value: " + encryptedValue);
// 解密
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedValue));
String decryptedValue = new String(decryptedBytes);
System.out.println("Decrypted Value: " + decryptedValue);
}
}
非对称加密
基本概念
非对称加密算法需要两个密钥:公开密钥和私有密钥。公钥与私钥是一对,如果用公钥对数据进行加密,只有用对应的私钥才能解密。因为加密和解密使用的是两个不同的密钥。简单来说就是甲方生成一对密钥并将公钥公开,需要向甲方发送信息的其他角色(乙方)使用该密钥(甲方的公钥)对机密信息进行加密后再发送给甲方;甲方再用自己私钥对加密后的信息进行解密。
优势利弊
优点:安全系数比较高
缺点:加解密相对速度慢、密钥长、计算量大、效率低
应用场景
CRS请求证书、蓝牙等硬件信息加密配对传输、关键 的登录信息验证。
代码实现
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class AsymmetricEncryptionExample {
public static void main(String[] args) throws Exception {
String input = "Hello World";
// 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
String encryptedValue = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted Value: " + encryptedValue);
// 解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedValue));
String decryptedValue = new String(decryptedBytes);
System.out.println("Decrypted Value: " + decryptedValue);
}
}
总结提升
不可逆加密(Hashing)
- 不可逆加密是一种单向加密算法,将输入的数据转换为固定长度的哈希值。
- 不可逆加密算法是不可逆的,即无法从哈希值还原出原始数据。
- 常见的不可逆加密算法有MD5、SHA-1、SHA-256等。
- 不可逆加密常用于密码存储、数字签名等场景。
对称加密(Symmetric Encryption)
- 对称加密使用相同的密钥进行加密和解密。
- 对称加密算法的特点是加密和解密速度快,但密钥的安全性需要保证。
- 常见的对称加密算法有DES、AES等。
- 对称加密常用于保护数据的机密性。
非对称加密(Asymmetric Encryption)
- 非对称加密使用一对密钥,包括公钥和私钥。
- 公钥用于加密数据,私钥用于解密数据。
- 非对称加密算法的特点是安全性高,但加密和解密的速度较慢。
- 常见的非对称加密算法有RSA、DSA等。
- 非对称加密常用于数据的加密和数字签名。