Java实现AES,DES,RSA加密

2024-05-30 02:04
文章标签 java aes 实现 加密 rsa des

本文主要是介绍Java实现AES,DES,RSA加密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Java的Cipher类

Cipher类提供了加密和解密的功能。

Cipher类可以完成aes,des,des3和rsa等加密方式

AES加密算法

介绍

这个标准用来替代原先的DES,AES加密过程涉及到4种操作,分别是字节替代、行移位、列混淆和轮密钥加。解密过程分别为对应的逆操作。由于每一步操作都是可逆的,按照相反的顺序进行解密即可恢复明文。加解密中每轮的密钥分别由初始密钥扩展得到。算法中16个字节的明文、密文和轮密钥都以一个4x4的矩阵表示。

特点

运算速度快,安全性高,资源消耗少

实现1

package utils;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;/*** @Description: 加密* @Author zhou* @Date 2024/5/29 - 14:38*/
public class AESUtil {public static final String sKey = "111;AAAA1234++==";/*** 加密* @param crypt 输入密码* @return 返回加密数据*/public static String encrypt(Object crypt){String sSrc = String.valueOf(crypt);byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);SecretKeySpec sKeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = null;//"算法/模式/补码方式"byte[] encrypted = new byte[0];try {cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);encrypted = cipher.doFinal(sSrc.getBytes(StandardCharsets.UTF_8));} catch (Exception e) {e.printStackTrace();}return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。}/*** 解密* @param crypt 需要解密的字符串* @return 解密后的数据*/public static String decrypt(Object crypt){String sSrc = String.valueOf(crypt);// 判断Key是否正确byte[] raw = sKey.getBytes(StandardCharsets.UTF_8);SecretKeySpec sKeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = null;//"算法/模式/补码方式"byte[] original = new byte[0];try {cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, sKeySpec);byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密original = cipher.doFinal(encrypted1);} catch (Exception e) {e.printStackTrace();}return new String(original,StandardCharsets.UTF_8);}public static void main(String[] args) {// 需要加密的字串String cSrc = "123456";// 加密String enString = encrypt(cSrc);System.out.println("加密后的字串是:" + enString);// 解密String DeString = decrypt(enString);System.out.println("解密后的字串是:" + DeString);}
}

实现2

package com.demo;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;/*** AES 本身就是为了取代 DES 的,AES 具有更好的 安全性、效率 和 灵活性*/
public class AESUtils {/** 加密(对外暴露)*/public static String encryptData(String privateKey, String content) throws Exception {KeyGenerator keygen = getKeyGenerator(privateKey);SecretKey key = new SecretKeySpec(keygen.generateKey().getEncoded(), "AES");return Base64.getEncoder().encodeToString(encrypt(key, content.getBytes("UTF-8")));}/** 解密(对外暴露)*/public static String decryptData(String privateKey, String content) throws Exception {KeyGenerator keygen = getKeyGenerator(privateKey);SecretKey key = new SecretKeySpec(keygen.generateKey().getEncoded(), "AES");return new String(decrypt(key, Base64.getDecoder().decode(content)), "UTF-8");}private static KeyGenerator getKeyGenerator(String privateKey) throws NoSuchAlgorithmException {KeyGenerator keygen = KeyGenerator.getInstance("AES");SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");secureRandom.setSeed(privateKey.getBytes());keygen.init(128, secureRandom);return keygen;}private static byte[] encrypt(Key key, byte[] srcBytes) {if (key != null) {try {// Cipher负责完成加密或解密工作,基于AESCipher cipher = Cipher.getInstance("AES");// 对Cipher对象进行初始化cipher.init(Cipher.ENCRYPT_MODE, key);// 加密,保存并返回return cipher.doFinal(srcBytes);} catch (Exception e) {e.printStackTrace();}}return null;}private static byte[] decrypt(Key key, byte[] encBytes) {if (key != null) {try {Cipher cipher = Cipher.getInstance("AES");//对Cipher对象进行初始化cipher.init(Cipher.DECRYPT_MODE, key);//解密return cipher.doFinal(encBytes);} catch (Exception e) {e.printStackTrace();}}return null;}public static void main(String[] args) throws Exception {String privateKey = "ABC";String content = "ASD456";String m = encryptData(privateKey, content);System.out.println("根据私钥:" + privateKey + ",加密后的密文是:" + m);System.out.println("根据私钥:" + privateKey + ",解密后的明文是:" + decryptData(privateKey, m));}}

DES加密算法

介绍

DES是对称性加密里常见的一种,是一种使用秘钥加密的块算法。秘钥长度是64位(bit), 超过位数秘钥被忽略。所谓对称性加密,加密和解密秘钥相同。对称性加密一般会按照固定长度,把待加密字符串分成块。不足一整块或者刚好最后有特殊填充字符。

常见的填充模式有:'pkcs5'、'pkcs7'、'iso10126'、'ansix923'、'zero' 类型,包括DES-ECB、DES-CBC、DES-CTR、DES-OFB、DES-CFB。

特点

分组比较短、秘钥太短、密码生命周期短、运算速度较慢。

实现

package com.demo;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;public class DESUtils {private static Key key;private static final String PRIVATE_KEY = "ABC";static {try {KeyGenerator generator = KeyGenerator.getInstance("DES");SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");secureRandom.setSeed(PRIVATE_KEY.getBytes());generator.init(secureRandom);key = generator.generateKey();generator = null;} catch (Exception e) {e.printStackTrace();}}/*** 加密,返回BASE64的加密字符串* @param str* @return*/public static String getEncryptString(String str) throws Exception {byte[] strBytes = str.getBytes("UTF-8");Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.ENCRYPT_MODE, key);byte[] encryptStrBytes = cipher.doFinal(strBytes);return Base64.getEncoder().encodeToString(encryptStrBytes);}/*** 对BASE64加密字符串进行解密* @param str* @return*/public static String getDecryptString(String str) throws Exception {byte[] strBytes = Base64.getDecoder().decode(str);Cipher cipher = Cipher.getInstance("DES");cipher.init(Cipher.DECRYPT_MODE, key);byte[] encryptStrBytes = cipher.doFinal(strBytes);return new String(encryptStrBytes, "UTF-8");}public static void main(String[] args) throws Exception {String name = "catdog";String password = "Cat<%1?2>Dog";String encryname = getEncryptString(name);String encrypassword = getEncryptString(password);System.out.println("加密:" + encryname);System.out.println("加密:" + encrypassword);System.out.println("解密:" + getDecryptString(encryname));System.out.println("解密:" + getDecryptString(encrypassword));}
}

RSA加密算法

介绍

RSA加密算法是一种非对称加密算法,这种算法非常可靠,密钥越长,它就越难破解。

特点

  • 不需要进行密钥传递,提高了安全性

  • 可以进行数字签名认证

  • 加密解密效率不高,一般只适用于处理小量数据(如:密钥)

  • 容易遭受小指数攻击

实现

package com.demo;import org.springframework.util.StringUtils;import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;/*** RSA 加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一。RSA 是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已被 ISO 推荐为公钥数据加密标准。*/
public class RSAUtils {/*** 加密(对外暴露)* 如果使用 公钥 对数据 进行加密,只有用对应的 私钥 才能 进行解密。* 如果使用 私钥 对数据 进行加密,只有用对应的 公钥 才能 进行解密。** @param keyStr* @param data* @return* @throws Exception*/public static String encryptData(String keyStr, String data, Boolean isPublicKey) throws Exception {if (StringUtils.isEmpty(keyStr)) {return "";}return encryptBASE64(encrypt(getKey(keyStr, isPublicKey), data.getBytes()));}/*** 解密(对外暴露)* 如果使用 公钥 对数据 进行加密,只有用对应的 私钥 才能 进行解密。* 如果使用 私钥 对数据 进行加密,只有用对应的 公钥 才能 进行解密。** @param keyStr* @param data* @return* @throws Exception*/public static String decryptData(String keyStr, String data, Boolean isPublicKey) throws Exception {if (StringUtils.isEmpty(keyStr)) {return "";}return new String(decrypt(getKey(keyStr, isPublicKey), decryptBASE64(data)), "UTF-8");}/*** 加密** @param key* @param srcBytes* @return*/private static byte[] encrypt(Key key, byte[] srcBytes) {if (key != null) {try {//Cipher负责完成加密或解密工作,基于RSACipher cipher = Cipher.getInstance("RSA");//对Cipher对象进行初始化cipher.init(Cipher.ENCRYPT_MODE, key);//加密,并返回return cipher.doFinal(srcBytes);} catch (Exception e) {e.printStackTrace();}}return null;}/*** 解密** @param key* @param encBytes* @return*/private static byte[] decrypt(Key key, byte[] encBytes) {if (key != null) {try {Cipher cipher = Cipher.getInstance("RSA");//对Cipher对象进行初始化cipher.init(Cipher.DECRYPT_MODE, key);//解密并返回结果return cipher.doFinal(encBytes);} catch (Exception e) {e.printStackTrace();}}return null;}/*** 根据key获取公有或者私有key对象** @param keyStr* @param isPublicKey* @return* @throws Exception*/private static Key getKey(String keyStr, Boolean isPublicKey) throws Exception {if (isPublicKey) {return getPublicKey(keyStr);} else {return getPrivateKey(keyStr);}}/*** 根据公有key获取公有key对象** @param key* @return* @throws Exception*/private static RSAPublicKey getPublicKey(String key) throws Exception {byte[] keyBytes = Base64.getDecoder().decode(key);X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");return (RSAPublicKey) keyFactory.generatePublic(keySpec);}/*** 根据私有key获取私有对象** @param key* @return* @throws Exception*/private static RSAPrivateKey getPrivateKey(String key) throws Exception {byte[] keyBytes = Base64.getDecoder().decode(key);PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);}/*** 获取公有/私有Key** @return*/private static KeyPair getRSAKey() {KeyPair keyPair = null;try {//生成公钥和私钥对,基于RSA算法生成对象KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");//初始化密钥对生成器,密钥大小为1024位keyPairGen.initialize(1024);//生成一个密钥对,保存在keyPair中keyPair = keyPairGen.generateKeyPair();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return keyPair;}/*** 对字符串进行BASE64Decoder** @param key* @return* @throws Exception*/private static byte[] decryptBASE64(String key) {return Base64.getDecoder().decode(key);}/*** 对字节数组进行BASE64Encoder** @param key* @return* @throws Exception*/private static String encryptBASE64(byte[] key) {return Base64.getEncoder().encodeToString(key);}public static void main(String[] args) {// 生成的一对key保存好try {//得到私钥和公钥KeyPair keyPair = getRSAKey();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();String pubKey = encryptBASE64(publicKey.getEncoded());String priKey = encryptBASE64(privateKey.getEncoded());System.out.println("公钥:" + pubKey);System.out.println("私钥:" + priKey);// 测试String message = "QWERDF";System.out.println("明文:" + message);String jiami = encryptData(pubKey, message, true);System.out.println("公钥加密后:" + jiami);String jiemi = decryptData(priKey, jiami, false);System.out.println("用私钥解密后的结果是:" + jiemi);jiami = encryptData(priKey, message, false);System.out.println("私钥加密后:" + jiami);jiemi = decryptData(pubKey, jiami, true);System.out.println("用公钥解密后的结果是:" + jiemi);} catch (Exception e) {e.printStackTrace();}}}

这篇关于Java实现AES,DES,RSA加密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1015275

相关文章

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Pydantic中model_validator的实现

《Pydantic中model_validator的实现》本文主要介绍了Pydantic中model_validator的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录引言基础知识创建 Pydantic 模型使用 model_validator 装饰器高级用法mo

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

AJAX请求上传下载进度监控实现方式

《AJAX请求上传下载进度监控实现方式》在日常Web开发中,AJAX(AsynchronousJavaScriptandXML)被广泛用于异步请求数据,而无需刷新整个页面,:本文主要介绍AJAX请... 目录1. 前言2. 基于XMLHttpRequest的进度监控2.1 基础版文件上传监控2.2 增强版多

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与