当前位置: 首页 > news >正文

加密算法(一)-对称加密(DES、AES、3DES、Blowfish、Twofish)一篇了解所有主流对称加密,轻松上手使用。

一、对称加密算法

        对称加密算法采用相同的密钥来进行加密和解密操作。其优点是加密和解密速度快,不过密钥的管理和分发存在一定的安全风险。

1.1、DES(已不推荐使用)

这是早期的对称加密算法,密钥长度为 56 位。但由于密钥长度较短,如今已不太安全。

优点

  1. 历史悠久,算法公开,研究较为透彻,有很多相关的实现和工具。
  2. 加密和解密速度相对较快,在早期计算机性能有限的情况下具有一定优势。

缺点

  1. 密钥长度较短,只有 56 位,在现代计算能力下,容易受到暴力破解攻击。
  2. 安全性逐渐降低,目前已不推荐用于对安全性要求较高的场景。

加密

 // DES 加密public static String desEncrypt(String plainText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

public static String desDecrypt(String encryptedText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String desKey = "66666666"; // DES 密钥长度必须为 8 字节// DES 测试String desEncrypted = desEncrypt(plainText, desKey);String desDecrypted = desDecrypt(desEncrypted, desKey);System.out.println("DES 加密: " + desEncrypted);System.out.println("DES 解密: " + desDecrypted);}DES 加密: 7LqxGfmOgresnxwwXzlGBWqFt8hXuqd6E4BC0mLxaBAohvYPdvaZXSO45z9XA5GH
DES 解密: 感谢关注,精华内容将持续更新!
    • 1.2、AES

        目前应用广泛的对称加密算法,支持 128 位、192 位和 256 位的密钥长度,安全性较高,效率也不错。

优点

1.安全性高,支持 128 位、192 位和 256 位的密钥长度,能够有效抵御各种已知的攻击。

2.加密和解密速度快,性能优越,在硬件和软件实现上都有很好的表现。

3.被广泛应用于各种领域,成为了对称加密算法的主流选择。

缺点

1.对于一些对资源要求极高的小型设备,可能会有一定的性能开销。

加密

// AES 加密public static String aesEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

 // AES 解密public static String aesDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String aesKey = "1234567812345678"; // AES 密钥长度可以为 16、24 或 32 字节// AES 测试String aesEncrypted = aesEncrypt(plainText, aesKey);String aesDecrypted = aesDecrypt(aesEncrypted, aesKey);System.out.println("AES 加密: " + aesEncrypted);System.out.println("AES 解密: " + aesDecrypted);}AES 加密: PUI7SM6+J4XvSDnioVneLtQDkBXchZlIF7k9v3fqe5Nwk8Polh+pBxR5RQmbHa7v
AES 解密: 感谢关注,精华内容将持续更新!
    1. 1.3、3DES

        它是 DES 的增强版本,通过多次使用 DES 算法来增加密钥长度,进而提高安全性。

优点

1.在 DES 的基础上进行了改进,通过多次使用 DES 算法,增加了密钥长度,提高了安全性。

2.兼容性好,由于是基于 DES 算法,在一些旧系统中可以继续使用。

缺点

1.加密和解密速度较慢,因为需要进行多次 DES 运算。

2.密钥长度相对较长,管理和存储成本较高。

加密

 // 3DES 加密public static String tripleDesEncrypt(String plainText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// 3DES 解密public static String tripleDesDecrypt(String encryptedText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String tripleDesKey = "0123456789abcdef01234567"; // 3DES 密钥长度必须为 24 字节// 3DES 测试String tripleDesEncrypted = tripleDesEncrypt(plainText, tripleDesKey);String tripleDesDecrypted = tripleDesDecrypt(tripleDesEncrypted, tripleDesKey);System.out.println("3DES 加密: " + tripleDesEncrypted);System.out.println("3DES 解密: " + tripleDesDecrypted);}
    1. 1.4、Blowfish

        这是一种可变密钥长度的对称分组密码算法,密钥长度可以从 32 位到 448 位,具有较高的加密速度和安全性,适用于对速度要求较高的场景。

优点

1.密钥长度可变,范围从 32 位到 448 位,可以根据不同的安全需求进行调整。

2.加密和解密速度快,尤其是在 64 位块的加密操作上表现出色。

3.算法简单,易于实现,没有专利限制。

缺点

1.块大小固定为 64 位,在处理大数据时可能效率不如其他算法。

2.应用范围相对较窄,不如 AES 等算法普及。

加密

// Blowfish 加密public static String blowfishEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// Blowfish 解密public static String blowfishDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String blowfishKey = "abcdefghijklmnop";// Blowfish 测试String blowfishEncrypted = blowfishEncrypt(plainText, blowfishKey);String blowfishDecrypted = blowfishDecrypt(blowfishEncrypted, blowfishKey);System.out.println("Blowfish 加密: " + blowfishEncrypted);System.out.println("Blowfish 解密: " + blowfishDecrypted);}Blowfish 加密: HEKXerp2DpkPIHeIIt/cPBxub0z7jWWYKxZImxB2VOfDWIrH/dHVNqSP7gyHwyiU
Blowfish 解密: 感谢关注,精华内容将持续更新!
    1. 1.5、Twofish

        作为 Blowfish 算法的继任者,Twofish 同样是对称分组加密算法。它支持 128 位、192 位和 256 位的密钥长度,设计上考虑了安全性和性能的平衡,并且具有良好的灵活性和可扩展性。

优点

1.安全性高,设计上考虑了各种攻击方式,具有较好的抗攻击能力。

2.支持多种密钥长度,包括 128 位、192 位和 256 位。

3.算法灵活性高,可扩展性好。

缺点

1.实现相对复杂,需要一定的技术成本。

2.应用不如 AES 广泛,相关的工具和库相对较少。

引入依赖

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.70</version>
</dependency>

加密

// Twofish 加密(需要 Bouncy Castle 库)public static String twofishEncrypt(String plainText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// Twofish 解密(需要 Bouncy Castle 库)public static String twofishDecrypt(String encryptedText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String twofishKey = "0123456789abcdef0123456789abcdef";// Twofish 测试String twofishEncrypted = twofishEncrypt(plainText, twofishKey);String twofishDecrypted = twofishDecrypt(twofishEncrypted, twofishKey);System.out.println("Twofish 加密: " + twofishEncrypted);System.out.println("Twofish 解密: " + twofishDecrypted);}Twofish 加密: Z9UhJb9JZT0++Do6zauZPwf5tOMc6gX2o4LE7pZLXcwTL4PJ6m4LGvobb9k7Uv1e
Twofish 解密: 感谢关注,精华内容将持续更新!

通过以上内容便可轻轻松松使用是对称加密算法.是不是超级简单.有任何问题欢迎留言哦!!!

重点!重点!重点!

遇到问题不用怕不如来我的知识库找找看,也许有意想不到的收获!!!

易网时代-易库资源-易库教程:.NET开发、Java开发、PHP开发、SqlServer技术、MySQL技术-开发资料大全-易网时代-易库资源-易库教程 (escdns.com)

http://www.xdnf.cn/news/247087.html

相关文章:

  • mysql-窗口函数一
  • 链表系列一>合并 k 个升序链表
  • 【CV数据集】DIOR遥感目标检测数据集(含处理好的YOLO、COCO、VOC格式和相关配置文件下载链接)
  • 响应式布局,在飞帆平台中如此简单
  • 文件包含漏洞学习
  • PostgreSQL:pgAdmin 4 使用教程
  • 手撕哈希表
  • Android 移动开发:ProgressBar (水平进度条)
  • 【LeetCode Hot100】回溯篇
  • cua: 为 AI 智能体提供高性能虚拟环境
  • GTA5(传承/增强) 13980+真车 超跑 大型载具MOD整合包+最新GTA6大型地图MOD 5月最新更新
  • PyTorch 2.0编译器技术深度解析:如何自动生成高性能CUDA代码
  • 【Bootstrap V4系列】学习入门教程之 页面内容排版
  • 图像加密算法概述
  • Elsevier latex报错Paragraph ended before \@citex was complete.<to be read again>
  • Vue3 + OpenLayers 企业级应用进阶
  • Linux 第六讲 --- 工具篇(一)yum/apt与vim
  • 哈希表笔记(四)Redis对比Java总结
  • YOLOv8模型训练过程
  • Python与MySQL高效集成指南:从基础到高级实践
  • Hibernate与MybatisPlus的混用问题(Invalid bound statement (not found))
  • (C题|社交媒体平台用户分析问题)2025年第二十二届五一数学建模竞赛(五一杯/五一赛)解题思路|完整代码论文集合
  • 恒流源电路
  • RAG工程-基于LangChain 实现 Advanced RAG(预检索-查询优化)(下)
  • 前端HTML基础知识
  • 【大模型面试每日一题】Day 5:GQA vs MHA效率对比
  • CV中常用Backbone-2:ConvNeXt模型详解
  • 微软推出数款Phi 4“开放式”人工智能模型
  • VB.net序列化和反序列化的使用方法和实用场景
  • NUS:多模态多视角理解评估