实战SSM_O2O商铺_44【DES加密】 关键配置信息进行DES加密

简介: 实战SSM_O2O商铺_44【DES加密】 关键配置信息进行DES加密

概述


为了安全,我们需要对数据库的用户名和密码进行加密。

之前的文章 Spring-使用加密的属性文件02


工程结构


20180809215453553.png

DES工具类

package com.artisan.o2o.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 
 * 
 * @ClassName: DESUtils
 * 
 * @Description: DES是一种对称加密算法。 所谓对称加密算法就是指使用相同的密钥
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年8月9日 下午9:09:22
 */
@SuppressWarnings("restriction")
public class DESUtils {
  private static Key key;
  // 设置密钥key
  private static String KEY_STR = "myKey";
  private static String CHARSETNAME = "UTF-8";
  private static String ALGORITHM = "DES";
  static {
    try {
      // 生成DES算法对象
      KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
      // 运行SHA1安全策略
      SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
      // 设置上密钥种子
      secureRandom.setSeed(KEY_STR.getBytes());
      // 初始化基于SHA1的算法对象
      generator.init(secureRandom);
      // 生成密钥对象
      key = generator.generateKey();
      generator = null;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  /**
   * 
   * 
   * @Title: getEncryptString
   * 
   * @Description: 获取加密后的信息
   * 
   * @param str
   * 
   * @return: String
   */
  public static String getEncryptString(String str) {
    // 基于BASE64编码,接收byte[]并转换为String
    BASE64Encoder base64encoder = new BASE64Encoder();
    try {
      // 按UTF-8编码
      byte[] bytes = str.getBytes(CHARSETNAME);
      // 获取加密对象
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      // 初始化密码信息
      cipher.init(Cipher.ENCRYPT_MODE, key);
      // 加密
      byte[] doFinal = cipher.doFinal(bytes);
      // byte[] to encode好的String并返回
      return base64encoder.encode(doFinal);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  /**
   * 
   * 
   * @Title: getDecryptString
   * 
   * @Description: 获取解密之后的信息
   * 
   * @param str
   * 
   * @return: String
   */
  public static String getDecryptString(String str) {
    // 基于BASE64编码,接收byte[]并转换为String
    BASE64Decoder base64decoder = new BASE64Decoder();
    try {
      // 将字符串decode成byte[]
      byte[] bytes = base64decoder.decodeBuffer(str);
      // 获取解密对象
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      // 初始化解密信息
      cipher.init(Cipher.DECRYPT_MODE, key);
      // 解密
      byte[] doFinal = cipher.doFinal(bytes);
      // 返回解密之后的信息
      return new String(doFinal, CHARSETNAME);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  // 测试
  public static void main(String[] args) {
    System.out.println(getEncryptString("root"));
    System.out.println(getEncryptString("root"));
    System.out.println(getDecryptString("WnplV/ietfQ="));
    System.out.println(getDecryptString("WnplV/ietfQ="));
  }
}


修改配置文件中的用户名和密码

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
#jdbc.username=root
#jdbc.password=root
jdbc.username=WnplV/ietfQ=
jdbc.password=WnplV/ietfQ=

继承PropertyPlaceholderConfigurer,重写convertProperty方法

package com.artisan.o2o.util;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
 * 
 * 
 * @ClassName: EncryptPropertyPlaceholderConfigurer
 * 
 * @Description: 继承PropertyPlaceholderConfigurer,重写convertProperty
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年8月9日 下午9:20:04
 */
public class EncryptPropertyPlaceholderConfigurer extends
    PropertyPlaceholderConfigurer {
  // 需要加密的字段数组
  private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
  /**
   * 对关键的属性进行转换
   */
  @Override
  protected String convertProperty(String propertyName, String propertyValue) {
    if (isEncryptProp(propertyName)) {
      // 解密
      String decryptValue = DESUtils.getDecryptString(propertyValue);
      return decryptValue;
    } else {
      return propertyValue;
    }
  }
  /**
   * 
   * 
   * @Title: isEncryptProp
   * 
   * @Description: 判断该属性是否加密
   * 
   * @param propertyName
   * 
   * @return: boolean
   */
  private boolean isEncryptProp(String propertyName) {
    for (String encryptpropertyName : encryptPropNames) {
      if (encryptpropertyName.equals(propertyName))
        return true;
    }
    return false;
  }
}


配置自定义的EncryptPropertyPlaceholderConfigurer

<bean  class="com.artisan.o2o.util.EncryptPropertyPlaceholderConfigurer"
        p:location="classpath:jdbc.properties"
        p:fileEncoding="utf-8"/> 


20180809220031432.png


测试

启动测试,可以debug调测 ,能正常从数据库加载数据即可


Github地址

代码地址: https://github.com/yangshangwei/o2o

相关文章
|
11天前
|
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
138 0
Spring Boot yml 配置敏感信息加密
本文介绍了如何在 Spring Boot 项目中使用 Jasypt 实现配置文件加密,包含添加依赖、配置密钥、生成加密值、在配置中使用加密值及验证步骤,并提供了注意事项,确保敏感信息的安全管理。
399 1
Jasypt加密数据库配置信息
本文介绍了使用 Jasypt 对配置文件中的公网数据库认证信息进行加密的方法,以提升系统安全性。主要内容包括:1. 背景介绍;2. 前期准备,如依赖导入及版本选择;3. 生成密钥并实现加解密测试;4. 在配置文件中应用加密后的密码,并通过测试接口验证解密结果。确保密码安全的同时,保障系统的正常运行。
229 3
Jasypt加密数据库配置信息
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
223 5
2023/11/10学习记录-C/C++对称分组加密DES
本文介绍了对称分组加密的常见算法(如DES、3DES、AES和国密SM4)及其应用场景,包括文件和视频加密、比特币私钥加密、消息和配置项加密及SSL通信加密。文章还详细展示了如何使用异或实现一个简易的对称加密算法,并通过示例代码演示了DES算法在ECB和CBC模式下的加密和解密过程,以及如何封装DES实现CBC和ECB的PKCS7Padding分块填充。
170 4
2023/11/10学习记录-C/C++对称分组加密DES
DES加密初探
本文介绍了Python中常用的DES和3DES加解密方法,包括ECB和CBC模式。通过示例代码展示了如何使用`Crypto`和`pyDes`库实现加解密,并讨论了不同的填充方式。最后,通过一道CTF例题,详细解析了从图像中提取密文、进行ASCII转换、Base64解码、凯撒解码和最终的DES解密过程。
292 4
DES加密初探
网络安全的盾与剑:漏洞防御与加密技术的实战应用
在数字化浪潮中,网络安全成为保护信息资产的重中之重。本文将深入探讨网络安全的两个关键领域——安全漏洞的防御策略和加密技术的应用,通过具体案例分析常见的安全威胁,并提供实用的防护措施。同时,我们将展示如何利用Python编程语言实现简单的加密算法,增强读者的安全意识和技术能力。文章旨在为非专业读者提供一扇了解网络安全复杂世界的窗口,以及为专业人士提供可立即投入使用的技术参考。
120 4
Docker中配置TLS加密的步骤
我们可以在 Docker 中成功配置 TLS 加密,增强 Docker 环境的安全性,保护容器之间以及与外界的通信安全。需要注意的是,在实际应用中,应根据具体情况进行更细致的配置和调整,确保符合安全要求。同时,定期更新证书和私钥,以保障安全性。
599 60
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
501 60