当前位置:网站首页>用户密码加密工具
用户密码加密工具
2022-08-03 04:35:00 【Dzooooone_】
加密类
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class CryptoUtil {
//加密算法
private static final String DESede_ALGORITHM = "DESede";
//加盐
private static final String KEY = "xxxx";
/**
* 文本加密
*
* @param plain 待加密文本
* @return
* @throws Exception
*/
public static final String encrypt(String plain) {
byte[] newKeys = getKeys(KEY);
try {
SecretKey secureKey = new SecretKeySpec(newKeys, DESede_ALGORITHM);
Cipher cipher = Cipher.getInstance(DESede_ALGORITHM);
cipher.init(1, secureKey);
byte[] encrypted = cipher.doFinal(plain.getBytes());
return new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
return null;
}
}
/**
* 密码解密
*
* @param cryptograph 待解密密码
* @return
* @throws Exception
*/
public static final String decrypt(String cryptograph) {
byte[] newKeys = getKeys(KEY);
byte[] encrypted = Base64.decodeBase64(cryptograph.getBytes());
try {
SecretKey secureKey = new SecretKeySpec(newKeys, DESede_ALGORITHM);
Cipher cipher = Cipher.getInstance(DESede_ALGORITHM);
cipher.init(2, secureKey);
byte[] cryptoGraphs = cipher.doFinal(encrypted);
return new String(cryptoGraphs);
} catch (Exception e) {
return null;
}
}
private static byte[] getKeys(String key) {
byte[] oldKeys = KEY.getBytes();
byte[] newKeys = new byte[24];
for (int i = 0; i < oldKeys.length && i != 24; i++)
newKeys[i] = oldKeys[i];
return newKeys;
}
public static void main(String[] args) throws Exception {
System.out.println(CryptoUtil.encrypt("xxxxxx"));
System.out.println(CryptoUtil.decrypt("gySSNZWJIyQ="));
System.out.println(CryptoUtil.decrypt("/4lOgQ80Y9LVB69nhkR1tA=="));
}
}对用户密码先进行加密后再入库
jwt依赖
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
</dependency>边栏推荐
猜你喜欢

2022/08/02 Study Notes (day22) Multithreading

IO进程线程->线程->day5

Concepts and Methods of Exploratory Testing

接口和协议

那些让电子工程师崩溃瞬间,你经历了几个呢?

mysql 创建索引的三种方式

社交电商:流量红利已尽,裂变营销是最低成本的获客之道

刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?

修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔

【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
随机推荐
打破传统电商格局,新型社交电商到底有什么优点?
WinForm的控件二次开发
工程制图点的投影练习
寄存器(内存访问)
荧光标记多肽FITC/AMC/FAM/Rhodamine/TAMRA/Cy3/Cy5/Cy7-Peptide
接口测试框架实战(二)| 接口请求断言
表的创建、修改与删除
【Harmony OS】【ARK UI】Date 基本操作
2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫
I ported GuiLite to STM32F4 board
install ambari
MySQL 入门:Case 语句很好用
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
Online password generator tool recommendation
技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
Redis连接不上的报错解决方案汇总
记录一些遇见的bug——mapstruct和lombok同时使用时,转换实体类时数据丢失问题
【翻译】开发与生产中的Kubernetes修复成本对比
JS底层手写
Assembly answers