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

【非对称加密算法】RSA算法

一、非对称加密算法 

非对称加密算法使用了两个不同的密钥:公钥和私钥。公钥是公开的,可以被任何人使用,而私钥是只有特定的人能够使用的。这种算法的加密和解密过程使用不同的密钥,因此称为非对称加密算法。

在非对称加密算法中,使用公钥进行加密,私钥进行解密。因此,它的主要优点是可以实现安全的通信,因为即使公钥被攻击者获得,攻击者也无法破解消息,因为只有使用私钥才能解密。

非对称加密算法常用于网络安全、电子邮件通信、电子支付和数字签名等领域。其中最常见的非对称加密算法是RSA算法。

二、RSA算法 

该算法的基本思想是将要加密的数据转化为一个数字,然后通过公钥进行加密。只有私钥才能解密这个加密后的数字,将其转化为原始的数据。加密和解密采用的是不同的密钥,公钥可以由任何人获得,而私钥只能由算法的使用者获得。

RSA算法的应用场景包括:身份验证、加密通信、数字签名、SSL/TLS证书、VPN等。

 (1)具体使用

public class Demo04 {public static void main(String[] args) throws Exception {// 明文:byte[] plain = "Hello, encrypt use RSA".getBytes("UTF-8");// 创建公钥/私钥对Human hong = new Human("小红");Human ming = new Human("小明");// 小明使用小红的公钥进行加密// 1.获取小红的公钥PublicKey hongPublicKey = hong.getPublicKey();System.out.println(String.format("小红的public key(公钥): %x", new BigInteger(1, hongPublicKey.getEncoded())));// 2.使用公钥加密byte[] encrypted = ming.encrypt(plain, hongPublicKey);System.out.println(String.format("encrypted(加密): %x", new BigInteger(1, encrypted)));// 小红使用自己的私钥解密:// 1.获取小红的私钥,并输出PrivateKey hongPrivateKey = hong.getPrivateKey();System.out.println(String.format("小红的private key(私钥): %x", new BigInteger(1, hongPrivateKey.getEncoded())));// 2.使用私钥解密byte[] decrypted = hong.decrypt(encrypted);System.out.println("decrypted(解密): " + new String(decrypted, "UTF-8"));}
}//用户类
class Human {// 姓名String name;// 私钥:PrivateKey privatekey;// 公钥:PublicKey publickey;// 构造方法public Human(String name) throws GeneralSecurityException {// 初始化姓名this.name = name;// 生成公钥/私钥对:KeyPairGenerator kpGen=KeyPairGenerator.getInstance("RSA");kpGen.initialize(1024);KeyPair kp=kpGen.generateKeyPair();this.privatekey=kp.getPrivate();this.publickey=kp.getPublic();}// 把私钥导出为字节public PrivateKey getPrivateKey() {return this.privatekey;}// 把公钥导出为字节public PublicKey getPublicKey() {return this.publickey;}// 用公钥加密public byte[] encrypt(byte[] message,PublicKey publickey) throws GeneralSecurityException {// 使用公钥进行初始化Cipher cipher=Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publickey);	//使用公钥进行初始化return cipher.doFinal(message);}// 用私钥解密:public byte[] decrypt(byte[] input) throws GeneralSecurityException {// 使用私钥进行初始化Cipher cipher=Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE,this.privatekey);	 //使用私钥进行初始化return cipher.doFinal(input);}
}

 只用使用同一个公钥-私钥对才能正常加解密!!!

 

http://www.lryc.cn/news/168443.html

相关文章:

  • 【滑动窗口】438. 找到字符串中所有字母异位词
  • 【PowerQuery】Excel 一分钟以内刷新PowerQuery数据
  • 【C语言】用冒泡排序实现my_qsort
  • 【css】深入理解flex属性
  • 前端项目开发流程
  • MybatisPlus逆向工程入门指南:让你的开发更高效、更简洁、更优雅
  • 通用商城项目(下)
  • k8s集群使用ingress转发grafana服务
  • MongoDB的备份和恢复
  • Pytorch学习笔记(GPU训练)
  • 一款开源的shell脚本分析工具
  • HTML <video> 标签
  • mac 本地运行 http-proxy-middleware ,请求超时
  • 【Effective Python】读书笔记-05类与接口
  • 【办公自动化】用Python在Excel中查找并替换数据(文末送书)
  • python学习随笔3
  • 《TCP/IP网络编程》阅读笔记--epoll的使用
  • Python 递归函数
  • Java实现计算两个日期之间的工作日天数
  • CS5817规格书|CS5817芯片参数|多功能便携式显示器方案芯片规格
  • 2023面试知识点一
  • 【算法题】2856. 删除数对后的最小数组长度
  • Java面向对象编程
  • K8S:Yaml文件详解及编写示例
  • 去耦电路设计应用指南(一)MCU去耦设计介绍
  • 【c++】杂记
  • 简记:使用 Django Shell 清空 数据库表
  • Web项目测试
  • Springboot 集成 Ehcache 提示 Cannot find cache named ‘employee_all‘ for Builder
  • pandas 笔记:shift