我在做RSA加密和解密在我的應用程序中。我有兩個文件放置在assets文件夾中,public_key.cer用於加密,private_key.cer用於解密.Getting我從下面的文件中獲取公鑰。獲取異常,同時試圖從證書中獲取私鑰
CertificateFactory certFactory = CertificateFactory.getInstance(X.509, BC);
InputStream is = context.getAssets().open("public_Key.cer");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(is);
publicKey = cert.getPublicKey();
RSA加密做工精細,而我面臨的問題,而試圖從certificate.Below獲得privae關鍵是使用獲得的私鑰
InputStream is = context.getAssets().open("private_key.cer");
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(is));
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(privSpec);
我得到的代碼例外。
com.android.org.bouncycastle.jcajce.provider.asymmetric.util.ExtendedInvalidKeySpecException: unable to process key spec: java.lang.IllegalArgumentException: unknown object in getInstance: com.android.org.bouncycastle.asn1.DERApplicationSpecific
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePrivate(KeyFactorySpi.java:105)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:186)
at com.teknospire.ndasenda_agent.utils.Conversion.decryptUsingPrivateKey(Conversion.java:111)
at com.teknospire.ndasenda_agent.utils.Conversion.getDecryptedSkey(Conversion.java:243)
at com.teknospire.ndasenda_agent.json.JsonCreationAndExtraction.readLoginParams(JsonCreationAndExtraction.java:40)
at com.mockUp.ndasenda.LoginActivity$LoginRequest.doInBackground(LoginActivity.java:283)
at com.mockUp.ndasenda.LoginActivity$LoginRequest.doInBackground(LoginActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:848)
Caused by: java.lang.IllegalArgumentException: unknown object in getInstance: com.android.org.bouncycastle.asn1.DERApplicationSpecific
at com.android.org.bouncycastle.asn1.ASN1Sequence.getInstance(ASN1Sequence.java:50)
at com.android.org.bouncycastle.asn1.ASN1Sequence.getInstance(ASN1Sequence.java:33)
at com.android.org.bouncycastle.asn1.pkcs.PrivateKeyInfo.getInstance(PrivateKeyInfo.java:45)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePrivate(KeyFactorySpi.java:91)
... 12 more
java.lang.NullPointerException
at org.bouncycastle.crypto.params.KeyParameter.<init>(KeyParameter.java:13)
at com.teknospire.ndasenda_agent.utils.Conversion.decryptUsingSessionKey(Conversion.java:145)
at com.teknospire.ndasenda_agent.utils.Conversion.getDecryptionData(Conversion.java:185)
at com.teknospire.ndasenda_agent.json.JsonCreationAndExtraction.readLoginParams(JsonCreationAndExtraction.java:41)
at com.mockUp.ndasenda.LoginActivity$LoginRequest.doInBackground(LoginActivity.java:283)
at com.mockUp.ndasenda.LoginActivity$LoginRequest.doInBackground(LoginActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:848)
誰能幫我,如何讀取從.cer文件的私鑰。
在此先感謝。
私鑰有很多可能的格式;你的代碼需要PKCS8格式,但你不會說'private_key.cer'的格式。 –