当前位置:网站首页>RSA public private key encryption tool class
RSA public private key encryption tool class
2022-07-21 20:10:00 【Haibi sky blue 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 {
/**
* Public key encryption
*
* @param content Content
* @param publicKey Public key
* @return Encrypted ciphertext
* @throws Exception Abnormal information
*/
public static String encrypt(String content, String publicKey) throws Exception {
//base64 Encoded public key
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA encryption
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
}
/**
* Private key decryption
*
* @param content Content
* @param privateKey Private key
* @return Decrypted string
* @throws Exception abnormal
*/
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 Decrypt
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
/**
* Randomly generate key pairs
*/
public static KeyPair genKeyPair() throws Exception {
// KeyPairGenerator Class is used to generate public and private key pairs , be based on RSA Algorithms generate objects
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(); // Get the private key
RSAPublicKey publicKeyRSA = (RSAPublicKey) keyPair.getPublic(); // Get the public key
String publicKey = new String(Base64.encodeBase64(publicKeyRSA.getEncoded()));
// Get the private key string
String privateKey = new String(Base64.encodeBase64((privateKeyRSA.getEncoded())));
String encrypt = encrypt("ldskjfsdlkjllkjlkj", publicKey);
System.out.println(" Encrypted ciphertext :" + encrypt);
String decrypt = decrypt(encrypt, privateKey);
System.out.println(" Decrypt the ciphertext :" + decrypt);
}
}
边栏推荐
- STL list output and addition
- From graphic design to software testing, I like to raise 11k+13 salary. Looking back, I'm very lucky
- Wechat applet cloud database development
- Wechat public number Development Access, Application for Test number for Local Development Using Wechat public Platform
- [C exercise] macro realizes the exchange of digital binary parity bits
- 10 lines of code to realize wechat applet payment function, and use applet cloud development to realize applet payment function (including source code
- C -- compilation preprocessing
- 通讯录实现
- STL list merge
- 867. Decomposition prime factor
猜你喜欢
Jenkins插件开发——提供对外访问接口
CentOS 8中Docker安装MySQL8
数据类型Map判空 、空字符串、空key值等各种判断方法,全网最详细
Development tools supporting data + code generation, yyds
How to choose sentinel vs. hystrix current limiting?
图表即代码:以代码化的方式构建新一代图形库 —— Feakin
拉取项目pom文件报错,jai_core-1.1.3.jar,解决jar引入问题
STM32 - general timer control ultrasonic sensor hcsr04
如何确定你访问的网站的真实性——证书体系
FL Studio 20.9 Chinese patch package for fruit composition software
随机推荐
【To .NET】. Net core web API development process knowledge points sorting [advanced]
Swagger的详细讲解及使用
STL list output and addition
对项目优化之一:redis缓存数据库的安装与项目中使用,加强项目读取操作
项目debug|no String-argument constructor/factory method to deserialize from String val
C -- compilation preprocessing
2022DASCTF MAY web
《构建之法》笔记---第六章 敏捷流程
逐步理解深度信念网络
629. K inverse pairs array
数据类型Map判空 、空字符串、空key值等各种判断方法,全网最详细
重写hashCode() 对比类是否相同
图表即代码:以代码化的方式构建新一代图形库 —— Feakin
Jd.com's popular architect growth manual is launched, and you deserve the architect aura
Iwemeta: Shi Yuzhu's golden sauce wine: OEM factories have been shut down. Who is producing it?
@Scheduled 定时任务详解
Unity ECS test demo
论文阅读 (61):Multi-Instance Attention Network for Few-Shot Learning
高德地图API获取当前位置对应的周边信息
Chart is code: build a new generation of graphics library in a coded way -- feekin