Spring 对属性文件的加密与解密

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

解密/加密工具类

package com.baobaotao.placeholder;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * 加密/解密工具类
 * Created by sherry on 15-6-28.
 */
public class DESUtils {
    /*指定加密/解密使用的密匙*/
    private static Key key;
    /*注意:密匙必须是8的倍数*/
    private static String KEY_STR = "myKeykkk";
    static {
        try {
            /*防止Linux下随机生成key*/
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(KEY_STR.getBytes());

            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            //keyGenerator.init(new SecureRandom(KEY_STR.getBytes()));
            keyGenerator.init(secureRandom);
            key = keyGenerator.generateKey();
            keyGenerator = null;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /*对字符串进行DES加密,返回BASE64编码的加密字符串*/
    public static String getEncryptString(String string){
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try {
            byte[] strBytes = string.getBytes("UTF-8");
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte[] encryptStrBytes = cipher.doFinal(strBytes);
            return base64Encoder.encode(encryptStrBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /*对BASE64编码的加密字符串进行解密,返回解密后的字符串*/
    public static String getDecryptString(String str){
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            byte[] strBytes = base64Decoder.decodeBuffer(str);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE,key);
            byte[] decryptStrBytes = cipher.doFinal(strBytes);
            return new String(decryptStrBytes,"UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        args = new String[]{"root","123456"};
        String[] result = new String[2];
        if (args == null||args.length<1){
            System.out.println("请输入要加密的字符串,用空格分割");
        }else {
            int i = 0;
            for (String arg:args){
                System.out.println(arg + ":" + getEncryptString(arg));
                result[i++] = getEncryptString(arg);
            }
        }
        for (String temp:result){
            System.out.println(temp+":"+getDecryptString(temp));
        }
    }
}

属性编辑器

package com.baobaotao.placeholder;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * Created by sherry on 15-6-28.
 */
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private String[] encryptPropNames = {"username_mysql","password"};

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (isEncryptProp(propertyName)){
            String decryptValue = DESUtils.getDecryptString(propertyValue);
            System.out.println("解密结果:"+decryptValue);
            return decryptValue;
        }else {
            System.out.println("无需解密:"+propertyValue);
            return propertyValue;
        }
    }

    /*判断是否是需要进行加密的属性*/
    public boolean isEncryptProp(String propertyName){
        for (String encryptpropertyName:encryptPropNames){
            if (encryptpropertyName.equals(propertyName)){
                return true;
            }
        }
        return false;
    }
}

配置使用

    <bean class="com.baobaotao.placeholder.EncryptPropertyPlaceholderConfigurer"
          p:location="classpath:jdbc.properties"
          p:fileEncoding="UTF-8"/>
    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="${driverClassName}"
          p:url="${url}"
          p:username="${username_mysql}"
          p:password="${password}"/>
时间: 2024-08-04 04:17:09

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

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!

Java小项目之:文件的加密与解密!再也不怕存的小电影被别人发现了!今天带来的java小项目是加密解密系统,再也不怕别人偷看自己的电脑了,也可以正大光明的存小电影了.减少借别人电脑被看隐私的尴尬,从这个项目开始!界面展示: 部分代码展示:package wt.diy.encryption.gui; import java.io.File; import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JO

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

实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.properties中. 其中部分配置信息(邮件发送相关): Java代码 #邮件发送的相关配置 email.host = smtp.163.com email.port = xxx email.username = xxx email.password = xxx email.sendFrom = xxx@16

spring导入属性文件

spring2.5之后导入属性文件的方法 在XML中加入 <context:property-placeholder location="classpath:db.properties"/> <bean id="" class=""> <property name="user" value="${user}"><property/> </bean>

Spring读取加密属性文件处理

引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.AOP及和其它框架的很好集成(如:hibername.ibatis.struts等)确实给web项目开发带来了诸多便利性,但是任何一种框架都不能完全满足个性化需求开发,spring亦是如此.现有一个项目是基于spring.struts和ibtatis的,其中数据库连接池使用的是proxool,领导要求

信息安全之仿射密码加密和解密

本文利用仿射密码,对一个只含可打印字符的txt文件进行加密和解密. //加解密时,文件内容为所有可打印字符,即ASCII码从32-126的95个字符 #include<iostream> #include<fstream> using namespace std; //加密类 class Encrypt { public: void set_AB(int a, int b) {A = a; B = b;} //设置加密密钥 int gcd(int a, int b) { while

在Linux环境下使用OpenSSL对消息和文件进行加密(转载)

转自:http://netsecurity.51cto.com/art/201301/378513.htm 1.简介 OpenSSL是一款功能强大的加密工具包.我们当中许多人已经在使用OpenSSL,用于创建RSA私匙或证书签名请求(CSR).不过,你可曾 知道可以使用OpenSSL来测试计算机速度?或者还可以用它来对文件或消息进行加密?本文将介绍几个简单易学的技巧,教你如何使用OpenSSL对消息 和文件进行加密. [相关推荐]:网络安全工具百宝箱 2.对消息进行加密和解密 首先,我们不妨对简

java spring中对properties属性文件加密及其解密

原创整理不易,转载请注明出处:java spring中对properties属性文件加密及其解密 代码下载地址:http://www.zuidaima.com/share/1781588957400064.htm 加密类: package com.zuidaima.commons.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import

Spring Boot加密属性文件数据

项目中敏感信息一般需要进行加密处理,比如数据库密码,Spring Boot内置不提供加密支持,不能加密属性文件的数据,在官方文档中提供了自定义Environment和Spring Cloud Vault两种解决方案.另外,可以使用jasypt-spring-boot. Jasypt Spring Boot 集成jasypt-spring-boot 有三种方式集成jasypt-spring-boot: 项目中如使用了@SpringBootApplication或@EnableAutoConfigu

Spring Cloud Config 加密和解密

先决条件:要使用加密和解密功能,您需要在JVM中安装全面的JCE(默认情况下不存在).您可以从Oracle下载"Java加密扩展(JCE)无限强度管理策略文件",并按照安装说明(实际上将JRE lib / security目录中的2个策略文件替换为您下载的文件). 如果远程属性源包含加密内容(以{cipher}开头的值),则在通过HTTP发送到客户端之前,它们将被解密.这种设置的主要优点是,当它们"静止"时,属性值不必是纯文本(例如在git仓库中).如果值无法解密,