当前位置:网站首页>Encryption, decryption and signature verification
Encryption, decryption and signature verification
2022-06-10 00:24:00 【Feifei can't fly】
Encryption, decryption and signature verification
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0</version>
</dependency>
public class EncodeSign {
public static void main(String[] args) {
// Generate key pair
KeyPair rsa = SecureUtil.generateKeyPair("RSA");
PrivateKey privateKey = rsa.getPrivate();
PublicKey publicKey = rsa.getPublic();
// Data transmitted
String data = "{\"result\": \"SUCCESS\"}";
// Set the signature algorithm and the private key of the signature
Sign signOne = SecureUtil.sign(SignAlgorithm.MD5withRSA);
signOne.setPrivateKey(privateKey);
// Signature
byte[] signData = signOne.sign(data.getBytes(StandardCharsets.UTF_8));
// Will sign the result Base64 Encrypted into string transmission
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);
// attestation
Sign signTwo = SecureUtil.sign(SignAlgorithm.MD5withRSA);
// Set the signature algorithm and the public key of the signature
signTwo.setPublicKey(publicKey);
// Restore signature as byte Array
byte[] signDataOrigin = Base64.decode(signDataStr);
boolean verify = signTwo.verify(data.getBytes(StandardCharsets.UTF_8), signDataOrigin);
System.out.println(verify);
// Decrypt
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);
}
}
边栏推荐
- 你了解单例模式吗?反正我不了解。
- 哨兵3(Sentinel-3)数据简介
- Leetcode296 weekly games
- Introduction to C # WPF layout control layoutcontrol
- MySQL scheduled task (event scheduler)
- MySQL开发实战总结(一)
- Retrofit2.0 method summary of adding header
- leetcode296场周赛
- 期货开户网上安全性可以保证吗?哪家期货公司好?
- Detailed explanation of IEEE 754 floating point number standard
猜你喜欢

Multiplier: how to build a circuit like Lego (Part 2)?

慢查询如何优化?(实战慢查询)

opencv体系结构

Operator (day 2)

Manual single precision floating point type

Py6s configuration tutorial (win10 × 64)

JVM explanation

It only takes eight steps to package the applet to generate an app

js 逻辑空分配双问号语法 、双竖杠语法 与 可选链语法

IOS cache - nscache and sandbox cache
随机推荐
478. 在圆内随机生成点
哨兵3(Sentinel-3)数据简介
(transfer) what is the difference between objectid, FID and oid fields in ArcGIS?
How can C get entity class attribute names and values?
With the advent of the digital era, 360 has joined hands with the dark horse of entrepreneurship to help small and medium-sized enterprises seize the key future
Mind map - 3. SQL injection vulnerability
732. my schedule III
[docker]mysql scheduled backup
Introduction to csrf/xsrf
ADB shell WM command using
数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
Gartner 2021年全球IaaS报告:AWS蛋糕被蚕食,中国云厂商稳步进击
Can the merger of G7 and E6 of head Internet of things SaaS become a "meituan" in the field of to B?
网络状态的起起伏伏
“當你不再是程序員,很多事會脫離掌控”—— 對話全球最大獨立開源公司SUSE CTO
银行有没有必要建立数据中台?看完你就明白了
Introduction to bias and variance
Basic and introductory knowledge for PHP learning
929. 独特的电子邮件地址
低边驱动和高边驱动