背景:
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 cn.com.infosec.test;
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 Bb {
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 = "成功加密324$$#@了。。。";
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