Java 基于Hutool实现DES加解密

打印 上一主题 下一主题

主题 896|帖子 896|积分 2688

POM.XML配置
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.shouke</groupId>
  7.     <artifactId>des-utils</artifactId>
  8.     <version>1.0</version>
  9.     <properties>
  10.         <java.version>1.8</java.version>
  11.         <maven.compiler.source>${java.version}</maven.compiler.source>
  12.         <maven.compiler.target>${java.version}</maven.compiler.target>
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15.     </properties>
  16.     <dependencies>
  17.         <dependency>
  18.             <groupId>cn.hutool</groupId>
  19.             <artifactId>hutool-all</artifactId>
  20.             <version>4.1.0</version>
  21.         </dependency>
  22.     </dependencies>
  23. </project>
复制代码
代码实现
  1. package com.shouke.utils;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.crypto.SecureUtil;
  4. import cn.hutool.crypto.symmetric.DES;
  5. import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
  6. /**
  7. * @description:对称加密
  8. */
  9. public class DesUtil {
  10.     private static final String KEY = "jPQQqFT3lwg=";
  11.     /**
  12.      * 根据KEY生成DES
  13.      */
  14.     private static final DES DES = SecureUtil.des(SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), KEY.getBytes()).getEncoded());
  15.     /**
  16.      * 获取加密后信息
  17.      *
  18.      * @param plainText 明文
  19.      * @return 加密后信息
  20.      */
  21.     public static String getEncryptData(String plainText) {
  22.         return DES.encryptHex(plainText); // 加密为16进制
  23.     }
  24.     /**
  25.      * 获取解密后信息
  26.      *
  27.      * @param cipherText 密文
  28.      * @return 解密后信息
  29.      */
  30.     public static String getDecryptData(String cipherText) {
  31.         return DES.decryptStr(cipherText);
  32.     }
  33.     /**
  34.      * 生成密钥,并转为字符串,可以储存起来,解密时可直接使用
  35.      *
  36.      * @return 密钥
  37.      */
  38.     public static String getSecretKey() {
  39.         byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded(); // 随机生成秘钥
  40.         return Base64.encode(key);
  41.     }
  42.     public static void main(String[] args) {
  43.         System.out.println(getEncryptData("shouke")); // 输出:21e995a30ccbfa38
  44.     }
  45. }
复制代码
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======
=文章篇幅过短,不允许发布,添加文字占位符,无其它含义======

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

欢乐狗

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表