RSA 加密签名验签解密
import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import java.security.*;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSACipher {
/**
* 加密方法
*
* @param publicKey 公钥
* @param raw 待加密明文
* @return 加密后的密文
* @throws Exception
*/
public static byte[] encrypt(String publicKey, byte[] raw) throws Exception {
Key key = getPublicKey(publicKey);
Cipher cipher = Cipher.getInstance(Config.RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
byte[] b1 = cipher.doFinal(raw);
return Base64.encodeBase64(b1);
}
/**
* 解密方法
*
* @param privateKey 私钥
* @param enc 待解密密文
* @return 解密后的明文
* @throws Exception
*/
public static byte[] decrypt(String privateKey, byte[] enc) throws Exception {
Key key = getPrivateKey(privateKey);
Cipher cipher = Cipher.getInstance(Config.RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
return cipher.doFinal(Base64.decodeBase64(enc));
}
/**
* 获取公钥
*
* @param key 密钥字符串(经过base64编码)
* @return 公钥
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(key.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 获取私钥
*
* @param key 密钥字符串(经过base64编码)
* @return 私钥
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key.getBytes()));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 签名
*
* @param privateKey 私钥
* @param content 要进行签名的内容
* @return 签名
*/
public static String sign(String privateKey, byte[] content) {
try {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey.getBytes()));
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
Signature signature = Signature.getInstance("SHA256WithRSA");
signature.initSign(priKey);
signature.update(content);
byte[] signed = signature.sign();
return new String(Base64.encodeBase64(signed));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 验签
*
* @param publicKey 公钥
* @param content 要验签的内容
* @param sign 签名
* @return 验签结果
*/
public static boolean checkSign(String publicKey, byte[] content, String sign) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decode2(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
java.security.Signature signature = java.security.Signature.getInstance("SHA256WithRSA");
signature.initVerify(pubKey);
signature.update(content);
return signature.verify(Base64.decode2(sign));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws Exception {
//客户端代码
String text = "hello";
//使用服务端公钥加密
byte[] encryptText = RSACipher.encrypt(Config.SERVER_PUBLIC_KEY, text.getBytes());
System.out.println("加密后:\n" + new String(encryptText));
//使用客户端私钥签名
String signature = RSACipher.sign(Config.CLIENT_PRIVATE_KEY, encryptText);
System.out.println("签名:\n" + signature);
//服务端代码
//使用客户端公钥验签
boolean result = RSACipher.checkSign(Config.CLIENT_PUBLIC_KEY, encryptText, signature);
System.out.println("验签:\n" + result);
//使用服务端私钥解密
byte[] decryptText = RSACipher.decrypt(Config.SERVER_PRIVATE_KEY, encryptText);
System.out.println("解密后:\n" + new String(decryptText));
}
}
RSA 加密签名验签解密的更多相关文章
- Java RSA加密以及验签
签名加密以及验签工具类: 一般秘钥分为3个key 1.自己生成的私钥, 2.通过私钥生成的公钥1 3.通过提交公钥1给某宝,获取的公钥2. RSA公钥加密算法简介 非对称加密算法.只有短的RSA钥匙才 ...
- RSA密钥生成、加密解密、签名验签
RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...
- js rsa sign使用笔记(加密,解密,签名,验签)
你将会收获: js如何加密, 解密 js如何签名, 验签 js和Java交互如何相互解密, 验签(重点) 通过谷歌, 发现jsrsasign库使用者较多. 查看api发现这个库功能很健全. 本文使用方 ...
- [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome
Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...
- C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServic
C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider DESCryptoS ...
- RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密
原文:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密 C#在using System.Security.Cryptograph ...
- 利用SHA-1算法和RSA秘钥进行签名验签(带注释)
背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...
- 数据安全管理:RSA加密算法,签名验签流程详解
本文源码:GitHub·点这里 || GitEE·点这里 一.RSA算法简介 1.加密解密 RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用.可以在不直接传递密钥的情况下,完成加 ...
- RSA签名验签
import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...
- .net 实现签名验签
本人被要求实现.net的签名验签,还是个.net菜鸡,来分享下采坑过程 依然,签名验签使用的证书格式依然是pem,有关使用openssl将.p12和der转pem的命令请转到php实现签名验签 .ne ...
随机推荐
- [Linux]Windows远程CENTOS7桌面
1 背景/问题描述 客户要在CENTOS7上运行我司的基于Java的一款图形化桌面软件,然后在Windows上远程该机器的桌面软件进行操作使用.但问题是,客户的CENTOS7服务器没有图形化桌面环境, ...
- day31:socketserver&hashlib&hmac&TCP登录
目录 1.socketserver:实现TCP协议下Server端的并发 2.hashlib模块 3.hashlib应用:文件校验 4.hmac应用:服务器的合法性校验 5.TCP登录程序 1.soc ...
- Redis(七)缓存穿透、缓存击穿、缓存雪崩以及分布式锁
应用问题解决 1 缓存穿透 1.1 访问结构 正常情况下,服务器接收到浏览器发来的web服务请求,会先去访问redis缓存,如果缓存中存在数据则直接返回,否则会去查询数据库里面的数据,然后保存在red ...
- Python pip速度慢,更换源
版权声明:本文为CSDN博主「cocoprince」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/Coco ...
- odoo 开发入门教程系列-QWeb简史
QWeb简史 到目前为止,我们的房地产模块的界面设计相当有限.构建列表视图很简单,因为只需要字段列表.表单视图也是如此:尽管使用了一些标记,如<group>或<page>,但在 ...
- InnoSetup打包 添加.NET环境安装
这是封装出来的针对.NET环境安装的精简流程 根据流程新建一个配置文件 教程都是很简单的,可以参考<InnoSetup 客户端程序打包教程> 添加.NET安装基本的函数及辅助方法 在[Se ...
- flink HelloWorld 之词频统计
最近也在跟着学习flink,也是费了一点功夫才把开发环境都搭建了起来,做了一个简单的词频统计的demo- 准备工作 首先我们需要搭建需要的flink开发环境,我这里使用的是IDEA作为我的开发工具,所 ...
- 2021-03-21:给定一棵二叉树的头节点head,求以head为头的树中,最小深度是多少?
2021-03-21:给定一棵二叉树的头节点head,求以head为头的树中,最小深度是多少? 福大大 答案2021-03-21: 1.递归. 2.莫里斯遍历. 代码用golang编写,代码如下: p ...
- 2021-10-10:杨辉三角 II。给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。力扣119。
2021-10-10:杨辉三角 II.给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行.在「杨辉三角」中,每个数是它左上方和右上方的数的和.力扣119. 福大大 答案20 ...
- lec-5-Policy Gradients
直接策略微分 Goal: idea:求最大值:直接求导 tip:利用log导数等式进行变换 具体推导: 理解策略梯度 假定开始policy服从高斯分布,采样得到回报,计算梯度,根据reward增加动作 ...