idea环境下,配置文件敏感信息使用ENC加密

5 篇文章 2 订阅

在开发过程中,代码中常常会涉及到一些敏感信息,比如数据库的连接池的配置,非对称加密所要使用的私钥,账户信息、密码等。现在给大家分享一种对敏感信息进行ENC加密的一种方式,先做如下演示:

1、引入jasypt-version.jar依赖 poml依赖。

      可以去 https://mvnrepository.com/artifact/org.jasypt/jasypt  获取poml依赖

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>需要依赖的版本</version>
</dependency>

2、可以在idea中 Terminal命令窗口界面,切换本地工具依赖jasypt-1.9.2.jar所在目录中 执行命令

java -cp jasypt-1.9.2.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="hello" password=123456algorithm=PBEWithMD5AndDES
——input:为需要加密的数据,如密码,账号等
——password:为加密数据使用的密钥
——algorithm:PBEWithMD5AndDES 为默认算法(不需要修改)

 操作展示:

操作演示

正常程序中使用时,需要在配置文件中增加ENC加密的salt值

 由于写到配置文件也有风险,可以在程序启动中设置 -Djasypt.encryptor.password=123456

====================================================分隔符===============================================

 

注:加解密方式依赖于自身的拦截器,当调用到加密属性时,会通过拦截器对加密数据进行解密。

java代码实现加解密演示:

加密

public void testEncryptConfigInfo(){
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        //加密算法,写死即可
        config.setAlgorithm("PBEWithMD5AndDES");
        //加密使用salt设置
        config.setPassword("123456");
        //应用配置
        encryptor.setConfig(config);
        //需要加密数据
        String plaintext="hello";
        //加密
        String encrypttext=encryptor.encrypt(plaintext);

        System.out.println(plaintext + " : " + encrypttext);
    }

 

解密

@Test
    public void testecryptConfigInfo(){
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        //加密算法,写死即可
        config.setAlgorithm("PBEWithMD5AndDES");
        //加密使用salt设置
        config.setPassword("123456");
        //应用配置
        encryptor.setConfig(config);
        //需要加密数据
        String encrypttext="/hb4Z46weMpbdvez39myTg==";
        //加密
        String plaintext=encryptor.decrypt(encrypttext);

        System.out.println(encrypttext + " : " + plaintext);
    }

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值