美文网首页Android开发
从jks证书中提取公钥和私钥(jks证书转pem证书)

从jks证书中提取公钥和私钥(jks证书转pem证书)

作者: Dovar_66 | 来源:发表于2018-05-02 12:17 被阅读0次

    jks转为p12,然后再将p12转为pem

    AndroidStudio使用keystore文件对APK进行签名,但快应用中要求使用PEM文件对RPK进行签名。

    现有demo.jks,证书密码为demopwd,转换成pem之后依然使用demopwd作为密码。

    方法一:使用cmd和openssl命令

    提取公钥:

    切换到jks证书的存储路径,执行如下命令:keytool -list -rfc -keystore demo.jks -storepass demopwd

    如果出现下图的错误提示:

    image

    那么请把demo.jks文件拷贝到与keytool.exe文件同目录下,keytool在jdk的bin目录下,拷贝之后cmd切换到bin目录重新执行刚才的命令

    然后就能在命令行中看到打印的公钥内容(也即Certificate),如下图

    image

    提取私钥:

    jks文件中的私钥不能直接得到,需要通过openssl将jks文件转换成pkcs12格式后再进行提取。

    执行如下命令将demo.jks文件转换成demo.pfx文件:

    keytool -v -importkeystore -srckeystore demo.jks -srcstoretype jks -srcstorepass demopwd -destkeystore demo.pfx -deststoretype pkcs12 -deststorepass demopwd -destkeypass demopwd

    image

    命令执行完成后目录下就会多了一个demo.pfx文件。

    然后,执行如下命令便可以将demo.pfx的私钥导出:

    openssl pkcs12 -in demo.pfx -nocerts -nodes -out demo.key

    image

    输入密码后会生成一个demo.key文件,打开查看内容

    image

    方法二:通过代码实现

    
    import sun.misc.BASE64Encoder;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.FileWriter;
    
    import java.security.*;
    
    import java.security.cert.Certificate;
    
    public class CertUtil {
    
    private FilekeystoreFile;
    
        private StringkeyStoreType;
    
        private char[]password;
    
        private Stringalias;
    
        private FileexportedFile;
    
        public KeyPairgetPrivateKey(KeyStore keystore, String alias, char[] password) {
    
    try {
    
    Key key = keystore.getKey(alias, password);
    
                if (keyinstanceof 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----\n");
    
            Certificate cert = keystore.getCertificate(alias);
    
            PublicKey publicKey = cert.getPublicKey();
    
            String encoded2 = encoder.encode(publicKey.getEncoded());
    
            fw.write("----BEGIN CERTIFICATE----\n");
    
            fw.write(encoded2);
    
            fw.write("\n");
    
            fw.write("----END CERTIFICATE----\n");
    
            fw.close();
    
        }
    
    public static void main(String args[])throws Exception {
    
            CertUtil export =new CertUtil();
    
            export.keystoreFile =new File("/F:/Program Files/filename.jks");
    
            export.keyStoreType ="JKS";
    
            export.password ="password".toCharArray();
    
            export.alias ="alias";
    
            export.exportedFile =new File("output");
    
            export.export();
        }
    }
    
    

    相关文章

      网友评论

        本文标题:从jks证书中提取公钥和私钥(jks证书转pem证书)

        本文链接:https://www.haomeiwen.com/subject/izaqrftx.html