当前位置:网站首页>【加密解密】明文加密解密-已实现【已应用】
【加密解密】明文加密解密-已实现【已应用】
2022-08-05 10:30:00 【刘贵庆】
目录:
一、加载jasypt包。
二、jasypt配置、数据库配置
三、项目正常运行
四、加密解密方法
五、报错及解决方法
具体内容:
一、加载jasypt包
gradle配置:
implementation group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.4'
或者:pom.xml文件:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
二、jasypt配置、数据库配置
application.properties文件:
#123wes为自定义加密盐:(jasypt配置一定要放最上面)
jasypt.encryptor.password=123wes
#加密方式 (切勿修改,若修改:同加密方法一并修改。)
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.algorithm=PBEWithMD5AndDES
#数据连接配置:
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/databasesName?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username= ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
spring.datasource.password= ENC(dfCDWWWEEgs4YXDDFKKK9kGW)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
或者:yml文件:
jasypt:
encryptor:
password:123wes
iv-generator-classname:org.jasypt.iv.NoIvGenerator
algorithm:PBEWithMD5AndDES
#数据库配置:
spring:
datasource:
jdbc-url:mysql://localhost:3306/databasesName?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username: ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
password: ENC(dfCDWWWEEgs4YXDDFKKK9kGW)
driver-class-name:com.mysql.cj.jdbc.Driver
三、正常运行
至此;点击Run运行项目。可正常运行。
四、加密解密方法
用户密码加密解密 :(注意:仅用来获取密码加密字符串,之后去掉,打包时不打此文件。)
TestJasypt.java文件:
package xxx;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class TestJasypt {
//获取密文:
public static void main(String[] args) {
// 加密
//password为自定义:加密盐,value为需要加密的字符串
System.out.println(setJasypt("123wes", "[email protected]"));
// 解密
password为加密盐,value为需要解密的:加密密文。
System.out.println(getJasypt("123wes", "Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6"));
}
/* 输出:密文:
Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6
// 放入配置文件如:password: ENC(Bf1Y3jZLLuLnkX1T2kXu9/r/Pil//fM6)
解密后:
[email protected]
*/
//注意:发布时,不加载此文件。
public static String setJasypt(String password, String value) {
PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
encryptOr.setConfig(ssPBEC(password));
String result = encryptOr.encrypt(value);
return result;
}
public static String getJasypt(String password, String value) {
PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
encryptOr.setConfig(ssPBEC(password));
String result = encryptOr.decrypt(value);
return result;
}
public static SimpleStringPBEConfig ssPBEC(String password) {
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName(null);
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
return config;
}
}
五、报错及解决方法
报错:Failed to bind properties under 'spring.datasource.password' to java.lang.String:
边栏推荐
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- 百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
- How to choose coins and determine the corresponding strategy research
- Import Excel/CSV from Sub Grid within Dynamics 365
- Create a Dapp, why choose Polkadot?
- FPGA: Basic Getting Started Button Controlling LED Lights
- 第八章:activiti多用户任务分配
- Header file search rules when compiling with GCC
- 入门 Polkadot 平行链开发,看这一篇就够了
- STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
猜你喜欢
three objects are arranged in a spherical shape around the circumference
反射修改jsessionid实现Session共享
The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
E-sports, convenience, efficiency, security, key words for OriginOS functions
基于MindSpore高效完成图像分割,实现Dice!
Introduction to SD NAND Flash!
three.js debugging tool dat.gui use
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
随机推荐
Latex如何控制表格的宽度和高度
入门 Polkadot 平行链开发,看这一篇就够了
Header file search rules when compiling with GCC
七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
SQL Outer Join Intersection, Union, Difference Query
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
第五章:activiti流程分流判断,判断走不同的任务节点
E-sports, convenience, efficiency, security, key words for OriginOS functions
Is digital transformation a business buy-in?
three objects are arranged in a spherical shape around the circumference
Create a Dapp, why choose Polkadot?
2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
数据可视化(一)
【Office】Microsoft Office下载地址合集(微软官方原版离线安装下载)
LeetCode 216. Combined Sum III (2022.08.04)
Chapter 5: Activiti process shunting judgment, judging to go to different task nodes
一张图看懂 SQL 的各种 join 用法!
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number