EncryptUtil工具类utils:
package deployment.utils;import deployment.constant.CommonConstant;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;/*** @description: 加密解密工具**/
public class EncryptUtil {public static final String DES = "DES";/**DES*/public int keysizeDES = 0;/**编码格式;默认使用uft-8*/public String charset = "utf-8";//单例public static EncryptUtil Instance;private EncryptUtil(){}//双重锁public static EncryptUtil getInstance(){if(Instance == null){synchronized (EncryptUtil.class){if(Instance == null){Instance = new EncryptUtil();}}}return Instance;}/*** 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错* @param res 加密的原文* @param algorithm 加密使用的算法名称* @param key  加密的秘钥* @param keysize* @param isEncode* @return*/private String keyGeneratorES(String res,String algorithm,String key,int keysize,boolean isEncode){try {KeyGenerator kg = KeyGenerator.getInstance(algorithm);if (keysize == 0) {byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);kg.init(new SecureRandom(keyBytes));}else if (key==null) {kg.init(keysize);}else {byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);kg.init(keysize, new SecureRandom(keyBytes));}SecretKey sk = kg.generateKey();SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);Cipher cipher = Cipher.getInstance(algorithm);if (isEncode) {cipher.init(Cipher.ENCRYPT_MODE, sks);byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);return parseByte2HexStr(cipher.doFinal(resBytes));}else {cipher.init(Cipher.DECRYPT_MODE, sks);return new String(cipher.doFinal(parseHexStr2Byte(res)));}} catch (Exception e) {e.printStackTrace();}return null;}/**将二进制转换成16进制 */public static String parseByte2HexStr(byte buf[]) {StringBuffer sb = new StringBuffer();for (int i = 0; i < buf.length; i++) {String hex = Integer.toHexString(buf[i] & 0xFF);if (hex.length() == 1) {hex = '0' + hex;}sb.append(hex.toUpperCase());}return sb.toString();}/**将16进制转换为二进制*/public static byte[] parseHexStr2Byte(String hexStr) {if (hexStr.length() < 1)return null;byte[] result = new byte[hexStr.length() / 2];for (int i = 0; i < hexStr.length() / 2; i++) {int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);result[i] = (byte) (high * 16 + low);}return result;}/*** 使用Base64进行加密* @param res 密文* @return String*/public String Base64Encode(String res){return Base64.encode(res.getBytes(StandardCharsets.UTF_8));}/*** 使用Base64进行解密* @param res 密文* @return String*/public String Base64Decode(String res) {return new String(Base64.decode(res));}/*** 使用DES加密算法进行加密(可逆)* @param res 需要加密的原文* @return String*/public String DESencode(String res) {return keyGeneratorES(res, DES, CommonConstant.SECRET_KEY, keysizeDES, true);}/*** 对使用DES加密算法的密文进行解密(可逆)* @param res 需要解密的密文* @return String*/public String DESdecode(String res) {return keyGeneratorES(res, DES, CommonConstant.SECRET_KEY, keysizeDES, false);}}

其中调用了一个公共配置类:

package deployment.constant;/*** 公共常用配置类*/
public class CommonConstant {//加解密密钥(可自行修改未不同的单词)public static final String SECRET_KEY = "IMPROVE";}

使用方法:

加密:

                String password = EncryptUtil.getInstance().DESencode(chkDatabase.getPassword());

解密:

        String password = EncryptUtil.getInstance().DESdecode(chkDatabase.getPassword());

EncryptUtil加密解密工具类,实测可以,复制粘贴皆可。全套代码加使用案例方法。相关推荐

  1. java des加密解密_Java实现的DES加密解密工具类实例

    本文实例讲述了Java实现的DES加密解密工具类.分享给大家供大家参考,具体如下: 一个工具类,很常用,不做深入研究了,那来可直接用 DesUtil.java package lsy; import ...

  2. java字符串加密解密工具类

    /*** 字符串加密解密工具类** @author guyuqiang* @date 2021-06-07*/ public class StringEncryptUtil {/*** 字符串默认键值 ...

  3. java des 加密工具的使用,Java中DES加密解密工具类的实现实例

    这篇文章主要介绍了Java实现的DES加密解密工具类,结合具体实例形式分析了Java实现的DES加密解密工具类定义与使用方法,需要的朋友可以参考下 本文实例讲述了Java实现的DES加密解密工具类.分 ...

  4. Java AES 加密解密工具类

    maven 引入一个依赖 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <depen ...

  5. jwt加密解密工具类

    jwt 加密&解密工具类 引入依赖 <dependency><groupId>io.jsonwebtoken</groupId><artifactId ...

  6. 常用的字符串加密解密工具类

    提前声明一下:此工具类忘记出处了,很抱歉,如果原创遇上了我立马上您的链接: 加密解密的工具类DataEncryptUtil: package org.util;import java.security ...

  7. java aes 工具类_AESUtil Java AES 加密解密工具类

    package com.singlee.util; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; impo ...

  8. SM2加密解密工具类

    Maven依赖 <dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jd ...

  9. Android AES加密解密工具类

    一个用于Android AES加密解密的工具类,记录一下... import android.os.Build import android.security.keystore.KeyGenParam ...

最新文章

  1. 滴滴算法大赛算法解决过程(实时更新)
  2. 凡普金科以互金 “头马”入选互联网企业百强的启示:创新为王
  3. Intervals on the Ring 环状数轴区间-模数-构造-区间交并集
  4. 文本分析工具 数据科学_数据科学工具
  5. PHP表单提交后页面跳转,PHP在表单提交后重定向到另一个页面
  6. 3详细参数_大疆精灵3值得入手吗?最详细的实测体验,各种参数应有尽有!
  7. python特征选择后显示选取的特征名_python实现求特征选择的信息增益
  8. use tool wget for windows download
  9. OS X Mavericks 10.9.3 (13D65) 官方原版 懒人版安装镜像
  10. 支持断电保护的FAT文件系统
  11. 安徽省计算机二级水平考试试卷,安徽省计算机二级考试理论试题(附答案)
  12. 张小龙演讲PPT: APP产品经理必须要懂的30条原则
  13. python窗口设置背景图片_PyQt5 实现给窗口设置背景图片的方法
  14. WEBERP测试实录:一 webERP安装
  15. + kt360buy - 牛肉丸是用牛的什么部位做的
  16. mysqld_safe启动mysql数据库的参数
  17. 方正如何禁止从网络启动计算机,我来问问方正电脑怎么关网卡启动
  18. 无人机配聚合路由器提供应急网络保障
  19. iPhone之随机数的生成
  20. 记录一些最近用过的编码转换

热门文章

  1. 如何把一个锅刷成电影质感?【转】
  2. AutoCAD 如何将非1:1比例尺统一改为1:1
  3. CPU部件实现之ALU、寄存器堆、PC、RAM
  4. EasyPoi通过模板生成Excel文件
  5. 汉字点阵字库的原理与显示
  6. 警告记录 - [Synth 8-3331] design has unconnected port
  7. 数据库课程实习设计——酒店房间预订管理系统
  8. AUTOCAD 每次打开一个图纸都重新运行程序ACAD.EXE
  9. SPSS-多重响应分析
  10. 计算机公式运算符,excel加减乘除-office零基础—Excel篇第25课「认识公式中的运算符」...