本文源码:GitHub·点这里 || GitEE·点这里

一、RSA算法简介

1、加密解密

RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用。可以在不直接传递密钥的情况下,完成加解密操作。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。

2、签名验签

数字签名就是信息的来源添加一段无法被伪造的加密字符串,这段数字串作为对信息的来源真实性的一个有效证明。这个过程称为签名和验签。

二、场景描述

  • 消息发送方:甲方,持有公钥
  • 消息接收方:乙方,持有私钥

1、加密解密过程

(1)、乙方生成一对密钥即公钥和私钥,私钥不公开,乙方自己持有,公钥为公开,甲方持有。

(2)、乙方收到甲方加密的消息,使用私钥对消息进行解密,获取明文。

2、签名验签过程

(1)、乙方收到消息后,需要回复甲方,用私钥对回复消息签名,并将消息明文和消息签名回复甲方。

(2)、甲方收到消息后,使用公钥进行验签,如果验签结果是正确的,则证明消息是乙方回复的。

三、源代码实现

1、密钥字符串获取

  • 代码生成
private static HashMap<String, String> getTheKeys() {
HashMap<String, String> keyPairMap = new HashMap<String, String>();
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 密钥大小:1024 位
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
String publicKey = printBase64Binary(keyPair.getPublic().getEncoded());
String privateKey = printBase64Binary(keyPair.getPrivate().getEncoded());
keyPairMap.put("publicKey", publicKey);
keyPairMap.put("privateKey", privateKey);
return keyPairMap ;
}
  • 读取文件

文件位置

public static final String PUB_KEY = "rsaKey/public.key" ;
public static final String PRI_KEY = "rsaKey/private.key" ;

文件加载

public static String getKey (String keyPlace) throws Exception {
BufferedReader br= null;
try {
br= new BufferedReader(new InputStreamReader(RsaCryptUtil.class.getClassLoader().
getResourceAsStream(keyPlace)));
String readLine= null;
StringBuilder keyValue = new StringBuilder();
while((readLine= br.readLine())!=null){
if(!(readLine.charAt(0)=='-')){
keyValue.append(readLine);
}
}
return keyValue.toString();
} catch (Exception e) {
throw new Exception("RSA密钥读取错误",e) ;
} finally{
if (br != null) {
try {
br.close();
} catch (Exception e) {
System.out.println("密钥读取流关闭异常");
}
}
}
}

2、公钥和私钥

  • 公钥字符串生成公钥
public static RSAPublicKey createPublicKey(String publicKeyValue) throws Exception {
try {
byte[] buffer = DatatypeConverter.parseBase64Binary(publicKeyValue);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
throw new Exception("公钥创建失败", e);
}
}
  • 私钥字符串生成私钥
public static RSAPrivateKey createPrivateKey(String privateKeyValue) throws Exception {
try {
byte[] buffer = javax.xml.bind.DatatypeConverter.parseBase64Binary(privateKeyValue);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
throw new Exception("私钥创建失败", e);
}
}

3、加密和解密

  • 公钥加密
public static String encrypt(RSAPublicKey publicKey, byte[] clearData) throws Exception {
if (publicKey == null) {
throw new Exception("加密公钥为空, 无法加密");
}
try {
Cipher cipher = Cipher.getInstance("RSA") ;
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(clearData);
return printBase64Binary(output);
} catch (Exception e) {
throw new Exception("公钥加密失败",e);
}
}
  • 私钥解密
public static String decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception {
if (privateKey == null) {
throw new Exception("解密私钥为空, 无法解密");
}
try {
Cipher cipher = Cipher.getInstance("RSA") ;
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] output = cipher.doFinal(cipherData);
return new String(output);
} catch (BadPaddingException e) {
throw new Exception("私钥解密失败",e);
}
}

4、签名和验签

  • 私钥签名
public static String sign (String signData, PrivateKey privateKey) throws Exception {
byte[] keyBytes = privateKey.getEncoded();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(keySpec);
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(key);
signature.update(signData.getBytes());
return printBase64Binary(signature.sign());
}
  • 公钥验签
public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
byte[] keyBytes = publicKey.getEncoded();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance("MD5withRSA");
signature.initVerify(key);
signature.update(srcData.getBytes());
return signature.verify(parseBase64Binary(sign));
}

5、编码和解码

/**
* 字节数组转字符
*/
public static String printBase64Binary(byte[] bytes) {
return DatatypeConverter.printBase64Binary(bytes);
}
/**
* 字符转字节数组
*/
public static byte[] parseBase64Binary(String value) {
return DatatypeConverter.parseBase64Binary(value);
}

6、测试代码块

  • 密钥生成测试
public static void testCreateKey () throws Exception {
HashMap<String, String> map = RsaCryptUtil.getTheKeys();
String privateKeyStr=map.get("privateKey");
String publicKeyStr=map.get("publicKey");
System.out.println("私钥:"+privateKeyStr);
System.out.println("公钥:"+publicKeyStr);
//消息发送方
String originData="cicada-smile";
System.out.println("原文:"+originData);
String encryptData = RsaCryptUtil.encrypt(RsaCryptUtil.createPublicKey(publicKeyStr),
originData.getBytes());
System.out.println("加密:"+encryptData);
//消息接收方
String decryptData=RsaCryptUtil.decrypt(RsaCryptUtil.createPrivateKey(privateKeyStr),
RsaCryptUtil.parseBase64Binary(encryptData));
System.out.println("解密:"+decryptData);
}
  • 密钥读取测试
public static void testReadKey () throws Exception {
String value = getKey("rsaKey/public.key");
System.out.println(value);
String privateKeyStr = getKey(RsaCryptUtil.PRI_KEY) ;
String publicKeyStr = getKey(RsaCryptUtil.PUB_KEY) ;
//消息发送方
String originData="cicada-smile";
System.out.println("原文:"+originData);
String encryptData = RsaCryptUtil.encrypt(RsaCryptUtil.createPublicKey(publicKeyStr),
originData.getBytes());
System.out.println("加密:"+encryptData);
//消息接收方
String decryptData=RsaCryptUtil.decrypt(RsaCryptUtil.createPrivateKey(privateKeyStr),
RsaCryptUtil.parseBase64Binary(encryptData));
System.out.println("解密:"+decryptData);
}
  • 签名验签测试
public static void testSignVerify () throws Exception {
String signData = "cicada-smile" ;
String privateKeyStr = getKey(RsaCryptUtil.PRI_KEY) ;
String publicKeyStr = getKey(RsaCryptUtil.PUB_KEY) ;
String signValue = sign(signData,RsaCryptUtil.createPrivateKey(privateKeyStr)) ;
boolean flag = verify(signData,RsaCryptUtil.createPublicKey(publicKeyStr),signValue);
System.out.println("原文:"+signData);
System.out.println("签名:"+signValue);
System.out.println("验签:"+flag);
}

四、源代码地址

GitHub·地址
https://github.com/cicadasmile
GitEE·地址
https://gitee.com/cicadasmile

数据安全管理:RSA加密算法,签名验签流程详解的更多相关文章

  1. 利用SHA-1算法和RSA秘钥进行签名验签(带注释)

    背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...

  2. RSA密钥生成、加密解密、签名验签

    RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...

  3. js rsa sign使用笔记(加密,解密,签名,验签)

    你将会收获: js如何加密, 解密 js如何签名, 验签 js和Java交互如何相互解密, 验签(重点) 通过谷歌, 发现jsrsasign库使用者较多. 查看api发现这个库功能很健全. 本文使用方 ...

  4. [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome

    Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...

  5. RSA签名验签

    import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...

  6. C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServic

    C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider DESCryptoS ...

  7. RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密

    原文:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密 C#在using System.Security.Cryptograph ...

  8. php 实现签名验签

    本人php菜鸟,主要使用php实现简单的签名验签功能 以下php代码使用的密钥格式为pem格式,其他证书格式可以使用openssl进行转换(未安装请实现安装): 以下是.p12文件导出pem格式公私钥 ...

  9. .net 实现签名验签

    本人被要求实现.net的签名验签,还是个.net菜鸡,来分享下采坑过程 依然,签名验签使用的证书格式依然是pem,有关使用openssl将.p12和der转pem的命令请转到php实现签名验签 .ne ...

随机推荐

  1. Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题【转】

    Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)的序列化问题 http://blog.didispace.com/Spring-Boot-And-Feign- ...

  2. npm切换成淘宝镜像源

    0-前言 我们都知道node下载第三方依赖包是从国外服务器下载,不是速度奇慢就是下载异常,切换成国内的淘宝镜像源很有必要: 1-切换方法 $ npm config set registry https ...

  3. vue中,使用element ui的弹窗与echarts之间的问题

    今天项目中有个需求,就是在页面中点击一个图标,弹出一个抽屉式的弹窗(弹窗是element UI的抽屉),弹窗里边是echarts呈现的数据,当我直接用echarts的时候,报错dom没有获取到: 这就 ...

  4. VMware下载及安装使用方法

    一.VMware的介绍: 虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统.DesktopVirtualBox,虚拟系统通过生成 ...

  5. Tensorflow Serving Docker compose 部署服务细节(Ubuntu)

    [摘要] Tensorflow Serving 是tf模型持久化的重要工具,本篇介绍如何通过Docker compose搭建并调试TensorFlow Serving TensorFlow Servi ...

  6. luogu CF16E Fish

    题目描述 有n条鱼,编号从1到n,住在湖里.每天有一对鱼相遇, 彼此相遇的概率是一样的.如果两条标号为i和j的鱼见面,第一只吃了第二只的概率为a{i,j},第二只会吃了第一只的概率为a{j,i}=1- ...

  7. iOS开发动画(Animation)总结

    UIView的,翻转.旋转,偏移,翻页,缩放,取反的动画效果   翻转的动画 //开始动画 [UIView beginAnimations:@"doflip" context:ni ...

  8. 3D硬件加速提升动画性能 与 z-index属性

    目录 1. chrome Layer borders 2. 层创建标准 3. 例子 总结 1. chrome Layer borders <WebKit技术内幕>第二章介绍了网页的结构,其 ...

  9. python2和python3编码问题

    欢迎加入python学习交流群 667279387 一.什么是编解码 1.什么是unicode 2.编码方式 二.python中的编解码 1.python2 (1).encode() 和 .decod ...

  10. git 设置和取消指定域名代理 - git config proxy

    Firstly - Check Check if U have global .gitconfig file 检查是否有全局 .gitconfig 文件 Usually global .gitconf ...