2
我試圖使用加密使用一個.pfx證書文件的文本文件:如何使用Android的X509公共證書進行加密?
public void EncryptUsingPublicKey(File in, File out, File publicKeyFile) throws IOException, GeneralSecurityException {
byte[] encodedKey = new byte[(int)publicKeyFile.length()];
new FileInputStream(publicKeyFile).read(encodedKey);
// create public key
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pk = kf.generatePublic(publicKeySpec);
FileInputStream is = new FileInputStream(in);
Cipher pkCipher = Cipher.getInstance("RSA");
pkCipher.init(Cipher.ENCRYPT_MODE, pk);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), pkCipher);
copy(is, os);
os.close();
}
我有兩個問題:
- 我應該在哪裏存儲在設備上的.pfx文件?
- 此功能是否正確?
謝謝你,我會檢查出保護。 – Chris
我用portecle專家的.CER(我選擇導出>頭榮譽證書和DER編碼),現在得到的錯誤 java.lang.IllegalArgumentException異常:錯誤的序列大小:3 但X509CertificateStructure不n要似乎存在? 我一直在閱讀這個http://bouncy-castle.1462172.n4.nabble.com/J2ME-SubjectPublicKeyInfo-Bad-sequence-size-td1467139.html – Chris