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

Spring 管理下的web项目,对Properties文件重要参数的加密解密处理 javaWeb项目中往往我们会把jdbc/redis等的属性配置放在properties文件中,但是又不想让非开发人员看到某些重要配置信息,所以需要对这样类似与用户名、密码等的字段进行加密,但是Spring管理项目启动时候只去读取,所以我们必须继承PropertyPlaceholderConfigurer 类来实现读取前的解密过程加密类import java.io.FileInputStream; 阅读详情

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

源代码下载地址:http://www.zuidaima.com/share/1781588957400064.htm


加密类:

package com.framework.commons.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

/**
 * <ul>
 * <li>Title:[DESEncryptUtil]</li>
 * <li>Description: [加密码解密类]</li>
 * <li>Copyright 2009 RoadWay Co., Ltd.</li>
 * <li>All right reserved.</li>
 * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
 * <li>Midified by [修改人] [修改时间]</li>
 * </ul>
 * 
 * @version 1.0
 */
public class DESEncryptUtil
{
    public static void main(String[] args) throws Exception
    {
        /** 生成KEY */
        String operatorType = "key";
        String keyFilePath = "D:/key.k";
        DESEncryptUtil.test(keyFilePath, null, operatorType);
        
        /** 加密 */
        operatorType = "encrypt";
        String sourceFilePath = "D:/jdbc_official.properties";
        DESEncryptUtil.test(keyFilePath, sourceFilePath, operatorType);
        
        /** 解密 */
        operatorType = "decrypt";
        sourceFilePath = "D:/en_jdbc_official.properties";
        DESEncryptUtil.test(keyFilePath, sourceFilePath, operatorType);
    }
    /**
     * <ul>
     * <li>Description:[创建一个密钥]</li>
     * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
     * <li>Midified by [修改人] [修改时间]</li>
     * </ul>
     * 
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static Key createKey() throws NoSuchAlgorithmException
    {
        Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1);
        KeyGenerator generator = KeyGenerator.getInstance("DES");
        generator.init(new SecureRandom());
        Key key = generator.generateKey();
        return key;
    }
    
    /**
     * <ul>
     * <li>Description:[根据流得到密钥]</li>
     * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
     * <li>Midified by [修改人] [修改时间]</li>
     * </ul>
     * 
     * @param is
     * @return
     */
    public static Key getKey(InputStream is)
    {
        try
        {
            ObjectInputStream ois = new ObjectInputStream(is);
            return (Key) ois.readObject();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    /**
     * <ul>
     * <li>Description:[对数据进行加密]</li>
     * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
     * <li>Midified by [修改人] [修改时间]</li>
     * </ul>
     * 
     * @param key
     * @param data
     * @return
     */
    private static byte[] doEncrypt(Key key, byte[] data)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] raw = cipher.doFinal(data);
            return raw;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    /**
     * <ul>
     * <li>Description:[对数据进行解密]</li>
     * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
     * <li>Midified by [修改人] [修改时间]</li>
     * </ul>
     * 
     * @param key
     * @param in
     * @return
     */
    public static InputStream doDecrypt(Key key, InputStream in)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] tmpbuf = new byte[1024];
            int count = 0;
            while ((count = in.read(tmpbuf)) != -1)
            {
                bout.write(tmpbuf, 0, count);
                tmpbuf = new byte[1024];
            }
            in.close();
            byte[] orgData = bout.toByteArray();
            byte[] raw = cipher.doFinal(orgData);
            ByteArrayInputStream bin = new ByteArrayInputStream(raw);
            return bin;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    private static void test(String keyFilePath, String sourceFilePath,
            String operatorType) throws Exception
    {
        // 提供了Java命令使用该工具的功能
        if (operatorType.equalsIgnoreCase("key"))
        {
            // 生成密钥文件
            Key key = DESEncryptUtil.createKey();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keyFilePath));
            oos.writeObject(key);
            oos.close();
            System.out.println("成功生成密钥文件" + keyFilePath);
        }
        else if (operatorType.equalsIgnoreCase("encrypt"))
        {
            // 对文件进行加密
            File file = new File(sourceFilePath);
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] tmpbuf = new byte[1024];
            int count = 0;
            while ((count = in.read(tmpbuf)) != -1)
            {
                bout.write(tmpbuf, 0, count);
                tmpbuf = new byte[1024];
            }
            in.close();
            byte[] orgData = bout.toByteArray();
            Key key = getKey(new FileInputStream(keyFilePath));
            byte[] raw = DESEncryptUtil.doEncrypt(key, orgData);
            file = new File(file.getParent() + "\\en_" + file.getName());
            FileOutputStream out = new FileOutputStream(file);
            out.write(raw);
            out.close();
            System.out.println("成功加密,加密文件位于:" + file.getAbsolutePath());
        }
        else if (operatorType.equalsIgnoreCase("decrypt"))
        {
            // 对文件进行解密
            File file = new File(sourceFilePath);
            FileInputStream fis = new FileInputStream(file);
            
            Key key = getKey(new FileInputStream(keyFilePath));
            InputStream raw = DESEncryptUtil.doDecrypt(key, fis);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] tmpbuf = new byte[1024];
            int count = 0;
            while ((count = raw.read(tmpbuf)) != -1)
            {
                bout.write(tmpbuf, 0, count);
                tmpbuf = new byte[1024];
            }
            raw.close();
            byte[] orgData = bout.toByteArray();
            file = new File(file.getParent() + "\\rs_" + file.getName());
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(orgData);
            System.out.println("成功解密,解密文件位于:" + file.getAbsolutePath());
        }
    }
}


DecryptPropertyPlaceholderConfigurer.java

package com.framework.spring;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.Key;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

import com.framework.commons.util.DESEncryptUtil;

public class DecryptPropertyPlaceholderConfigurer extends
        PropertyPlaceholderConfigurer
{
    private Resource[] locations;
    
    private Resource keyLocation;
    
    private String fileEncoding;
    
    public void setKeyLocation(Resource keyLocation)
    {
        this.keyLocation = keyLocation;
    }
    
    public void setLocations(Resource[] locations)
    {
        this.locations = locations;
    }
    
    public void loadProperties(Properties props) throws IOException
    {
        if (this.locations != null)
        {
            PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
            for (int i = 0; i < this.locations.length; i++)
            {
                Resource location = this.locations[i];
                if (logger.isInfoEnabled())
                {
                    logger.info("Loading properties file from " + location);
                }
                InputStream is = null;
                try
                {
                    is = location.getInputStream();
                    Key key = DESEncryptUtil.getKey(keyLocation.getInputStream());
                    is = DESEncryptUtil.doDecrypt(key, is);
                    if (fileEncoding != null)
                    {
                        propertiesPersister.load(props, new InputStreamReader(
                                is, fileEncoding));
                    }
                    else
                    {
                        propertiesPersister.load(props, is);
                    }
                }
                finally
                {
                    if (is != null)
                    {
                        is.close();
                    }
                }
            }
        }
    }
}


配置文件:

<!-- 加密码属性文件 -->
    <bean id="myPropertyConfigurer"
        class="com.framework.spring.DecryptPropertyPlaceholderConfigurer">
        <property name="locations">
            <list><value>classpath*:spring_config/jdbc_official.databaseinfo</value></list>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
        <property name="keyLocation" value="classpath:spring_config/key.key" />
    </bean>
spdk + fio测试nvme 设备的性能 fio的测试对象可以是块设备、文件等,在spdk的使用过程中会unbind默认的nvme driver,所以在系统中是看不到nvme块设备的,在spdk中可以通过fio_plugin的方式,将spdk的用户态driver部分的io功能打包成一个ioengine提供给fio使用,可以综合spdk的高性能和fio提供的复杂场景。更多DPDK相关学习资料有需要的可以自行报名学习,免费订阅,永久学习,或点击这里加。这个jobfile是spdk中提供的例子。领取,关注我持续更新哦!2.7 jobfile的例子。 阅读详情

相关推荐

小爱音箱Play增强版刷机接入大模型:自定义唤醒词与远程控制全攻略

智能音箱作为家庭语音入口,受限于厂商预设技能,难以满足个性化需求。通过串口刷机与Linux系统分区改造,可以完全掌控设备底层,将豆包、通义千问等大模型API接入原有音频链路,实现真正智能的语音问答。自定义唤醒词打破了“小爱同学”的唯一唤醒方式,结合ESP32-S3外挂模块,还能优化远场拾音。更深入的应用在于将音箱打造为家庭中控,通过大模型工具调用解析自然语言指令,实现远程控制电脑、智能灯等设备。本文从设备选型、拆机串口、系统备份到语音链路搭建,完整梳理了小爱音箱Play增强版的改造路径,适合具备动手能力的开

weixin_30036289的博客 960

Java 数据加密解密及脱敏处理

在开发中会遇到一些敏感信息 需要做加密和脱敏处理,以下是加密解密实战

qq_41867674的博客 2181

5分钟搞定!用FunASR在本地快速部署达摩院Paraformer语音识别模型(附常见问题解决)

本文详细介绍了如何在本地快速部署阿里达摩院开源的Paraformer语音识别模型。通过FunASR工具包,用户可在5分钟内完成环境配置、模型下载与基础识别,并掌握批量处理、性能调优及构建API服务等进阶技巧。文章重点解析了Paraformer的非自回归架构带来的速度优势,并提供了音频格式转换、依赖冲突、显存不足等常见问题的实战解决方案,助力开发者实现高效、离线的语音转写应用。

p1q2r的博客 715

SpringBoot--代码方式读取properties出现异常解决

问题 问题出现的代码 String ss = Objects.requireNonNull(app.getClass().getClassLoader().getResource(config.properties&amp;quot;)).getPath(); System.out.println(&amp;quot;当前获取的地址为: &amp;quot; + ss); try { F...

细心程度决定你的成败 1508

自学ROS完成智能小车路径规划(一)--配置功能包

此系列文章记录本人学习ros的过程,将会尽可能详细以便自己能够看懂 参考系列:古月居老师,CSDN,ROS tutorials,科大讯飞智能车竞赛线上赛 本文章的目标: 比较全局路径规划算法中A*算法与Dijkstra算法在该地图场景下的规划与运行时间 比较局部路径规划算法中DWA算法与TEB算法在该地图场景下的规划与运行时间 在同等参数条件下选择出各自适合的算法进行融合,经过比较,我将选择dijkstra算法作为全局路径规划算法,TEB算法为局部路径规划算法 第一步,按照科大讯飞的教程配置文件 科大讯

weixin_42621524的博客 9381

手把手教你用yt-dlp批量下载YouTube视频数据集(附TikTok/Kaggle资源)

本文提供了一份构建视频数据集的实战指南,重点介绍了使用yt-dlp工具批量下载YouTube视频的高级技巧,包括环境配置、批量参数设置与自动化脚本。同时,文章还整合了TikTok、Kaggle及学术开源数据集等多源高质量视频资源,并分享了数据清洗、格式统一与管理的完整流程,帮助研究者和开发者高效获取与处理训练数据。

weixin_29057237的博客 310

properties配置文件的加密

要完成properties属性文件某些属性值的加密,和读取属性文件时进行解密,需要4个步骤 编写加密解密工具类 手动通过加密解密工具类获得加密后的属性值密文,并把密文填写在properties文件中 编写PropertyPlaceholderConfigurer的子类,重写convertProperty()方法 在spring-dao.xml配置文件中配置PropertyPlaceholderConfigurer类 接下来我们将拿配置数据库的properties文件进行举例(一般我们需要对用户名和密码进

qq_43842093的博客 3020

每天一个优秀提示词学习收藏 - 文本创作篇(二)

宝子们,今天不藏私啦!我把这段时间四处搜罗、潜心钻研出来的文本创作 Prompt 提示词大礼包双手奉上,这可都是能让你的 AI 创作水平原地起飞的干货!走过路过不要错过,赶紧码住,开启你的 AI 创作进阶之旅吧!

2423

DES加密,前端示例,Java示例,在线测试

DES加密

山月神话 921

嵌入式模拟电路面试题大全及参考答案(持续更新)

高输入阻抗:由于晶体管的基极-发射极(BJT)或栅极-源极(MOSFET)电压很低,跨导放大器具有很高的输入阻抗。低输出阻抗:晶体管的集电极-发射极或漏极-源极电阻较低,提供较低的输出阻抗。电压控制:输入信号控制晶体管的跨导,从而控制输出电流。晶体管:作为放大器的核心,可以是BJT或MOSFET。偏置电路:为晶体管提供稳定的工作点,确保其在放大区域内工作。负载电阻:与晶体管的集电极或漏极连接,将电流转换为电压。晶体管的选择:根据所需的增益、速度和功率消耗选择合适的晶体管。偏置稳定性。

大模型大数据攻城狮的专栏 994

【linux】解决报错:Network error: Connection refused

【linux】解决报错:Network error: Connection refused

weixin_44502754的博客 4954

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

加密类:package com.framework.commons.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;impor...

weixin_29483853的博客 221

手把手教你学matlab(8.33)--simulink:DC-DC 双向 Buck-Boost 转换器仿真实例

通过以上步骤,你应该能够建立一个带有 PI 控制器的 DC-DC 双向 Buck-Boost 转换器仿真模型,并观察其性能。Simulink 提供了丰富的工具和模块,可以帮助你进一步优化和调整仿真模型。

MHD0815的博客 963

java properties加密_java spring中对properties属性文件加密及其解密 | 学步园

加密类:package com.zuidaima.commons.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import...

weixin_39627430的博客 307

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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

之前在项目里面连接数据库需要将密码解密,而且连接好多数据库,并且有的数据库需要解密有的直接连接就可以。这样代码的可读性特别低,在连接数据库的时候都要现连接:Properties prop = new Properties();prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("lecture.p...

weixin_30017913的博客 366

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Android ADB WiFi调试实战:从零配置到车载/机顶盒破局

ADB(Android Debug Bridge)是Android系统底层调试的核心工具,其WiFi调试模式本质是通过TCP/IP协议重定向Client-Server-Device通信链路,绕过USB物理依赖,实现网络化、去中心化的设备管控。该技术基于adbd守护进程监听指定端口(默认5555),结合RSA密钥信任机制与局域网可达性,提供免线连接、多设备并发、跨平台操作等关键能力。广泛应用于真机测试、自动化部署、车载系统调试及无USB接口嵌入式设备(如机顶盒、中控屏)的远程运维场景。本文聚焦adb WiFi

weixin_34133829的博客 361

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Spring Cloud 加密和解密

加密和解密 重要 要使用加密和解密功能,您需要在JVM中安装完整功能的JCE(默认情况下不包括)。您可以从Oracle下载“Java密码学扩展(JCE)无限强度辖区策略文件”并按照安装说明进行操作(本质上,您需要用下载的JRE lib / security目录替换这两个策略文件)。 如果远程属性源包含加密的内容(以开头的值{cipher}...

大强博客 2056

java properties 锁_spring--给配置文件.properties加密

11111111111编写类并继承PropertyPlaceholderConfigurer.javapackage com.xx.encryptDecrypt;import java.util.HashMap;import java.util.Map;import java.util.Properties;import org.apache.log4j.Logger;import org.spr...

weixin_30043049的博客 237
上一篇: 史上最全的maven pom.xml文件教程详解
下一篇: java开源框架SpringSide 3.1.4.3开发Web的demo项目实战
yaerfeng
博客等级 码龄17年 1697粉丝 665原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值