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

java的AES加密算法和RSA非对称加密算法

一、AES加密算法:

 

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;public class AESEncryption {public static void main(String[] args) throws Exception {// 生成密钥KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");keyGenerator.init(128); // 可以是128, 192或256位SecretKey secretKey = keyGenerator.generateKey();byte[] keyBytes = secretKey.getEncoded();SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");// 加密Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, keySpec);String plainText = "Hello World";byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);System.out.println("Encrypted: " + encryptedText);// 解密
        cipher.init(Cipher.DECRYPT_MODE, keySpec);byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));String decryptedText = new String(decryptedBytes);System.out.println("Decrypted: " + decryptedText);}
}

 

 

 

二、RSA非对称加密:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.Cipher;public class RSAEncryption {public static void main(String[] args) throws Exception {// 生成密钥对KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");keyPairGenerator.initialize(2048); // 可以是1024, 2048等大小KeyPair keyPair = keyPairGenerator.generateKeyPair();PublicKey publicKey = keyPair.getPublic();PrivateKey privateKey = keyPair.getPrivate();// 加密Cipher encryptCipher = Cipher.getInstance("RSA");encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);String plainText = "Hello World";byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes());String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);System.out.println("Encrypted: " + encryptedText);// 解密Cipher decryptCipher = Cipher.getInstance("RSA");decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedText));String decryptedText = new String(decryptedBytes);System.out.println("Decrypted: " + decryptedText);}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

http://www.jsqmd.com/news/259649/

相关文章:

  • 图的基本概念
  • 物联网数据中台建设方法论与实践
  • 探寻不锈钢管板好货源?2026年国内厂家推荐,高温合金法兰/压力容器法兰/非标法兰/双相钢法兰,不锈钢管板公司有哪些 - 品牌推荐师
  • java-ssm324医院预约挂号系统vue问诊 失信 投诉-springboot
  • 一篇文章看懂 spring-boot-starter-web 的 POM 配置与 compile 作用域
  • 深度学习毕设项目推荐-基于python-CNN卷积神经网络训练识别不同颜色的裤子识别
  • 2026年目前服务好的双相钢法兰供应商选哪家,不锈钢法兰/双相钢法兰/非标法兰/变压器法兰,双相钢法兰直销厂家排行 - 品牌推荐师
  • Maven 依赖作用域实战避坑指南
  • 2026年目前做得好的变压器法兰品牌有哪些,不锈钢管板/压力容器法兰/不锈钢法兰/法兰/船用法兰,变压器法兰厂家推荐 - 品牌推荐师
  • 深度学习毕设项目推荐-基于python-CNN-pytorch训练识别苹果树叶病害识别
  • 企业估值中的可穿戴设备市场评估
  • 10 分钟使用 OrchardCore 快速构建 .NET 内容管理系统(CMS)
  • 基于微信小程序的宠物寄领养系统(源码+论文+部署+安装)
  • 深度学习毕设项目推荐-基于python-CNN深度学习训练识别手势方向
  • C# 的小惊喜:ValueTuple,让多返回值更优雅,性能更强
  • 聚焦不锈钢管板:国内生产技术成熟的厂家一览,变压器法兰/压力容器法兰/双相钢法兰/不锈钢法兰,不锈钢管板公司哪个好 - 品牌推荐师
  • 2026年市场评价好的锻件源头厂家哪家权威,法兰/双相钢法兰/非标法兰/船用法兰/变压器法兰,锻件供应商找哪家 - 品牌推荐师
  • .NET + Vue 3 全栈开发:基于 YOLO 的AI图像识别平台实践
  • 2026年行业内可靠的不锈钢法兰厂商排行,非标法兰/不锈钢法兰/不锈钢管板/锻件/法兰,不锈钢法兰品牌怎么选择 - 品牌推荐师
  • 基于Spark的传感器数据实时分析与预测
  • MySQL的一张表使用InnoDB引擎,创建时如果忘记给这张表添加主键,请问这边表有没有聚簇索引?如果有的话聚簇索引是什么样的?
  • 2026年RTO市场新动态:口碑供货商评测指南,旋风除尘器/沸石转轮/沸石转轮+CO/RTO,RTO制造商推荐榜 - 品牌推荐师
  • 卡尔曼滤波中的变量解释(未完待续)
  • 2026年国内诚信的沸石转轮+CO公司联系电话,沸石转轮/活性炭箱/沸石转轮+CO/除尘器,沸石转轮批发厂家排行榜单 - 品牌推荐师
  • Java服务入门级耗时/cpu/内存/io分析
  • SMS-Activate停止运行
  • 详细介绍:技术演进中的开发沉思-219 Ajax:样式与事件
  • 【计算机毕业设计案例】基于python-CNN卷积神经网络深度学习训练识别不同颜色的裤子识别
  • 提示工程架构师:为社会变革注入新动能
  • 【课程设计/毕业设计】基于深度学习python-CNN-pytorch训练识别苹果树叶病害识别