当前位置:网站首页>用户密码加密工具
用户密码加密工具
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>边栏推荐
猜你喜欢
随机推荐
超好用的画图工具推荐
【翻译】开发与生产中的Kubernetes修复成本对比
[Developers must see] [push kit] Collection of typical problems of push service service 2
私域流量引流方法?分享购火爆的商业模式,你值得拥有
js的垃圾回收机制
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
easyswoole的mysqli 事务怎么写
WebSocket的实际应用
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
安装ambari
CyberArk被评为2022年Gartner特权访问管理魔力象限领导者
CobalStrike(CS)基础超级详细版
13.机器学习基础:数据预处理与特征工程
【生物素叠氮化物|cas:908007-17-0】价格_厂家
工程制图第九章作业
install ambari
C#异步和多线程
寄存器(内存访问)
Redis缓存雪崩、缓存穿透、缓存击穿








