zoukankan      html  css  js  c++  java
  • tomcat的jks的私钥导出nginx需要的key文件

    方法一:

    1.先用keytool导出pfx文件。第一个123456是jks密码,后边两个是新生成的pfx的密码

    keytool -v -importkeystore -srckeystore D:\fuwuqi.jks -srcstoretype jks -srcstorepass 123456 -destkeystore D:\fuwuqi.pfx -deststoretype pkcs12 -deststorepass 123456 -destkeypass 123456

    2.用ssl导出key文件

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

    方法二:

     代码生成

    package com.acca.dovepay.nucc.utils;
    
    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 CertUtil {
    
        private File keystoreFile;
    
        private String keyStoreType;
    
        private char[] password;
    
        private String alias;
    
        private File exportedFile;
    
        public 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----
    ");
    
            fw.write(encoded);
    
            fw.write("
    ");
    
            fw.write("----END PRIVATE KEY----
    ");
    
            Certificate cert = keystore.getCertificate(alias);
    
            PublicKey publicKey = cert.getPublicKey();
    
            String encoded2 = encoder.encode(publicKey.getEncoded());
    
            fw.write("----BEGIN CERTIFICATE----
    ");
    
            fw.write(encoded2);
    
            fw.write("
    ");
    
            fw.write("----END CERTIFICATE----
    ");
    
            fw.close();
    
        }
    
        public static void main(String args[]) throws Exception {
    
            CertUtil export = new CertUtil();
    
            export.keystoreFile = new File("D:\20181120fuwuqi.jks");
    
            export.keyStoreType = "JKS";
    
            export.password = "123456".toCharArray();
    
            export.alias = "mykey";
    
            export.exportedFile = new File("D:\SSS.key");
    
            export.export();
        }
    }

    另crt文件的生成,转载https://jingyan.baidu.com/article/154b463178eac928ca8f41a9.html?qq-pf-to=pcqq.c2c

  • 相关阅读:
    使用pdm建表并生成SQL语句
    eclipse从svn检出项目之后,找不到BuildPath
    如何搞定SVN目录的cleanup问题和lock问题
    ORA-00923: 未找到要求的 FROM 关键字
    java.sql.SQLException: ORA-00911: 无效字符
    10.vue-router实现路由懒加载( 动态加载路由 )
    9、vue-router的两种模式(hash模式和history模式)的区别
    8、vue-router传递参数的几种方式
    5、vue-router有哪几种导航钩子( 导航守卫 )
    4.怎么定义 vue-router 的动态路由? 怎么获取传过来的值
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/9999493.html
Copyright © 2011-2022 走看看