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

加密解密工具类

加密解密工具类

  • package com.example.modules.util;import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;  
    import javax.crypto.SecretKey;  
    import javax.crypto.spec.SecretKeySpec;  
    import java.security.SecureRandom;  
    import java.util.Base64;  public class EncryptionUtil {  // DES加密解密  public static String desEncrypt(String data, String key) throws Exception {  SecretKey secretKey = generateDESKey(key);  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encrypted = cipher.doFinal(data.getBytes());  return Base64.getEncoder().encodeToString(encrypted);  }  public static String desDecrypt(String encryptedData, String key) throws Exception {  SecretKey secretKey = generateDESKey(key);  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedData));  return new String(original);  }  private static SecretKey generateDESKey(String key) throws Exception {  byte[] keyBytes = key.getBytes();  SecretKeySpec keySpec = new SecretKeySpec(keyBytes, 0, 8, "DES");  return keySpec;  }  // AES加密解密  public static String aesEncrypt(String data, String key) throws Exception {  SecretKey secretKey = generateAESKey(key);  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  cipher.init(Cipher.ENCRYPT_MODE, secretKey);  byte[] encrypted = cipher.doFinal(data.getBytes());  return Base64.getEncoder().encodeToString(encrypted);  }  public static String aesDecrypt(String encryptedData, String key) throws Exception {  SecretKey secretKey = generateAESKey(key);  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  cipher.init(Cipher.DECRYPT_MODE, secretKey);  byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedData));  return new String(original);  }  private static SecretKey generateAESKey(String key) throws Exception {  byte[] keyBytes = key.getBytes();  SecretKeySpec keySpec = new SecretKeySpec(keyBytes, 0, 16, "AES");  return keySpec;  }  public static void main(String[] args) {  try {  String originalText = "Hello, World!";  String desKey = "12345678"; // DES key must be 8 bytes  String aesKey = "1234567890123456"; // AES key must be 16 bytes  String desEncrypted = desEncrypt(originalText, desKey);  String desDecrypted = desDecrypt(desEncrypted, desKey);  String aesEncrypted = aesEncrypt(originalText, aesKey);  String aesDecrypted = aesDecrypt(aesEncrypted, aesKey);  System.out.println("DES Original: " + originalText);  System.out.println("DES Encrypted: " + desEncrypted);  System.out.println("DES Decrypted: " + desDecrypted);  System.out.println("AES Original: " + originalText);  System.out.println("AES Encrypted: " + aesEncrypted);  System.out.println("AES Decrypted: " + aesDecrypted);  } catch (Exception e) {  e.printStackTrace();  }  }  
    }
    
http://www.lryc.cn/news/431983.html

相关文章:

  • validationtools中按键测试选项光标移除
  • 【Hot100算法刷题集】哈希-02-字母异位词分组(含排序构造键、自定义键、自定义哈希函数法)
  • 用华为智驾,开启MPV的下半场
  • 发烧时眼睛胀痛的多种原因
  • 用ACF和PACF计算出一堆数据的周期个数以及周期时长,数据分析python
  • 生活方式对人健康影响非常大 第三篇
  • ubuntu22.04 qemu 安装windows on arm虚拟机
  • 前端框架的演变与选择
  • Oracle(109)如何管理用户密码策略?
  • 【重学MySQL】十三、基本的 select 语句
  • vue3.5新特性整理
  • RK3588 系列之3—rknn使用过程中遇到的bug
  • Java中的强引用、软引用、弱引用和虚引用于JVM的垃圾回收机制
  • 网络协议的基础知识
  • Java高级Day37-UDP网络编程
  • 如何利用ChatGPT提升学术论文讨论部分的撰写质量和效率
  • 谷歌seo网址如何快速被收录?
  • 自动驾驶---什么是Frenet坐标系?
  • 如何编写Linux PCI设备驱动器 之一
  • 梯度弥散问题及解决方法
  • Python中pickle文件操作及案例-学习篇
  • 微服务日常总结
  • C和C++内存管理
  • axios取消请求
  • 阿里中间件——diamond
  • pyenv -- 一款macos下开源的多版本python环境安装管理工具 国内加速版安装 + 项目venv虚拟环境 pip加速 使用与总结
  • VitePress 自定义 CSS 指南
  • 【舍入,取整,取小数,取余数丨Excel 函数】
  • 无线信道中ph和ph^2的场景
  • HCIA--实验五:静态路由综合实验