⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pkcs8parser.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   PKCS8Parser.java

package jit.asn1parser.pkcs;

import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.pkcs.RSAPrivateKeyStructure;
import jit.asn1.pkcs.pkcs8.EncryptedPrivateKeyInfo;
import jit.asn1.pkcs.pkcs8.PrivateKeyInfo;
import jit.asn1.x509.AlgorithmIdentifier;
import jit.asn1.x9.X9ECParameters;
import jit.asn1.x9.X9PrivateKeyInfo;
import jit.asn1parser.Parser;
import jit.crypto.CipherParameters;
import jit.crypto.params.*;
import jit.cryptolib.toolkit.Crypto;
import jit.jcrypto.JKey;
import jit.math.BigInteger;

public class PKCS8Parser
{

    public PKCS8Parser()
    {
    }

    public DEREncodable generatePriKeyInfo(JKey jPriKey)
        throws Exception
    {
        CipherParameters priKey = Parser.conver2CipherParam(jPriKey);
        if(priKey instanceof RSAKeyParameters)
            return generateRSAPriKeyInfo(priKey);
        if(priKey instanceof ECKeyParameters)
            return generateECCPriKeyInfo(priKey);
        else
            throw new Exception("not support KeyType:".concat(String.valueOf(String.valueOf(priKey.getClass().getName()))));
    }

    private DEREncodable generateRSAPriKeyInfo(CipherParameters priKey)
    {
        AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption);
        RSAPrivateCrtKeyParameters param = (RSAPrivateCrtKeyParameters)priKey;
        RSAPrivateKeyStructure rsaPrvStru = new RSAPrivateKeyStructure(param.getModulus(), param.getPublicExponent(), param.getExponent(), param.getP(), param.getQ(), param.getDP(), param.getDQ(), param.getQInv());
        return new PrivateKeyInfo(algId, rsaPrvStru.getDERObject());
    }

    private DEREncodable generateECCPriKeyInfo(CipherParameters priKey)
    {
        ECPrivateKeyParameters ecPriKey = (ECPrivateKeyParameters)priKey;
        DEROctetString d = new DEROctetString(ecPriKey.getD().toByteArray());
        ECDomainParameters ecdp = ecPriKey.getParameters();
        X9ECParameters x9Params = new X9ECParameters(ecdp.getCurve(), ecdp.getG(), ecdp.getN(), ecdp.getH());
        return new X9PrivateKeyInfo(d, x9Params);
    }

    public EncryptedPrivateKeyInfo generateEPKI(byte password[], DEREncodable pki)
        throws Exception
    {
        byte salt[] = Crypto.generateSalt();
        int iterations = Crypto.generateIterations();
        ParametersWithIV param = Crypto.generatePKCS5Parameters(15, password, salt, iterations);
        byte data[] = Parser.writeDERObj2Bytes(pki.getDERObject());
        byte en_data[] = Crypto.cipherEncode(7, true, param, data);
        DEROctetString octString = new DEROctetString(en_data);
        en_data = octString.getOctets();
        DEREncodableVector derV = new DEREncodableVector();
        DEROctetString derO = new DEROctetString(salt);
        DERInteger derI = new DERInteger(iterations);
        derV.add(derO);
        derV.add(derI);
        DERSequence derS = new DERSequence(derV);
        AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.pbeWithMD5AndDES_CBC, derS);
        return new EncryptedPrivateKeyInfo(algId, en_data);
    }

    public EncryptedPrivateKeyInfo generateEPKI(byte password[], JKey jPriKey)
        throws Exception
    {
        DEREncodable pki = generatePriKeyInfo(jPriKey);
        return generateEPKI(password, pki);
    }

    public JKey getPriKeyFromEPKI(int keyType, EncryptedPrivateKeyInfo epki, byte password[])
        throws Exception
    {
        AlgorithmIdentifier algId = epki.getEncryptionAlgorithm();
        ASN1Sequence derS = (ASN1Sequence)algId.getParameters();
        byte salt[] = ((DEROctetString)derS.getObjectAt(0)).getOctets();
        int iterations = ((DERInteger)derS.getObjectAt(1)).getValue().intValue();
        DERObjectIdentifier oid = algId.getObjectId();
        int pbeEng;
        int cipherEng;
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD2AndDES_CBC))
        {
            pbeEng = 16;
            cipherEng = 7;
        } else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD2AndRC2_CBC))
        {
            pbeEng = 16;
            cipherEng = 11;
        } else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD5AndDES_CBC))
        {
            pbeEng = 15;
            cipherEng = 7;
        } else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD5AndRC2_CBC))
        {
            pbeEng = 15;
            cipherEng = 11;
        } else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC))
        {
            pbeEng = 17;
            cipherEng = 7;
        } else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithSHA1AndRC2_CBC))
        {
            pbeEng = 17;
            cipherEng = 11;
        } else
        {
            throw new Exception("not support EncryptedPrivateKeyInfo algorithm:".concat(String.valueOf(String.valueOf(oid.getId()))));
        }
        ParametersWithIV param = Crypto.generatePKCS5Parameters(pbeEng, password, salt, iterations);
        byte data[] = epki.getEncryptedData();
        byte de_data[] = Crypto.cipherEncode(cipherEng, false, param, data);
        ASN1Sequence sequence = (ASN1Sequence)Parser.writeBytes2DERObj(de_data);
        CipherParameters priKey = null;
        if(keyType == 1)
        {
            PrivateKeyInfo priKeyInfo = new PrivateKeyInfo(sequence);
            priKey = getRSAPriKey(priKeyInfo);
            return Parser.convert2JKey(2, priKey);
        }
        if(keyType == 2)
        {
            X9PrivateKeyInfo priKeyInfo = new X9PrivateKeyInfo(sequence);
            priKey = getECCPriKey(priKeyInfo);
            return Parser.convert2JKey(1002, priKey);
        } else
        {
            throw new Exception("not support keyType:".concat(String.valueOf(String.valueOf(keyType))));
        }
    }

    private CipherParameters getRSAPriKey(DEREncodable priKeyInfo)
        throws Exception
    {
        PrivateKeyInfo pki = (PrivateKeyInfo)priKeyInfo;
        if(!pki.getAlgorithmId().getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption))
        {
            throw new Exception("RSAPrivateKeyInfo algId not rigth.");
        } else
        {
            ASN1Sequence sequence = (ASN1Sequence)pki.getPrivateKey();
            RSAPrivateKeyStructure stru = new RSAPrivateKeyStructure(sequence);
            return new RSAPrivateCrtKeyParameters(stru.getModulus(), stru.getPublicExponent(), stru.getPrivateExponent(), stru.getPrime1(), stru.getPrime2(), stru.getExponent1(), stru.getExponent2(), stru.getCoefficient());
        }
    }

    private CipherParameters getECCPriKey(DEREncodable priKeyInfo)
    {
        X9PrivateKeyInfo pki = (X9PrivateKeyInfo)priKeyInfo;
        return pki.getPrivateKey();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -