当前位置:网站首页>RSA公私钥加密工具类
RSA公私钥加密工具类
2022-07-21 05:02:00 【海比天蓝2022】
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAEncryptUtil {
/**
* 公钥加密
*
* @param content 内容
* @param publicKey 公钥
* @return 加密后的密文
* @throws Exception 异常信息
*/
public static String encrypt(String content, String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
}
/**
* 私钥解密
*
* @param content 内容
* @param privateKey 私钥
* @return 解密后的字符串
* @throws Exception 异常
*/
public static String decrypt(String content, String privateKey) throws Exception {
byte[] inputByte = Base64.decodeBase64(content.getBytes(StandardCharsets.UTF_8));
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
/**
* 随机生成密钥对
*/
public static KeyPair genKeyPair() throws Exception {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(512, new SecureRandom());
return keyPairGen.generateKeyPair();
}
public static void main(String[] args) throws Exception {
KeyPair keyPair = genKeyPair();
RSAPrivateKey privateKeyRSA = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKeyRSA = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
String publicKey = new String(Base64.encodeBase64(publicKeyRSA.getEncoded()));
// 得到私钥字符串
String privateKey = new String(Base64.encodeBase64((privateKeyRSA.getEncoded())));
String encrypt = encrypt("ldskjfsdlkjllkjlkj", publicKey);
System.out.println("加密密文:" + encrypt);
String decrypt = decrypt(encrypt, privateKey);
System.out.println("解密密文:" + decrypt);
}
}
边栏推荐
- 论文阅读 (61):Multi-Instance Attention Network for Few-Shot Learning
- Swagger的详细讲解及使用
- FL Studio 20.9水果编曲软件中文汉化补丁包
- 微信公众号开发接入,利用微信公众平台申请测试号进行本地开发
- 868. 筛质数
- How do I optimize from 20s to 500ms
- Read yaml configuration file based on Hydra Library (support command line parameters)
- 解决Oracle数据库查询单表排序顺序错误问题之一
- Flutter obtains longitude and latitude through geolocator positioning plug-in and calls Gaode peripheral information interface
- MySQL之DML(数据操纵语言)
猜你喜欢
One more blank page when printing the Jim building block report - problem solving
2022DASCTF MAY web
Getting started with the gradle project construction tool
微信公眾號開發接入,利用微信公眾平臺申請測試號進行本地開發
@Scheduled 定时任务详解
Introduction, installation and basic use of MYCAT
(3) Jdbctemplate
STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
STM32 - positioning module atgm336h, data analysis, longitude and latitude extraction
Ibm3650m4 physical machine installation esxi7.0
随机推荐
How to choose sentinel vs. hystrix current limiting?
Okaleido tiger NFT is about to log in to binance NFT platform, and the future market continues to be optimistic
C——编译预处理
[untitled]
uniapp开发app解决uni.pageScrollTo不生效
数据库设计 数据库系统概论(第五版)
Gradle项目构建工具入门
Quanzhi a40i development board hardware specification - 100% domestic + industrial level scheme (Part 1)
iwemeta:史玉柱的黄金酱酒:贴牌的代工厂都被关停了,谁在生产?
高德地图API获取当前位置对应的周边信息
MySQL DML (data manipulation language)
通讯录实现
STM32 - ADC reads the photosensitive sensor, controls the LED light, and the watchdog interrupts
869. Trial division for divisor
The JSON tool converts objects to JSON format strings
Wechat official account development access, using wechat public platform to apply for test number for local development
How to determine the authenticity of the website you visit -- certificate system
判断“String[]”数组中是否有存在重复的值,利用hashSet特性排查
蓝牙智能营养电子秤解决方案
Development tools supporting data + code generation, yyds