当前位置:网站首页>加解密及验签
加解密及验签
2022-06-09 23:48:00 【飞飞不 会飞】
加解密及验签
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0</version>
</dependency>
public class EncodeSign {
public static void main(String[] args) {
// 生成密钥对
KeyPair rsa = SecureUtil.generateKeyPair("RSA");
PrivateKey privateKey = rsa.getPrivate();
PublicKey publicKey = rsa.getPublic();
// 传输的数据
String data = "{\"result\": \"SUCCESS\"}";
// 设置签名算法和签名的私钥
Sign signOne = SecureUtil.sign(SignAlgorithm.MD5withRSA);
signOne.setPrivateKey(privateKey);
// 签名
byte[] signData = signOne.sign(data.getBytes(StandardCharsets.UTF_8));
// 将签名结果Base64加密成字符串传输
String signDataStr = Base64.encode(signData);
System.out.println("signDataStr = " + signDataStr);
RSA rsaForDataEncode = new RSA();
rsaForDataEncode.setPrivateKey(privateKey);
byte[] encrypt = rsaForDataEncode.encrypt(data.getBytes(StandardCharsets.UTF_8), KeyType.PrivateKey);
String encodeData = Base64.encode(encrypt);
System.out.println(encodeData);
// 验签
Sign signTwo = SecureUtil.sign(SignAlgorithm.MD5withRSA);
// 设置签名算法和签名的公钥
signTwo.setPublicKey(publicKey);
// 还原签名为byte数组
byte[] signDataOrigin = Base64.decode(signDataStr);
boolean verify = signTwo.verify(data.getBytes(StandardCharsets.UTF_8), signDataOrigin);
System.out.println(verify);
// 解密
RSA rsaForDataDecode = new RSA();
rsaForDataDecode.setPublicKey(publicKey);
byte[] decodeData = Base64.decode(encodeData);
byte[] decrypt = rsaForDataDecode.decrypt(decodeData, KeyType.PublicKey);
String dataOrgin = new String(decrypt, StandardCharsets.UTF_8);
System.out.println("dataOrgin = " + dataOrgin);
}
}
边栏推荐
- Can I take the PMP Exam without project management experience?
- The number of encrypted assets covered by Bloomberg terminal products expanded to the top 50 assets
- Recommendation letter | look at the sky step by step, hand in hand, and see the stars one by two
- Pta7-5 Sina Weibo hot topics
- Retrofit2.0 添加Header的方法总结
- bias、variance介绍
- Enterprise worry free | apipost privatization deployment activity is about to start
- (transfer) what is the difference between objectid, FID and oid fields in ArcGIS?
- pycharm 2022永久激活版下载,亲测有效
- 片上变化(on chip variation,OCV)概念学习
猜你喜欢

MySQL执行计划

Flutter ITMS-90338: Non-public API usage - Frameworks/webview_flutter_wkwebview.framework

Introduction to bias and variance

The problem of connecting the computer to the printer (the printer display is not specified) solution

电脑连接打印机出现的问题(打印机显示未指定)解决方法

我服了,MySQL表500W行,居然有人不做分区?

界面控件DevExpress WinForms——全新的WXI皮肤抢“鲜”看

pycharm 2022永久激活版下载,亲测有效

银行有没有必要建立数据中台?看完你就明白了

Gartner 2021年全球IaaS报告:AWS蛋糕被蚕食,中国云厂商稳步进击
随机推荐
Py6S配置教程(win10 ×64)
« Lorsque vous n'êtes plus programmeur, beaucoup de choses échappent au contrôle » - conversation avec Suse CTO, la plus grande entreprise open source indépendante au monde
Gartner 2021年全球IaaS报告:AWS蛋糕被蚕食,中国云厂商稳步进击
10个常见触发IO瓶颈的高频业务场景
ArcMap resolving geometric errors
Application of technical scheme of PTP time server (NTP network time server)
Idea uninstall tutorial
请教一个问题,pg有类似mysql server_id一样的实例唯一标识么?
C # WPF realizes dynamic increase and decrease of tab page
Retrofit2.0 method summary of adding header
Introduction to bias and variance
Pop up frequently: an error occurred when vscode tried to create a file in the target directory. Try again. Skip this file and close setup
pycharm 2022永久激活版下载,亲测有效
辐射亮度与表观反射率的转换
Enterprise worry free | apipost privatization deployment activity is about to start
ADB shell WM command using
二叉树展开为链表[树处理的精髓--如何处理每一颗子树]
Detailed explanation of IEEE 754 floating point number standard
2022最新版阿里开发手册发布!!!
(转)ArcGIS中ObjectID,FID和OID字段有什么区别?