Java?基于Hutool實(shí)現(xiàn)DES加解密示例詳解
更新時(shí)間:2023年08月21日 08:33:24 作者:授客
這篇文章主要介紹了Java基于Hutool實(shí)現(xiàn)DES加解密,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
POM.XML配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shouke</groupId>
<artifactId>des-utils</artifactId>
<version>1.0</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>代碼實(shí)現(xiàn)
package com.shouke.utils;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
/**
* @description:對稱加密
*/
public class DesUtil {
private static final String KEY = "jPQQqFT3lwg=";
/**
* 根據(jù)KEY生成DES
*/
private static final DES DES = SecureUtil.des(SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), KEY.getBytes()).getEncoded());
/**
* 獲取加密后信息
*
* @param plainText 明文
* @return 加密后信息
*/
public static String getEncryptData(String plainText) {
return DES.encryptHex(plainText); // 加密為16進(jìn)制
}
/**
* 獲取解密后信息
*
* @param cipherText 密文
* @return 解密后信息
*/
public static String getDecryptData(String cipherText) {
return DES.decryptStr(cipherText);
}
/**
* 生成密鑰,并轉(zhuǎn)為字符串,可以儲存起來,解密時(shí)可直接使用
*
* @return 密鑰
*/
public static String getSecretKey() {
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded(); // 隨機(jī)生成秘鑰
return Base64.encode(key);
}
public static void main(String[] args) {
System.out.println(getEncryptData("shouke")); // 輸出:21e995a30ccbfa38
}
}到此這篇關(guān)于Java 基于Hutool實(shí)現(xiàn)DES加解密的文章就介紹到這了,更多相關(guān)Java DES加解密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中List與數(shù)組相互轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了Java中List與數(shù)組相互轉(zhuǎn)換的方法,實(shí)例分析了Java中List與數(shù)組相互轉(zhuǎn)換中容易出現(xiàn)的問題與相關(guān)的解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05
Java簡單模擬實(shí)現(xiàn)一個(gè)線程池
本文主要介紹了Java簡單模擬實(shí)現(xiàn)一個(gè)線程池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Spring框架實(shí)現(xiàn)AOP的兩種方式詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架實(shí)現(xiàn)AOP的兩種方式,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定的借鑒價(jià)值,需要的可以參考一下2022-09-09
WIN7系統(tǒng)JavaEE(java)環(huán)境配置教程(一)
這篇文章主要介紹了WIN7系統(tǒng)JavaEE(java+tomcat7+Eclipse)環(huán)境配置教程,本文重點(diǎn)在于java配置,感興趣的小伙伴們可以參考一下2016-06-06
springboot?實(shí)現(xiàn)不同context-path下的會話共享
這篇文章主要介紹了springboot?實(shí)現(xiàn)不同context-path下的會話共享,基于很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

