背景:
Bouncy Castle Crypto是一个Java实现的加密包,同时也有C#版本。它包含一套轻量级的API可以在包括J2ME在内的绝大多数环境中运行。部分手机不支持JSR177也可使用该加密包实现加密功能。Bouncy Castle加密包包含了大多数流行的加密实现(如:AES、DES、Blowfish等)、散列算法(如:MD5、SHA等)、字节字符编码(如:Base64)等。
Bouncy Castle API通过加密引擎(位于org.bouncycastle.crypto.引擎中)来提供密码实现,这些引擎实现了特定的接口,比如 AsymmetricBlockCipher、BlockCipher或StreamCipher(所有这些接口都可以在 org.bouncycastle.crypto包中找到)。这些接口都具有一个共同的用途:它们让你初始化密码,以字节数组的形式提供要被加密或解密的 数据(既可以以块的形式也可以作为消息流的一部分提供),然后执行加密或解密操作。与使用JCA和SATSA-CRYPTO API一样,当初始化一个密码实现时,你指定想要该实现执行加密还是解密,以及操作中要使用的密钥细节。
除了支持许多不同密码算法,Bouncy Castle API还为其支持的密码提供了密钥生成算法。这是该API的一个重要特性,因为在编写应用程序时,安全的密钥生成和分发至关重要。
注意 :尽管安全密钥生成是Bouncy Castle API提供的一个重要功能,但仅适用该功能还远远不够。应用程序使用其密钥的方式与创建密钥的方式同样重要;弱存储或交换(比如通过不安全的网络通道共享对称密码算法的密钥)抵消了出色的密钥生成算法的强大作用。
前提:
你需要使用到bcprov-ext-jdk15on-161.jar的 jar 包。
可参考:https://blog.csdn.net/hqy1719239337/article/details/88690163
实现如下:
package testRsa;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.generators.*;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.asn1.pkcs.*;
import org.bouncycastle.asn1.*;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;
public class test_rsa {
public static void main(String[] args) throws Exception {
//生成密钥对
RSAKeyPairGenerator rsaKeyPairGenerator = new RSAKeyPairGenerator();
RSAKeyGenerationParameters rsaKeyGenerationParameters = new RSAKeyGenerationParameters(BigInteger.valueOf(3),
new SecureRandom(), 1024, 25);
//初始化参数
rsaKeyPairGenerator.init(rsaKeyGenerationParameters);
AsymmetricCipherKeyPair keyPair = rsaKeyPairGenerator.generateKeyPair();
//公钥
AsymmetricKeyParameter publicKey = keyPair.getPublic();
//私钥
AsymmetricKeyParameter privateKey = keyPair.getPrivate();
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(publicKey);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKey);
//变字符串
ASN1Object asn1ObjectPublic = subjectPublicKeyInfo.toASN1Primitive();
byte[] publicInfoByte = asn1ObjectPublic.getEncoded();
ASN1Object asn1ObjectPrivate = privateKeyInfo.toASN1Primitive();
byte[] privateInfoByte = asn1ObjectPrivate.getEncoded();
//这里可以将密钥对保存到本地
final Base64.Encoder encoder64 = Base64.getEncoder();
System.out.println("PublicKey:\n" + encoder64.encodeToString(publicInfoByte));
System.out.println("PrivateKey:\n" + encoder64.encodeToString(privateInfoByte));
//加密、解密
ASN1Object pubKeyObj = subjectPublicKeyInfo.toASN1Primitive();//这里也可以从流中读取,从本地导入
AsymmetricKeyParameter pubKey = PublicKeyFactory.createKey(SubjectPublicKeyInfo.getInstance(pubKeyObj));
AsymmetricBlockCipher cipher = new RSAEngine();
cipher.init(true, pubKey);//true表示加密
final Base64.Decoder decoder64 = Base64.getDecoder();
//加密
String data = "成aa功324$$了#*(=-nh)。。。";
System.out.println("\n明文:" + data);
byte[] encryptData = cipher.processBlock(data.getBytes("utf-8")
, 0, data.getBytes("utf-8").length);
System.out.println("密文:" + encoder64.encodeToString(encryptData));
//解密
AsymmetricKeyParameter priKey = PrivateKeyFactory.createKey(privateInfoByte);
cipher.init(false, priKey);//false表示解密
byte[] decriyptData=cipher.processBlock(encryptData, 0, encryptData.length);
String decryptData = new String(decriyptData,"utf-8");
System.out.println("解密后数据:" + decryptData);
}
}
结果:
参考:https://blog.csdn.net/a571574085/article/details/81335095