繁体   English   中英

将.der文件中的私钥导出为PKCS#1

[英]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转换为RSAPrivateKeyDSAPrivateKeyECPrivateKey 这是我要导出的代码的片段

        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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM