当前位置: 首页 > 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)

相关文章:

  • 网络安全防火墙技术有哪些?网络防火墙的主要作用
  • Java朴实无华按天计划从入门到实战(94天直达Java高阶)
  • 【Shell 脚本编程】详细指南:第二章 - 变量与字符串操作
  • Qml组件之Image
  • 数字智慧方案6160丨智慧医疗系统平台建设方案(46页PPT)(文末有下载方式)
  • Go-web开发之社区功能
  • B站Michale_ee——ESP32_IDF SDK——FreeRTOS_2 队列
  • 2025大模型微调视频课程全套(附下载)
  • 2025年渗透测试面试题总结-拷打题库30(题目+回答)
  • Curl 全面使用指南
  • node.js模块化步骤(各标准区别)CommonJS规范、AMD规范、UMD规范、ES Modules (ESM)
  • 小刚说C语言刷题—1602总分和平均分
  • 基于若依RuoYi-Vue3-FastAPI 的 Docker 部署记录
  • 驱动开发系列55 - Linux Graphics QXL显卡驱动代码分析(二)显存管理
  • 《Android 应用开发基础教程》——第十章:使用 Gson 实现网络 JSON 数据解析与对象映射
  • RAGFlow报错:ESConnection.sql got exception
  • 纯html实现的json数据转csv文件
  • 题解:洛谷 CF2091E Interesting Ratio
  • 猫,为什么是猫?
  • Y1代码AC集
  • 李强签署国务院令,公布修订后的《中华人民共和国植物新品种保护条例》
  • 中国固体火箭发动机领域杰出专家赵殿礼逝世,享年92岁
  • Meta一季度净利增长三成:上调全年资本支出,受关税影响亚洲出口电商广告支出减少
  • 山西太原一居民小区发生爆炸,应急管理部派工作组赴现场
  • 五一去哪儿| 追着花期去旅行,“赏花经济”绽放文旅新活力
  • 屠呦呦当选美国国家科学院外籍院士