简体   繁体   English

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

[英]export private key in .der file as PKCS#1

I'm writing a java code to generate keys and save them in files, I am using BouncyCastle library to write the privatekey into .pem file using pemwriter(if it is in PKCS#1) and using a regular FileOutputStream to export it into PKCS#8. 我正在编写一个Java代码以生成密钥并将其保存在文件中,我正在使用BouncyCastle库使用pemwriter(如果在PKCS#1中)将私钥写入.pem文件中,并使用常规的FileOutputStream将其导出到PKCS中#8。

Now when exporting into DER, the problem come when trying to export it in PKCS#1. 现在,当导出到DER时,尝试在PKCS#1中导出它时就会出现问题。

I searched a lot but cannot find a suitable way to encode the privatekey in PKCS#1 or to convert the regular encoding of java privatekey's (PKCS#8) to PKCS#1, or if you can guide me to convert PrivateKey to RSAPrivateKey or DSAPrivateKey or ECPrivateKey . 我进行了大量搜索,但找不到合适的方法来在PKCS#1中对DSAPrivateKey进行RSAPrivateKey或将RSAPrivateKey DSAPrivateKey (PKCS#8)的常规编码转换为PKCS#1,或者是否可以指导我将PrivateKey转换为RSAPrivateKeyDSAPrivateKeyECPrivateKey Here is a snippet of my code to export 这是我要导出的代码的片段

        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(); 

where privateKeyBytes are the returned bytes of PrivateKey.getEncoded(). 其中privateKeyBytes是PrivateKey.getEncoded().的返回字节PrivateKey.getEncoded(). they are in PKCS#8 and if I can convert PrivateKey to RSAPrivateKey or DSAPrivateKey they represent the private key in PKCS#1 format 它们在PKCS#8中,如果我可以将PrivateKey转换为RSAPrivateKey或DSAPrivateKey,它们表示PKCS#1格式的私钥

Apparently you can use type information and perform class casting 显然,您可以使用类型信息并执行类转换

   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