当前位置:网站首页>【加密解密】明文加密解密-已实现【已应用】
【加密解密】明文加密解密-已实现【已应用】
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:
边栏推荐
- SMB + SMB2: Accessing shares return an error after prolonged idle period
- 华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
- 多线程(进阶) - 2.5w字总结
- First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
- R语言ggplot2可视化:可视化密度图(Density plot)、可视化多个分组的密度图、数据点分布在箱图中间、添加主标题、副标题、题注信息
- poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
- FPGA:基础入门LED灯闪烁
- 气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
- Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
- The fuse: OAuth 2.0 four authorized login methods must read
猜你喜欢
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
Introduction to SD NAND Flash!
Opencv算术操作
【温度预警程序de开发】事件驱动模型实例运用
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
Data Middle Office Construction (10): Data Security Management
RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
随机推荐
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
Go compilation principle series 6 (type checking)
语音社交软件开发——充分发挥其价值
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
第六章:activiti流程分流判断之排它网关和并行网关
导火索:OAuth 2.0四种授权登录方式必读
产品太多了,如何实现一次登录多产品互通?
攻防世界-PWN-new_easypwn
Chapter 4: In the activiti process, variable transmission and acquisition process variables, setting and acquiring multiple process variables, setting and acquiring local process variables "recommende
SD NAND Flash简介!
FPGA:基础入门LED灯闪烁
This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
什么是 DevOps?看这一篇就够了!
项目成本控制如何帮助项目成功?
一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
DFINITY 基金会创始人谈熊市沉浮,DeFi 项目该何去何从
R语言使用yardstick包的pr_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的ROC曲线(precision(精准率),R代表的是recall(召回率)
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
linux下oracle常见操作以及日常积累知识点(函数、定时任务)