![](/img/trans.png)
[英]Unable do client authentication with PKCS#1 private key with java keyStore
[英]export private key in .der file as PKCS#1
我正在編寫一個Java代碼以生成密鑰並將其保存在文件中,我正在使用BouncyCastle
庫使用pemwriter(如果在PKCS#1中)將私鑰寫入.pem文件中,並使用常規的FileOutputStream將其導出到PKCS中#8。
現在,當導出到DER時,嘗試在PKCS#1中導出它時就會出現問題。
我進行了大量搜索,但找不到合適的方法來在PKCS#1中對DSAPrivateKey
進行RSAPrivateKey
或將RSAPrivateKey
DSAPrivateKey
(PKCS#8)的常規編碼轉換為PKCS#1,或者是否可以指導我將PrivateKey
轉換為RSAPrivateKey
或DSAPrivateKey
或ECPrivateKey
。 這是我要導出的代碼的片段
JcePEMEncryptorBuilder builder = new JcePEMEncryptorBuilder("DES-EDE3-CBC");
PEMEncryptor enc = builder.build(password);
FileOutputStream fis = new FileOutputStream(new File(privatekey.der));
if (isPKCS8) {
if (!encrypt) {
fis.write(privateKeyBytes);
} else {
fis.write(enc.encrypt(privateKeyBytes));
}
fis.flush();
fis.close();
其中privateKeyBytes是PrivateKey.getEncoded().
的返回字節PrivateKey.getEncoded().
它們在PKCS#8中,如果我可以將PrivateKey轉換為RSAPrivateKey或DSAPrivateKey,它們表示PKCS#1
格式的私鑰
顯然,您可以使用類型信息並執行類轉換
PrivateKey privKey = ...;
if (privKey instance of RSAPrivateKey) {
RSAPrivateKey rsaPrivKey = (RSAPrivateKey)privKey;
if (privKey instance of DSAPrivateKey) {
DSAPrivateKey dsaPrivKey = (DSAPrivateKey)privKey;
if (privKey instance of ECPrivateKey) {
ECPrivateKey ecPrivKey = (ECPrivateKey)privKey;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.