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

📄 encrypteddataparser.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// 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:   EncryptedDataParser.java

package jit.asn1parser.pkcs.pkcs7;

import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.pkcs.pkcs7.*;
import jit.asn1.x509.AlgorithmIdentifier;
import jit.asn1parser.Parser;
import jit.cryptolib.toolkit.Crypto;
import jit.jcrypto.*;
import jit.jcrypto.param.CBCParam;
import jit.jcrypto.param.PBEParam;
import jit.jcrypto.soft.JMechanism;
import jit.math.BigInteger;

public class EncryptedDataParser
{

    static int SYMMETRIC_MOD = 0;
    static int PBE_MOD = 1;
    static int RSA_MOD = 2;
    static int ECC_MOD = 3;
    private Session session;

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

    public EncryptedContentInfo generateEncryptedContentInfo(DERObjectIdentifier contentType, DERObjectIdentifier encryptionID, byte data[], JKey jkey)
        throws Exception
    {
        byte encryptedData[] = null;
        Mechanism randomM = new JMechanism(0x80000020);
        Mechanism cryptoM = null;
        AlgorithmIdentifier encryptionAlg = new AlgorithmIdentifier(encryptionID, null);
        int params[] = getCryptoType(encryptionID);
        if(params[0] == SYMMETRIC_MOD)
        {
            boolean isCBCMod = false;
            byte iv[] = session.generateRandom(randomM, 8);
            DEROctetString doct = new DEROctetString(iv);
            CBCParam cbcParam = new CBCParam(iv);
            if(encryptionID.equals(PKCSObjectIdentifiers.desEncryption))
                cryptoM = new JMechanism(289);
            else
            if(encryptionID.equals(PKCSObjectIdentifiers.desCBCEncryption))
            {
                cryptoM = new JMechanism(290, cbcParam);
                isCBCMod = true;
            } else
            if(encryptionID.equals(PKCSObjectIdentifiers.rc2Encryption))
                cryptoM = new JMechanism(257);
            else
            if(encryptionID.equals(PKCSObjectIdentifiers.rc2CBCEncryption))
            {
                cryptoM = new JMechanism(258, cbcParam);
                isCBCMod = true;
            } else
            if(encryptionID.equals(PKCSObjectIdentifiers.des3Encryption))
                cryptoM = new JMechanism(306);
            else
            if(encryptionID.equals(PKCSObjectIdentifiers.des3CBCEncryption))
            {
                cryptoM = new JMechanism(307, cbcParam);
                isCBCMod = true;
            }
            if(isCBCMod)
                encryptionAlg = new AlgorithmIdentifier(encryptionID, doct);
        } else
        if(params[0] == RSA_MOD)
            cryptoM = new JMechanism(1);
        else
        if(params[0] == ECC_MOD)
            cryptoM = new JMechanism(1026);
        else
            return generatePBEECI(contentType, encryptionID, jkey, data);
        encryptedData = session.encrypt(cryptoM, jkey, data);
        DEROctetString encryptedContent = new DEROctetString(encryptedData);
        return new EncryptedContentInfo(contentType, encryptionAlg, encryptedContent);
    }

    public EncryptedContentInfo generateEncryptedContentInfo(DERObjectIdentifier encryptionID, byte data[], JKey jkey)
        throws Exception
    {
        DERObjectIdentifier contentType = PKCSObjectIdentifiers.data;
        return generateEncryptedContentInfo(contentType, encryptionID, data, jkey);
    }

    public EncryptedContentInfo generateEncryptedContentInfo(DERObjectIdentifier encryptionID, DEREncodable obj, JKey jkey)
        throws Exception
    {
        DERObjectIdentifier contentType = null;
        if(obj instanceof SignedData)
            contentType = PKCSObjectIdentifiers.signedData;
        else
        if(obj instanceof EnvelopedData)
            contentType = PKCSObjectIdentifiers.envelopedData;
        else
        if(obj instanceof SignedAndEnvelopedData)
            contentType = PKCSObjectIdentifiers.signedAndEnvelopedData;
        else
        if(obj instanceof DigestedData)
            contentType = PKCSObjectIdentifiers.digestedData;
        else
        if(obj instanceof EncryptedData)
            contentType = PKCSObjectIdentifiers.encryptedData;
        else
            contentType = PKCSObjectIdentifiers.data;
        byte data[] = null;
        if(contentType.equals(PKCSObjectIdentifiers.data))
            data = ((ASN1OctetString)obj).getOctets();
        else
            data = Parser.writeDERObj2Bytes(obj.getDERObject());
        return generateEncryptedContentInfo(contentType, encryptionID, data, jkey);
    }

    private EncryptedContentInfo generatePBEECI(DERObjectIdentifier contentType, DERObjectIdentifier oid, JKey jkey, byte data[])
        throws Exception
    {
        Mechanism randomM = new JMechanism(0x80000020);
        byte salt[] = session.generateRandom(randomM, 8);
        int iterations = Crypto.generateIterations();
        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 encryptionAlg = new AlgorithmIdentifier(oid, derS);
        PBEParam pbeParam = new PBEParam(iterations, salt);
        Mechanism cryptoM = null;
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD2AndDES_CBC))
            cryptoM = new JMechanism(0x80000010, pbeParam);
        else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD2AndRC2_CBC))
            cryptoM = new JMechanism(0x80000013, pbeParam);
        else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD5AndDES_CBC))
            cryptoM = new JMechanism(0x80000011, pbeParam);
        else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithMD5AndRC2_CBC))
            cryptoM = new JMechanism(0x80000014, pbeParam);
        else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC))
            cryptoM = new JMechanism(0x80000012, pbeParam);
        else
        if(oid.equals(PKCSObjectIdentifiers.pbeWithSHA1AndRC2_CBC))
            cryptoM = new JMechanism(0x80000015, pbeParam);
        else
            throw new Exception("not support encryption algorithm:".concat(String.valueOf(String.valueOf(oid.getId()))));
        byte encryptedData[] = session.encrypt(cryptoM, jkey, data);
        derO = new DEROctetString(encryptedData);
        return new EncryptedContentInfo(contentType, encryptionAlg, derO);
    }

    public EncryptedData generateEncryptedData(EncryptedContentInfo eci)
    {
        return new EncryptedData(new DERInteger(0), eci);
    }

    public EncryptedContentInfo getECIFromEncryptedData(EncryptedData encryptedData)
    {
        return encryptedData.getEncryptedContentInfo();
    }

    private int[] getCryptoType(DERObjectIdentifier encryptionID)
        throws Exception
    {
        int mod = -1;
        int eng = -1;
        if(encryptionID.equals(PKCSObjectIdentifiers.desEncryption))
        {
            mod = SYMMETRIC_MOD;
            eng = 6;
        } else
        if(encryptionID.equals(PKCSObjectIdentifiers.desCBCEncryption))
        {
            mod = SYMMETRIC_MOD;
            eng = 7;
        } else
        if(encryptionID.equals(PKCSObjectIdentifiers.des3Encryption))
        {

⌨️ 快捷键说明

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