当前位置:   article > 正文

Jasypt加密/解密工具嵌入Spring boot框架的使用教程

jasypt

一、Jasypt 是什么?

        Jasypt (Java Simplified Encryption) 是一个开源的Java库,专注于简化加解密过程,特别适合在Java应用程序中处理敏感数据,如密码、密钥或任何其他需要保护的信息。Jasypt的目标是提供一个易于使用的API,使得开发者能够轻松地集成加密功能,而不必成为加密专家。以下是Jasypt的一些关键特性及用途:

关键特性:

  • 文本加密与解密:Jasypt允许你加密字符串,并在需要时轻松解密回原始形式。这对于存储如数据库密码、API密钥等敏感信息非常有用。
  • 密码器支持:它支持多种加密算法(如PBEWITHMD5ANDDES、PBEWITHSHA256AND256BITAES-CBC-BC等),并允许自定义加密策略。
  • 配置文件加密:特别适用于Spring框架,Jasypt可以透明地解密在配置文件中加密的属性,比如数据库连接字符串中的密码。
  • 环境感知:可以在不同的环境中(如开发、测试、生产)使用不同的加密策略和密钥,增强安全性。
  • 无状态操作:Jasypt的加密操作不依赖于外部状态,使得它易于部署和使用。
  • 集成友好:提供了与Spring框架的紧密集成,可以通过Spring Boot Starter轻松配置和使用。

用途

  • 安全存储配置信息:在配置文件中加密mysql、redis等数据库密码,防止明文泄露。
  • 应用内加密逻辑:在应用内部对敏感数据进行加解密操作,例如用户密码的存储和验证。
  • 日志安全:在记录包含敏感信息的日志前对其进行加密,确保日志安全。
  • 数据交换安全:在不同系统间交换数据时,对数据进行加密传输,保障数据传输的安全性。

二、使用步骤

环境要求

  • SpringBoot2.0以上
  • Jasypt 3.0.3

2.1 pom.xml文件中引入依赖

  1. <dependency>
  2.     <groupId>com.github.ulisesbocchio</groupId>
  3.     <artifactId>jasypt-spring-boot-starter</artifactId>
  4.     <version>3.0.3</version>
  5. </dependency>

2.2 在本地application-dev.yml文件中添加Jasypt配置

  1. jasypt:
  2. encryptor:
  3. # 盐值
  4. password: 123
  5. # 指定加密方式
  6. algorithm: PBEWithMD5AndDES
  7. iv-generator-classname: org.jasypt.iv.NoIvGenerator
  8. property:
  9. # 标识为加密属性的前缀
  10. prefix: ENC(
  11. # 标识为加密属性的后缀
  12. suffix: )

注意:这里是解密配置,加密时的算法和盐值必须与以上配置中的algorithm与password(盐值)一致。

盐值概念:
        在密码学中,盐值(Salt)是一种随机数据,通常用于加强密码的哈希过程,以增加破解的难度。在Jasypt(Java Simplified Encryption)中,盐值(也称为密钥或加密密码)是用于加密和解密过程的一个重要组成部分。它是一个额外的输入,与待加密的数据一起使用,以生成一个特定的加密结果。这样,即使相同的明文数据被加密多次,每次都会因为不同的盐值而得到不同的密文。

2.3 编写JasyptUtil工具类用于加密/解密

  1. public class JasyptUtil {
  2. /**
  3. * PBE 算法
  4. */
  5. public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
  6. public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
  7. public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
  8. public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
  9. private JasyptUtil() {
  10. }
  11. /**
  12. * Jasypt 加密
  13. *
  14. * @param encryptedStr 加密字符串
  15. * @param password 盐值
  16. * @return
  17. */
  18. public static String encrypt(String encryptedStr, String password) {
  19. return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);
  20. }
  21. /**
  22. * Jasypt 加密
  23. *
  24. * @param encryptedStr 加密字符串
  25. * @param algorithm 加密算法
  26. * PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
  27. * @param password 盐值
  28. * @return
  29. */
  30. public static String encrypt(String encryptedStr, String algorithm, String password) {
  31. // StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
  32. StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
  33. EnvironmentPBEConfig config = new EnvironmentPBEConfig();
  34. // 指定加密算法
  35. config.setAlgorithm(algorithm);
  36. // 加密盐值
  37. config.setPassword(password);
  38. //config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
  39. encryptor.setConfig(config);
  40. // 加密
  41. return encryptor.encrypt(encryptedStr);
  42. }
  43. /**
  44. * Jasypt 解密
  45. *
  46. * @param decryptStr 解密字符串
  47. * @param password 盐值
  48. * @return
  49. */
  50. public static String decrypt(String decryptStr, String password) {
  51. return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);
  52. }
  53. /**
  54. * Jasypt 解密
  55. *
  56. * @param decryptStr 解密字符串
  57. * @param algorithm 指定解密算法:解密算法要与加密算法一一对应
  58. * PBE ALGORITHMS: [PBEWITHMD5ANDDES, PBEWITHMD5ANDTRIPLEDES, PBEWITHSHA1ANDDESEDE, PBEWITHSHA1ANDRC2_40]
  59. * @param password 盐值
  60. * @return
  61. */
  62. public static String decrypt(String decryptStr, String algorithm, String password) {
  63. // StandardPBEStringEncryptor、StandardPBEBigDecimalEncryptor、StandardPBEBigIntegerEncryptor、StandardPBEByteEncryptor
  64. StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
  65. EnvironmentPBEConfig config = new EnvironmentPBEConfig();
  66. // 指定解密算法:解密算法要与加密算法一一对应
  67. config.setAlgorithm(algorithm);
  68. // 加密秘钥
  69. config.setPassword(password);
  70. //config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
  71. encryptor.setConfig(config);
  72. // 解密
  73. return encryptor.decrypt(decryptStr);
  74. }
  75. public static void main(String[] args) {
  76. String encryptedStr = "I am the string to be encrypted";
  77. String algorithm = PBE_ALGORITHMS_SHA1_RC2_40;
  78. String password = "salt";
  79. String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
  80. System.out.println("加密后的字符串:" + str);
  81. System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
  82. }
  83. }

2.3.1 编写测试类进行测试jasypt加密测试

  1. package com.agileboot.admin.jasypt;
  2. import com.agileboot.common.utils.jasypt.JasyptUtil;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. @SpringBootTest
  6. public class jasyptTest {
  7. /**
  8. * PBE 算法
  9. */
  10. public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
  11. public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
  12. public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
  13. public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";
  14. @Test
  15. public void TsetJasypt() {
  16. String encryptedStr = "root";
  17. String algorithm = PBE_ALGORITHMS_MD5_DES;
  18. String password = "salt";
  19. String str = JasyptUtil.encrypt(encryptedStr, algorithm, password);
  20. System.out.println("加密后的字符串:" + str);
  21. System.out.println("解密后的字符串:" + JasyptUtil.decrypt(str, algorithm, password));
  22. }
  23. }

2.3.2 启动类加上@EnableEncryptableProperties这个注解,用于自动解密

运行结果如下图:

可以在以上代码中设置自己需要加密的明文、盐值、算法,然后将加密后的密码填入application-dev.yml文件中配置mysql、redis等数据库的密码填写位置。注意填写方式为ENC(经过jasypt加密后的密码)。

2.4 修改application-dev.yml配置文件

修改mysql数据库连接密码:

修改redis数据库连接密码:

通过以上的操作就配置完成啦,进一步提高了系统的安全性。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/945657
推荐阅读
相关标签
  

闽ICP备14008679号