RSA PKCS1私钥解密(JAVA实现)

在开始编码前,你需要根据你的JAVA版本下载2个JAR文件
Bouncycastle的主JAR包
bcprov-jdk15on-164.jar
和OPENSSL相关JAR包
bcpkix-jdk15on-164.jar
官网
https://www.bouncycastle.org/java.html

使用RSA PKCS1格式私钥解密字符串后进行Base64解密

private String decryptRSA(String str, String privateKey) throws Exception {
   
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    Cipher rsa = C
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,以下是一个使用RSA加密PKCS1JAVA代码示例,其中使用了Bouncy Castle库: ```java import org.bouncycastle.crypto.AsymmetricBlockCipher; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.engines.RSAEngine; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.RSAKeyParameters; import org.bouncycastle.crypto.util.PublicKeyFactory; import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.crypto.util.PKCS1Encoding; import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; public class RSAPKCS1Example { public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); // 生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(2048, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥和私钥 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 加密数据 byte[] data = "Hello, World!".getBytes("UTF-8"); byte[] encryptedData = encrypt(data, publicKey); // 解密数据 byte[] decryptedData = decrypt(encryptedData, privateKey); String decryptedText = new String(decryptedData, "UTF-8"); // 输出结果 System.out.println("原始数据:" + new String(data, "UTF-8")); System.out.println("加密后数据:" + bytesToHex(encryptedData)); System.out.println("解密后数据:" + decryptedText); } public static byte[] encrypt(byte[] data, RSAPublicKey publicKey) throws Exception { AsymmetricKeyParameter keyParameters = PublicKeyFactory.createKey(publicKey.getEncoded()); AsymmetricBlockCipher cipher = new PKCS1Encoding(new RSAEngine()); cipher.init(true, keyParameters); return cipher.processBlock(data, 0, data.length); } public static byte[] decrypt(byte[] encryptedData, RSAPrivateKey privateKey) throws Exception { AsymmetricKeyParameter keyParameters = PrivateKeyFactory.createKey(privateKey.getEncoded()); AsymmetricBlockCipher cipher = new PKCS1Encoding(new RSAEngine()); cipher.init(false, keyParameters); return cipher.processBlock(encryptedData, 0, encryptedData.length); } public static String bytesToHex(byte[] data) { StringBuilder builder = new StringBuilder(); for (byte b : data) { builder.append(String.format("%02X", b)); } return builder.toString(); } } ``` 在上面的示例中,我们使用了Bouncy Castle库来进行RSA加密和解密,并使用了PKCS1编码方式。首先,我们使用`KeyPairGenerator`生成一个2048位的RSA密钥对,并从中获取公钥和私钥。然后,我们将原始数据进行加密,并将加密后的数据存储在`encryptedData`数组中。最后,我们使用私钥对加密后的数据进行解密,并将解密后的数据存储在`decryptedData`数组中。注意,我们使用了一个辅助函数`bytesToHex`将字节数组转换为十六进制字符串,以便于输出结果。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值