1. 程式人生 > 實用技巧 >ECC加密證書與RSA證書

ECC加密證書與RSA證書

linux生成ECC證書

openssl ecparam -out EccCA.key -name prime256v1 -genkey
openssl req -config openssl.cnf -key EccCA.key -new -out EccCA.req 
openssl x509 -req -in EccCA.req -signkey EccCA.key -out EccCA.pem   //生成根證書
openssl ecparam -out EccSite.key -name prime256v1 -genkey
openssl req -config openssl.cnf -key EccSite.key -new -out EccSite.req
openssl x509 -req -in EccSite.req -CA EccCA.pem -CAkey EccCA.key -out EccSite.pem -CAcreateserial //使用根證書籤名的證書

pem證書轉p12證書

openssl pkcs12 -export -inkey EccSite.key -in EccSite.pem -passin pass:123456 -passout pass:123456 -out EccSite.p12 

windows jdk自帶keytool生成pfx證書:

執行命令
keytool -genkey
-alias test --別名
-keypass 123123 --私鑰密碼
-keyalg RSA --演算法
-sigalg sha256withrsa --演算法小類
-keysize 1024 --金鑰長度
-validity 365 --有效期


-keystore d:/test.jks --生成路徑
-storepass 123123 --主密碼

即:

keytool -genkey -alias czz -keypass 123123 -keyalg RSA -sigalg sha256withrsa -keysize 1024 -validity 365 -keystore d:/czz.jks -storepass 123123

將生成的jks轉成pfx證書

package com.jdwa.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class JKS2PFX {
    private static final String KEYSTORE_PASSWORD = "12345678";

    private static void coverToPfx() {
        try {
            KeyStore inputKeyStore = KeyStore.getInstance("JKS");
            FileInputStream fis = new FileInputStream("D://czz.jks");
            char[] nPassword = null;
            if ((KEYSTORE_PASSWORD == null)
                    || KEYSTORE_PASSWORD.trim().equals("")) {
                nPassword = null;
            } else {
                nPassword = KEYSTORE_PASSWORD.toCharArray();
            }

            inputKeyStore.load(fis, nPassword);
            fis.close();

            KeyStore outputKeyStore = KeyStore.getInstance("PKCS12");
            outputKeyStore.load(null, KEYSTORE_PASSWORD.toCharArray());

            Enumeration enums = inputKeyStore.aliases();
            while (enums.hasMoreElements()) {
                String keyAlias = (String) enums.nextElement();
                System.out.println("alias=[" + keyAlias + "]");
                if (inputKeyStore.isKeyEntry(keyAlias)) {
                    Key key = inputKeyStore.getKey(keyAlias, nPassword);
                    Certificate[] certChain = inputKeyStore
                            .getCertificateChain(keyAlias);
                    outputKeyStore.setKeyEntry(keyAlias, key, KEYSTORE_PASSWORD
                            .toCharArray(), certChain);
                }
            }
            FileOutputStream out = new FileOutputStream("D://czz.pfx");
            outputKeyStore.store(out, nPassword);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        coverToPfx();
    }
}
歡迎大家可以關注我的微信公眾號,方便利用零碎時間互相交流。共勉!

路漫漫其修遠兮,吾將上下而求索。。。