当前位置: 首页 > news >正文

JAVA-3DES对称加解密工具(不依赖第三方库)


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class EncryptUtil {// 密钥public static final String ENCRYPT_KEY = "ABCD1234";// 定义加密算法,可用DES,DESede,Blowfishprivate static final String ALGORITHM_DESDE = "DESede";public static void main(String[] args) {// 加密String encPassword = desEdeEncoder("123456", EncryptUtil.ENCRYPT_KEY);System.out.println(encPassword);// 解密String desPassword = desEdeDecoder(encPassword, EncryptUtil.ENCRYPT_KEY);System.out.println(desPassword);}public static boolean isEmpty(String str) {return null == str || str.trim().length() == 0;}/*** 3DES加密** @param src* @param key* @return*/public static String desEdeEncoder(String src, String key) {byte[] bytes = null;try {SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);cipher.init(Cipher.ENCRYPT_MODE, secretKey);bytes = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));} catch (Exception e) {throw new RuntimeException(e);}return byte2HexStr(bytes);}/*** 3DES 解密** @param dest* @param key* @return*/public static String desEdeDecoder(String dest, String key) {String desStr;try {SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] bytes = cipher.doFinal(str2ByteArray(dest));desStr = new String(bytes, StandardCharsets.UTF_8);} catch (Exception e) {throw new RuntimeException(e);}return desStr;}public static String md5Digest(String src) throws NoSuchAlgorithmException {MessageDigest md5 = MessageDigest.getInstance("MD5");byte[] bytes = md5.digest(src.getBytes(StandardCharsets.UTF_8));return byte2HexStr(bytes);}private static byte[] build3DesKey(String keyStr) {byte[] bytes = new byte[24];byte[] temp = keyStr.getBytes(StandardCharsets.UTF_8);System.arraycopy(temp, 0, bytes, 0, Math.min(bytes.length, temp.length));return bytes;}private static byte[] str2ByteArray(String str) {int byteArrayLength = str.length() / 2;byte[] bytes = new byte[byteArrayLength];for (int i = 0; i < byteArrayLength; i++) {byte b0 = (byte) Integer.valueOf(str.substring(i * 2, i * 2 + 2), 16).intValue();bytes[i] = b0;}return bytes;}private static String byte2HexStr(byte[] bytes) {StringBuilder sbf = new StringBuilder();for (byte aByte : bytes) {String s = Integer.toHexString(aByte & 0xFF);if (s.length() == 1) {sbf.append("0");}sbf.append(s.toUpperCase());}return sbf.toString();}}
http://www.lryc.cn/news/165402.html

相关文章:

  • 基于Matlab卡尔曼滤波的IMU和GPS组合导航数据融合(附上源码+数据)
  • net自动排课系统完整源码(适合智慧校园)
  • Matlab匿名函数教程
  • 【Vue】一文让你进入Vue的大门
  • Linux mmap读/写触发共享文件页生命周期
  • linux 用户、组操作
  • MySQL报错this is incompatible withsal mode=only full group by处理办法
  • Mybatis 动态语言 - mybatis-freemarker
  • 软件源码开发,网络中的“摄像头”:运维监控系统
  • ping命令
  • MFC:程序的托盘显示
  • AI绘画:StableDiffusion实操教程-斗破苍穹-云韵-婚服(附高清图下载)
  • JS装饰器的介绍
  • 微信小程序(原生)使用Swiper实现(商品详情)视频和图片轮播(仿京东/淘宝商品详情头部视频+图片轮播)
  • 关于for in 循环会遍历原型链上的属性的问题
  • 冠达管理:人民币升值板块个股?
  • 27.EI文章复现《高比例清洁能源接入下计及需求响应的配电网重构》
  • mysql的索引结构
  • SMT生产中基板的机械清洁处理法有哪些
  • 微服务面试题
  • LeetCode 1132.申请的报告2
  • 室内探索无人机,解决复杂环境下的任务挑战!
  • 操作指南 | 如何参与Moonbeam投票委托
  • xxl-job中多节点分片的时候如何在linux服务器开启多个执行器实例?
  • springboot三种注入方式
  • 信息化发展38
  • PMP含金量再升级!北京上海等地可评职称!
  • 动态调用微服务
  • 什么是字符集什么是字符编码
  • Python小项目之Tkinter应用】随机点名/抽奖工具大优化:新增查看历史记录窗口!语音播报功能!修复预览文件按钮等之前版本的bug!