一般用于配置密码等敏感信息

解密/加密工具类

  1. package com.baobaotao.placeholder;
  2.  
  3. import sun.misc.BASE64Decoder;
  4. import sun.misc.BASE64Encoder;
  5.  
  6. import javax.crypto.Cipher;
  7. import javax.crypto.KeyGenerator;
  8. import java.security.Key;
  9. import java.security.SecureRandom;
  10.  
  11. /**
  12. * 加密/解密工具类
  13. * Created by sherry on 15-6-28.
  14. */
  15. public class DESUtils {
  16. /*指定加密/解密使用的密匙*/
  17. private static Key key;
  18. /*注意:密匙必须是8的倍数*/
  19. private static String KEY_STR = "myKeykkk";
  20. static {
  21. try {
  22. /*防止Linux下随机生成key*/
  23. SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
  24. secureRandom.setSeed(KEY_STR.getBytes());
  25.  
  26. KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
  27. //keyGenerator.init(new SecureRandom(KEY_STR.getBytes()));
  28. keyGenerator.init(secureRandom);
  29. key = keyGenerator.generateKey();
  30. keyGenerator = null;
  31. }catch (Exception e){
  32. throw new RuntimeException(e);
  33. }
  34. }
  35.  
  36. /*对字符串进行DES加密,返回BASE64编码的加密字符串*/
  37. public static String getEncryptString(String string){
  38. BASE64Encoder base64Encoder = new BASE64Encoder();
  39. try {
  40. byte[] strBytes = string.getBytes("UTF-8");
  41. Cipher cipher = Cipher.getInstance("DES");
  42. cipher.init(Cipher.ENCRYPT_MODE,key);
  43. byte[] encryptStrBytes = cipher.doFinal(strBytes);
  44. return base64Encoder.encode(encryptStrBytes);
  45. } catch (Exception e) {
  46. throw new RuntimeException(e);
  47. }
  48. }
  49.  
  50. /*对BASE64编码的加密字符串进行解密,返回解密后的字符串*/
  51. public static String getDecryptString(String str){
  52. BASE64Decoder base64Decoder = new BASE64Decoder();
  53. try {
  54. byte[] strBytes = base64Decoder.decodeBuffer(str);
  55. Cipher cipher = Cipher.getInstance("DES");
  56. cipher.init(Cipher.DECRYPT_MODE,key);
  57. byte[] decryptStrBytes = cipher.doFinal(strBytes);
  58. return new String(decryptStrBytes,"UTF-8");
  59. } catch (Exception e) {
  60. throw new RuntimeException(e);
  61. }
  62. }
  63.  
  64. public static void main(String[] args) {
  65. args = new String[]{"root","123456"};
  66. String[] result = new String[2];
  67. if (args == null||args.length<1){
  68. System.out.println("请输入要加密的字符串,用空格分割");
  69. }else {
  70. int i = 0;
  71. for (String arg:args){
  72. System.out.println(arg + ":" + getEncryptString(arg));
  73. result[i++] = getEncryptString(arg);
  74. }
  75. }
  76. for (String temp:result){
  77. System.out.println(temp+":"+getDecryptString(temp));
  78. }
  79. }
  80. }

属性编辑器

  1. package com.baobaotao.placeholder;
  2.  
  3. import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
  4.  
  5. /**
  6. * Created by sherry on 15-6-28.
  7. */
  8. public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
  9. private String[] encryptPropNames = {"username_mysql","password"};
  10.  
  11. @Override
  12. protected String convertProperty(String propertyName, String propertyValue) {
  13. if (isEncryptProp(propertyName)){
  14. String decryptValue = DESUtils.getDecryptString(propertyValue);
  15. System.out.println("解密结果:"+decryptValue);
  16. return decryptValue;
  17. }else {
  18. System.out.println("无需解密:"+propertyValue);
  19. return propertyValue;
  20. }
  21. }
  22.  
  23. /*判断是否是需要进行加密的属性*/
  24. public boolean isEncryptProp(String propertyName){
  25. for (String encryptpropertyName:encryptPropNames){
  26. if (encryptpropertyName.equals(propertyName)){
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. }

配置使用

  1. <bean class="com.baobaotao.placeholder.EncryptPropertyPlaceholderConfigurer"
  2. p:location="classpath:jdbc.properties"
  3. p:fileEncoding="UTF-8"/>
  4. <!--配置数据源-->
  5. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
  6. p:driverClassName="${driverClassName}"
  7. p:url="${url}"
  8. p:username="${username_mysql}"
  9. p:password="${password}"/>

Spring 对属性文件的加密与解密的更多相关文章

  1. Spring的属性文件properties使用注意

    Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...

  2. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  3. Spring对外部属性文件指定的某个属性进行加密、解密

    [From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...

  4. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  5. Java实现文件的加密与解密

    最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...

  6. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  7. Spring配置属性文件

    在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...

  8. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  9. Java使用基本JDK操作ZIP文件以及zip文件的加密、解密等功能

    Java使用基本JDK操作ZIP文件 http://blog.csdn.net/zhyh1986/article/details/7723649 Java解压和压缩带密码的zip文件 http://b ...

随机推荐

  1. django Ajax介绍

    1.Ajax技术 认识ajax之前必须先了解json模块,json(Javascript  Obiect  Notation,JS对象标记)属于一种轻量级的数据交换格式 json的格式来源于js的格式 ...

  2. Java学习过程中的收获

    1. String <--> Date 这种转换要用到java.text.SimpleDateFormat类 字符串转换成日期类型: 方法1: 也是最简单的方法 Date date=new ...

  3. 在Linux上部署Kettle环境

    首先我们有一个正常安装的,桌面版的Linux. Kettle的应用程序是Linux版本与Windows版本在同一个文件夹下共存的,所以可以直接把本机上的Kettle解压,通过FTP工具上传到Linux ...

  4. 洛谷P1829 [国家集训队]Crash的数字表格 / JZPTAB(莫比乌斯反演)

    题目背景 提示:原 P1829 半数集问题 已经迁移至 P1028 数的计算 题目描述 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a ...

  5. Java - 关于基础数据类型的形参和返回值

    1. 当基础数据类型被当作形参时,最好使用其包装类,因为这样可方便调用者传参(基础数据类型亦或是其包装类都可)   2. 当基础数据类型被当作返回值时,最好使用原型,因为这样可以方便调用者接收返回值( ...

  6. Percona-Tookit工具包之pt-mext

      Preface       We are always obliged to analyze many outputs generated by various tools directly ev ...

  7. JDK1.8简单配置环境变量---两步曲

    鄙人最近重新装完系统之后,在安装和配置jdk1.8的时候,发现网上许多教程配置jdk环境变量时都还在沿用传统的方式配置,但是随着技术的更新,完全没有必要那么麻烦了. 下载和安装jdk的教程,在这里就不 ...

  8. DNS的主从,转发与负载功能

    接着原来<DNS原理与应用>的文章,本章内容主要通过实现DNS的主从,转发,及基于域名解析不同的ip实现后端服务负载均衡的效果.最后再实现DNS的高级功能:类似CDN原理实现基于IP实现区 ...

  9. ubuntu16 升级pip3后报错File "/usr/bin/pip3", line 9, in <module> from pip import main ImportError: cannot import name 'main'

    问题:ubuntu16 执行pip3 install --upgrade pip之后,pip3执行出错. Traceback (most recent call last): File "/ ...

  10. pc和移动端页面字体设置

    移动端项目:font-family:Tahoma,Arial,Roboto,”Droid Sans”,”Helvetica Neue”,”Droid Sans Fallback”,”Heiti SC” ...