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

📄 envelopeddataparser.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:   EnvelopedDataParser.java

package jit.asn1parser.pkcs.pkcs7;

import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.pkcs.pkcs12.Pfx;
import jit.asn1.pkcs.pkcs7.*;
import jit.asn1.x509.*;
import jit.asn1parser.pkcs.PKCS12Parser;
import jit.asn1parser.x509.SPKIParser;
import jit.asn1parser.x509.X509Cert;
import jit.jcrypto.JKey;
import jit.jcrypto.Session;
import jit.jcrypto.soft.JMechanism;
import jit.math.BigInteger;

// Referenced classes of package jit.asn1parser.pkcs.pkcs7:
//            EncryptedDataParser

public class EnvelopedDataParser
{

    EncryptedDataParser encryptedDataParser;
    private Session session;

    public EnvelopedDataParser(Session session)
    {
        encryptedDataParser = null;
        this.session = null;
        this.session = session;
        encryptedDataParser = new EncryptedDataParser(session);
    }

    public RecipientInfo generateRecipientInfo(IssuerAndSerialNumber issuerAndSN, AlgorithmIdentifier keyEncryptionAlg, byte contentEncryptKey[], JKey jrecipientPubKey)
        throws Exception
    {
        if(keyEncryptionAlg == null)
            throw new Exception("keyEncryptionAlg must not be null");
        if(issuerAndSN == null)
            throw new Exception("Issuer And SerialNumber must not be null");
        byte encryptedKey[] = null;
        JMechanism mechanism = null;
        if(jrecipientPubKey.getKeyType() == 1)
        {
            if(!keyEncryptionAlg.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption))
                throw new Exception("keyEncryptionAlg not match up to the keyEncryptKey");
            mechanism = new JMechanism(1);
            encryptedKey = session.encrypt(mechanism, jrecipientPubKey, contentEncryptKey);
        } else
        if(jrecipientPubKey.getKeyType() == 1001)
        {
            if(!keyEncryptionAlg.getObjectId().equals(PKCSObjectIdentifiers.ecEncryption))
                throw new Exception("keyEncryptionAlg not match up to the keyEncryptKey");
            mechanism = new JMechanism(1026);
            encryptedKey = session.encrypt(mechanism, jrecipientPubKey, contentEncryptKey);
        }
        DEROctetString derEncryptedKey = new DEROctetString(encryptedKey);
        RecipientInfo recipientInfo = new RecipientInfo(new DERInteger(0), issuerAndSN, keyEncryptionAlg, derEncryptedKey);
        return recipientInfo;
    }

    public EncryptedContentInfo generateEncryptedContentInfo(DERObjectIdentifier contentType, DERObjectIdentifier contentEncryptionOID, byte contentInfo[], JKey contentEncryptKey)
        throws Exception
    {
        if(contentType == null || contentEncryptionOID == null)
            throw new Exception("contentType and contentEncryptionAlg must not be null");
        if(contentEncryptionOID.equals(PKCSObjectIdentifiers.desCBCEncryption) || contentEncryptionOID.equals(PKCSObjectIdentifiers.rc2CBCEncryption) || contentEncryptionOID.equals(PKCSObjectIdentifiers.des3CBCEncryption))
            return encryptedDataParser.generateEncryptedContentInfo(contentType, contentEncryptionOID, contentInfo, contentEncryptKey);
        else
            throw new Exception("not support the contentEncryptionOID:".concat(String.valueOf(String.valueOf(contentEncryptionOID.getId()))));
    }

    public EnvelopedData generateEnvelopedData(DERObjectIdentifier contentType, byte content[], DERObjectIdentifier contentEncryptionOID, X509Cert recipientCert)
        throws Exception
    {
        JMechanism mechanism = null;
        if(contentEncryptionOID.equals(PKCSObjectIdentifiers.desCBCEncryption))
            mechanism = new JMechanism(288);
        else
        if(contentEncryptionOID.equals(PKCSObjectIdentifiers.rc2CBCEncryption))
            mechanism = new JMechanism(256);
        else
        if(contentEncryptionOID.equals(PKCSObjectIdentifiers.des3CBCEncryption))
            mechanism = new JMechanism(305);
        else
            throw new Exception("unsupported content encryption OID".concat(String.valueOf(String.valueOf(contentEncryptionOID.getId()))));
        JKey contentEncryptionKey = session.generateKey(mechanism, 0);
        byte bContentEncryptionKey[] = contentEncryptionKey.getKey();
        SubjectPublicKeyInfo spki = recipientCert.getSubjectPublicKeyInfo();
        SPKIParser spkiParser = new SPKIParser();
        JKey recipientPubKey = spkiParser.getPublicKey(spki);
        BigInteger sn = recipientCert.getSerialNumber();
        X509Name issuer = recipientCert.getIssuer();
        IssuerAndSerialNumber issuerAndSerialNumber = new IssuerAndSerialNumber(issuer, sn);
        AlgorithmIdentifier keyEncryptionAlg = null;
        if(recipientPubKey.getKeyType() == 1)
            keyEncryptionAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption);
        else
        if(recipientPubKey.getKeyType() == 1001)
            keyEncryptionAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.ecEncryption);
        RecipientInfo recipientInfo = generateRecipientInfo(issuerAndSerialNumber, keyEncryptionAlg, bContentEncryptionKey, recipientPubKey);
        EncryptedContentInfo encryptedContentInfo = generateEncryptedContentInfo(contentType, contentEncryptionOID, content, contentEncryptionKey);
        return generateEnvelopedData(recipientInfo, encryptedContentInfo);
    }

    public EnvelopedData generateEnvelopedData(RecipientInfo recipientInfo, EncryptedContentInfo encryptedContentInfo)
        throws Exception
    {
        if(recipientInfo == null || encryptedContentInfo == null)
        {
            throw new Exception("elements of EnvelopedData must not be null");
        } else
        {
            DERSet recipientInfoSet = new DERSet(recipientInfo.getDERObject());
            EnvelopedData envelopedData = new EnvelopedData(new DERInteger(0), recipientInfoSet, encryptedContentInfo);
            return envelopedData;
        }
    }

    public byte[] decodeEnvelop(EnvelopedData envelopedData, Pfx pfx, char pfxPwd[])
        throws Exception
    {
        PKCS12Parser p12Parser = new PKCS12Parser();
        p12Parser.load(pfx);
        p12Parser.decrypt(pfxPwd);
        JKey recipientPriKey = p12Parser.getPrivateKey();
        X509Cert cert[] = p12Parser.getCertificates();
        X509Cert pubCert = cert[0];
        BigInteger sn = pubCert.getSerialNumber();
        X509Name issuer = pubCert.getIssuer();
        IssuerAndSerialNumber acturalRecipienter = new IssuerAndSerialNumber(issuer, sn);
        ASN1Set recipientInfos = envelopedData.getRecipientInfos();
        RecipientInfo recipientInfo[] = new RecipientInfo[recipientInfos.size()];
        recipientInfo[0] = new RecipientInfo((ASN1Sequence)recipientInfos.getObjectAt(0));
        IssuerAndSerialNumber recipienter = recipientInfo[0].getIssuerAndSerialNumber();
        if(!recipienter.equals(acturalRecipienter))
            throw new Exception("the recipienter cert is not suitable.");
        AlgorithmIdentifier keyEncryptAlg = recipientInfo[0].getKeyEncryptionAlgorithm();
        ASN1OctetString octetEncrptKey = recipientInfo[0].getEncryptedKey();
        byte encryptedKey[] = octetEncrptKey.getOctets();
        byte bContentEncryptKey[] = null;
        JMechanism mechanism = null;
        if(recipientPriKey.getKeyType() == 2)
        {
            if(!keyEncryptAlg.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption))
                throw new Exception("wrong type of  keyEncryptKey");
            mechanism = new JMechanism(1);
            bContentEncryptKey = session.decrypt(mechanism, recipientPriKey, encryptedKey);
        } else
        if(recipientPriKey.getKeyType() == 1002)
        {
            if(!keyEncryptAlg.getObjectId().equals(PKCSObjectIdentifiers.ecEncryption))
                throw new Exception("wrong type of  keyEncryptKey");
            mechanism = new JMechanism(1026);
            bContentEncryptKey = session.decrypt(mechanism, recipientPriKey, encryptedKey);
        } else
        {
            throw new Exception("unsupported type of recipientPriKey");
        }
        JKey contentEncryptKey = new JKey(145, bContentEncryptKey);
        EncryptedContentInfo encryptedContentInfo = envelopedData.getEncryptedContentInfo();
        byte contentInfo[] = encryptedDataParser.decryptEncryptedContentInfo(encryptedContentInfo, contentEncryptKey);
        return contentInfo;
    }

    public byte[] decodeEnvelop(EnvelopedData envelopedData, X509Cert recipientCert, JKey recipientPriKey)
        throws Exception
    {
        BigInteger sn = recipientCert.getSerialNumber();
        X509Name issuer = recipientCert.getIssuer();
        IssuerAndSerialNumber acturalRecipienter = new IssuerAndSerialNumber(issuer, sn);
        ASN1Set recipientInfos = envelopedData.getRecipientInfos();
        RecipientInfo recipientInfo[] = new RecipientInfo[recipientInfos.size()];
        recipientInfo[0] = new RecipientInfo((ASN1Sequence)recipientInfos.getObjectAt(0));
        IssuerAndSerialNumber recipienter = recipientInfo[0].getIssuerAndSerialNumber();
        if(!recipienter.equals(acturalRecipienter))
            throw new Exception("the recipienter cert is not suitable.");
        AlgorithmIdentifier keyEncryptAlg = recipientInfo[0].getKeyEncryptionAlgorithm();
        ASN1OctetString octetEncrptKey = recipientInfo[0].getEncryptedKey();
        byte encryptedKey[] = octetEncrptKey.getOctets();
        byte bContentEncryptKey[] = null;
        JMechanism mechanism = null;
        if(recipientPriKey.getKeyType() == 2)
        {
            if(!keyEncryptAlg.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption))
                throw new Exception("wrong type of  keyEncryptKey");
            mechanism = new JMechanism(1);
            bContentEncryptKey = session.decrypt(mechanism, recipientPriKey, encryptedKey);
        } else
        if(recipientPriKey.getKeyType() == 1002)
        {
            if(!keyEncryptAlg.getObjectId().equals(PKCSObjectIdentifiers.ecEncryption))
                throw new Exception("wrong type of  keyEncryptKey");
            mechanism = new JMechanism(1026);
            bContentEncryptKey = session.decrypt(mechanism, recipientPriKey, encryptedKey);
        } else
        {
            throw new Exception("unsupported type of recipientPriKey");
        }
        JKey contentEncryptKey = new JKey(145, bContentEncryptKey);
        EncryptedContentInfo encryptedContentInfo = envelopedData.getEncryptedContentInfo();
        byte contentInfo[] = encryptedDataParser.decryptEncryptedContentInfo(encryptedContentInfo, contentEncryptKey);
        return contentInfo;
    }

    public ContentInfo generateEnvelopedDataContent(EnvelopedData envelopData)
    {
        return new ContentInfo(PKCSObjectIdentifiers.envelopedData, envelopData);
    }

    public EnvelopedData getEnvelopedDataFromContentInfo(ContentInfo contentInfo)
        throws Exception
    {
        if(!contentInfo.getContentType().equals(PKCSObjectIdentifiers.envelopedData))
            throw new Exception("content type is not EnvelopedData");
        else
            return EnvelopedData.getInstance(contentInfo.getContent());
    }
}

⌨️ 快捷键说明

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