src/main/java/cn/jiguang/common/utils/sm2/BCECUtil.java
public static byte[] convertECPrivateKeySEC1ToPKCS8(byte[] sec1Key) throws IOException {
        /**
         * 参考org.bouncycastle.asn1.pkcs.PrivateKeyInfo和
         * org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey，逆向拼装
         */
        X962Parameters params = getDomainParametersFromName(SM2Util.JDK_EC_SPEC, false);
        ASN1OctetString privKey = new DEROctetString(sec1Key);
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1Integer(0)); //版本号
        v.add(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params)); //算法标识
        v.add(privKey);
        DERSequence ds = new DERSequence(v);
        return ds.getEncoded(ASN1Encoding.DER);
    }
public static byte[] convertECPrivateKeySEC1ToPKCS8(byte[] sec1Key) throws IOException {
        /**
         * 参考org.bouncycastle.asn1.pkcs.PrivateKeyInfo和
         * org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey，逆向拼装
         */
        X962Parameters params = getDomainParametersFromName(SM2Util.JDK_EC_SPEC, false);
        ASN1OctetString privKey = new DEROctetString(sec1Key);
        ASN1EncodableVector v = new ASN1EncodableVector();
        v.add(new ASN1Integer(0)); //版本号
        v.add(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params)); //算法标识
        v.add(privKey);
        DERSequence ds = new DERSequence(v);
        return ds.getEncoded(ASN1Encoding.DER);
    }
public static byte [ ] convertECPrivateKeySEC1ToPKCS8 ( byte [ ] sec1Key ) throws IOException { /**
         * 参考org.bouncycastle.asn1.pkcs.PrivateKeyInfo和
         * org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey，逆向拼装
         */ X962Parameters params = getDomainParametersFromName ( SM2Util . JDK_EC_SPEC , false ) ; ASN1OctetString privKey = new DEROctetString ( sec1Key ) ; ASN1EncodableVector v = new ASN1EncodableVector ( ) ; v . add ( new ASN1Integer ( 0 ) ) ; //版本号 v . add ( new AlgorithmIdentifier ( X9ObjectIdentifiers . id_ecPublicKey , params ) ) ; //算法标识 v . add ( privKey ) ; DERSequence ds = new DERSequence ( v ) ; return ds . getEncoded ( ASN1Encoding . DER ) ; }
将SEC1标准的私钥字节流恢复为PKCS8标准的字节流

@param sec1Key
@return
@throws IOException
将SEC1标准的私钥字节流恢复为PKCS8标准的字节流