0

我从密钥库浏览器读取证书,获取私钥存在一些问题,但公钥变得完美。下面是代码:

KeyStore keystore1 = KeyStore.getInstance("Windows-MY");
keystore1.load(null, null);

if (keystore1 != null) {
  Enumeration<String> enumeration = keystore1.aliases();

  while (enumeration.hasMoreElements()) {
    String alias = enumeration.nextElement();
    if (alias.equals("myalias")) {
       char[] keypwd = "123456".toCharArray();
       KeyStore.PrivateKeyEntry keyEnt = (KeyStore.PrivateKeyEntry) keystore1.getEntry(alias, new KeyStore.PasswordProtection(keypwd));     
       System.out.println("getPublicKey: " + keyEnt.getCertificate().getPublicKey().getEncoded());

       //show RSAPrivateKey [size=2048 bits, type=Exchange, container={5089EC94-FF45-4339-ACCF-E6ECCCB16899}]
       System.out.println("privateKey111: " + keyEnt.getPrivateKey()); 
    }
  }
}

公钥输出是正确的,但私钥看起来像这样:

RSAPrivateKey [size=2048 bits, type=Exchange, container={5089EC94-FF45-4339-ACCF-E6ECCCB16899}]

密码正确。我怎样才能得到私钥?

4

1 回答 1

0

这是我使用的一个私钥导出器,它读取 JKS 密钥库,也许您可​​以先转换密钥库或相应地修改您的代码。

c:\test>java -classes ./classes ExportPrivateKey mystore.ks JKS mystorepwd myalias mycert_priv.crt

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import sun.misc.BASE64Encoder;

public class ExportPrivateKey {
    private File keystoreFile;
    private String keyStoreType;
    private char[] password;
    private String alias;
    private File exportedFile;

    public static KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
        try {
            Key key=keystore.getKey(alias,password);
            if(key instanceof PrivateKey) {
                Certificate cert=keystore.getCertificate(alias);
                PublicKey publicKey=cert.getPublicKey();
                return new KeyPair(publicKey,(PrivateKey)key);
            }
        } catch (UnrecoverableKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (KeyStoreException e) { }
        return null;
    }

    public void export() throws Exception{
        KeyStore keystore=KeyStore.getInstance(keyStoreType);
        BASE64Encoder encoder=new BASE64Encoder();
        keystore.load(new FileInputStream(keystoreFile),password);
        KeyPair keyPair=getPrivateKey(keystore,alias,password);
        PrivateKey privateKey=keyPair.getPrivate();
        String encoded=encoder.encode(privateKey.getEncoded());
        FileWriter fw=new FileWriter(exportedFile);
        fw.write(“—–BEGIN PRIVATE KEY—–\n“);
        fw.write(encoded);
        fw.write(“\n“);
        fw.write(“—–END PRIVATE KEY—–”);
        fw.close();
    }

    public static void main(String args[]) throws Exception{
        ExportPrivateKey export=new ExportPrivateKey();
        export.keystoreFile=new File(args[0]);
        export.keyStoreType=args[1];
        export.password=args[2].toCharArray();
        export.alias=args[3];
        export.exportedFile=new File(args[4]);
        export.export();
    }
}
于 2013-05-03T11:24:25.127 回答