谈谈对《加密算法》的理解
文章目录
- 一、什么是加密算法?
- 二、常见的加密算法有哪些?
- 2.1 对称加密
- 2.2 非对称加密
- 2.3 哈希算法
- 三、加密算法代码展示
- 3.1 MD5加密
- 3.2 秘钥加密
- 3.3 AES加密解密
- 四、加密算法的使用场景
一、什么是加密算法?
加密算法是一种通过数学方法将明文转换为密文的过程,其目的是防止未经授权的访问。它的核心特征有:机密性、完整性、认证性和不可否认性。
二、常见的加密算法有哪些?
加密算法主要分为以下两类:
2.1 对称加密
对称加密使用相同的密钥进行加密和解密,特点是速度快、适合处理明文数据。常见的对称加密算法包括:
- DES:早期标准,现因密钥长度短(56位)已不安全。
- AES:当前主流算法,支持128、192、256位密钥,广泛应用于SSL/TLS、磁盘加密等。
2.2 非对称加密
非对称加密使用一对密钥(公钥和私钥),公钥加密,私钥解密,安全性高但速度较慢。常见算法包括:
- RSA:基于大整数分解难题,广泛用于数字签名和密钥交换。
- ECC:基于椭圆曲线数学,密钥长度短但安全性高,适合资源受限环境。
2.3 哈希算法
哈希算法将任意长度的数据映射为固定长度的哈希值,用于数据完整性验证和密码存储。常见算法包括:
- MD5:128位,已不推荐用于安全场景,因存在碰撞风险。
- SHA-256:256位,SHA-2家族成员,广泛用于区块链和数字证书。
- bcrypt:专为密码哈希设计,安全性高。
三、加密算法代码展示
3.1 MD5加密
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {String content = "a";int hash = content.hashCode();// 根据算法名称获取MD5算法对象MessageDigest messageDigest = MessageDigest.getInstance("MD5");// 获取MD5算法对象byte[] bytes = content.getBytes("UTF-8");// 对字节数组进行摘要messageDigest.update(bytes);// 获取摘要后的结果byte[] resultArray = messageDigest.digest();System.out.println("原文 = " + content);System.out.println("原文字节数组 = " + Arrays.toString(bytes));System.out.println("密文字节数组 = " + Arrays.toString(resultArray));System.out.println("密文 = " + Tools.toHexString(resultArray));}
运行结果如下:
原文 = a
原文字节数组 = [97]
密文字节数组 = [12, -63, 117, -71, -64, -15, -74, -88, 49, -61, -103, -30, 105, 119, 38, 97]
密文 = 0cc175b9c0f1b6a831c399e269772661
3.2 秘钥加密
public static void main(String[] args) {try {Mac mac = Mac.getInstance("HmacMD5");// 初始化,传入密钥KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");// 生成(随机)秘钥SecretKey secretKey = keyGenerator.generateKey();mac.init(secretKey);mac.update("hello".getBytes());byte[] bytes = mac.doFinal();// 将密文转为16进制字符串String result = Tools.toHexString(bytes);System.out.println("密文:" + result);} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);} catch (InvalidKeyException e) {throw new RuntimeException(e);}}
3.3 AES加密解密
public class AESKit {public static void main(String[] args) {// AES加密:128位:16字节String msg = "我本将心向明月奈何明月照沟渠";String encrypt = AESKit.encrypt("1234567890abcdef", msg);System.out.println("原文 = " + msg);System.out.println("密文 = " + encrypt);String decrypt = AESKit.decrypt("1234567890abcdef", encrypt);System.out.println("解密 = " + decrypt);}// 加密public static String encrypt(String key, String content) {try {// 1.创建加密对象Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");// 2.初始化加密对象cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 3.加密byte[] bytes= cipher.doFinal(content.getBytes("UTF-8"));// 4.返回密文return Base64.getEncoder().encodeToString(bytes);} catch (Exception e) {throw new RuntimeException(e);}}// 解密public static String decrypt(String key, String encryptMsg) {try {// 1.创建加密对象Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");// 2.初始化解密对象cipher.init(Cipher.DECRYPT_MODE, secretKey);// 3.解密byte[] bytes = Base64.getDecoder().decode(encryptMsg);byte[] resultBytes = cipher.doFinal(bytes);return new String(resultBytes);} catch (Exception e) {throw new RuntimeException(e);}}
}
测试结果如下:
原文 = 我本将心向明月奈何明月照沟渠
密文 = C6lGmU3jvX+7At/0IXXXyKCr2WWolqXCPlDLySdXvDga8a4Bf/GTpsb7PANKcNGL
解密 = 我本将心向明月奈何明月照沟渠
四、加密算法的使用场景
加密算法在以下领域发挥重要作用:
网络安全:SSL/TLS协议使用RSA和AES保护HTTPS通信。
数据存储:磁盘加密使用AES保护敏感数据。
区块链:SHA-256用于比特币的挖矿和交易验证。
身份认证:RSA和ECC用于数字签名,确保身份可信。