📄 encrypteddataparser.java
字号:
mod = SYMMETRIC_MOD;
eng = 8;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.des3CBCEncryption))
{
mod = SYMMETRIC_MOD;
eng = 9;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.rc2Encryption))
{
mod = SYMMETRIC_MOD;
eng = 10;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.rc2CBCEncryption))
{
mod = SYMMETRIC_MOD;
eng = 11;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.rsaEncryption))
{
mod = RSA_MOD;
eng = 12;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.ecEncryption))
{
mod = ECC_MOD;
eng = 13;
} else
if(encryptionID.equals(PKCSObjectIdentifiers.pbeWithMD2AndDES_CBC) || encryptionID.equals(PKCSObjectIdentifiers.pbeWithMD2AndRC2_CBC) || encryptionID.equals(PKCSObjectIdentifiers.pbeWithMD5AndDES_CBC) || encryptionID.equals(PKCSObjectIdentifiers.pbeWithMD5AndRC2_CBC) || encryptionID.equals(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) || encryptionID.equals(PKCSObjectIdentifiers.pbeWithSHA1AndRC2_CBC))
mod = PBE_MOD;
else
throw new Exception("not support encryption algorithm:".concat(String.valueOf(String.valueOf(encryptionID.getId()))));
int params[] = new int[2];
params[0] = mod;
params[1] = eng;
return params;
}
public byte[] decryptEncryptedContentInfo(EncryptedContentInfo encryptedContentInfo, JKey jkey)
throws Exception
{
AlgorithmIdentifier alg = encryptedContentInfo.getContentEncryptionAlgorithm();
DERObjectIdentifier encryptionID = alg.getObjectId();
byte encryptedData[] = encryptedContentInfo.getEncryptedContent().getOctets();
Mechanism cryptoM = null;
int params[] = getCryptoType(alg.getObjectId());
if(params[0] == SYMMETRIC_MOD)
{
DEROctetString doct = (DEROctetString)alg.getParameters();
byte iv[] = null;
CBCParam cbcParam = null;
if(encryptionID.equals(PKCSObjectIdentifiers.desEncryption))
cryptoM = new JMechanism(289);
else
if(encryptionID.equals(PKCSObjectIdentifiers.desCBCEncryption))
{
iv = doct.getOctets();
cbcParam = new CBCParam(iv);
cryptoM = new JMechanism(290, cbcParam);
} else
if(encryptionID.equals(PKCSObjectIdentifiers.rc2Encryption))
cryptoM = new JMechanism(257);
else
if(encryptionID.equals(PKCSObjectIdentifiers.rc2CBCEncryption))
{
iv = doct.getOctets();
cbcParam = new CBCParam(iv);
cryptoM = new JMechanism(258, cbcParam);
} else
if(encryptionID.equals(PKCSObjectIdentifiers.des3Encryption))
cryptoM = new JMechanism(306);
else
if(encryptionID.equals(PKCSObjectIdentifiers.des3CBCEncryption))
{
iv = doct.getOctets();
cbcParam = new CBCParam(iv);
cryptoM = new JMechanism(307, cbcParam);
}
} else
if(params[0] == RSA_MOD)
cryptoM = new JMechanism(1);
else
if(params[0] == ECC_MOD)
cryptoM = new JMechanism(1026);
else
return decryptedPBEData(alg, jkey, encryptedData);
return session.decrypt(cryptoM, jkey, encryptedData);
}
private byte[] decryptedPBEData(AlgorithmIdentifier alg, JKey jkey, byte enData[])
throws Exception
{
ASN1Sequence seq = (ASN1Sequence)alg.getParameters();
if(seq == null)
throw new Exception("Not enough params in PBE algorithmIdentifier.");
byte salt[] = ((ASN1OctetString)seq.getObjectAt(0)).getOctets();
int iterations = ((DERInteger)seq.getObjectAt(1)).getValue().intValue();
PBEParam pbeParam = new PBEParam(iterations, salt);
Mechanism cryptoM = null;
DERObjectIdentifier oid = alg.getObjectId();
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);
return session.decrypt(cryptoM, jkey, enData);
}
public byte[] decryptEncryptedData(EncryptedData encryptedData, JKey jkey)
throws Exception
{
EncryptedContentInfo eci = encryptedData.getEncryptedContentInfo();
return decryptEncryptedContentInfo(eci, jkey);
}
public DEREncodable decryptObjectFromEncryptedECI(EncryptedContentInfo eci, JKey jkey)
throws Exception
{
byte data[] = decryptEncryptedContentInfo(eci, jkey);
ASN1Sequence seq = null;
DERObjectIdentifier oid = eci.getContentType();
if(oid.equals(PKCSObjectIdentifiers.signedData))
{
seq = (ASN1Sequence)Parser.writeBytes2DERObj(data);
return new SignedData(seq);
}
if(oid.equals(PKCSObjectIdentifiers.envelopedData))
{
seq = (ASN1Sequence)Parser.writeBytes2DERObj(data);
return new EnvelopedData(seq);
}
if(oid.equals(PKCSObjectIdentifiers.signedAndEnvelopedData))
{
seq = (ASN1Sequence)Parser.writeBytes2DERObj(data);
return new SignedAndEnvelopedData(seq);
}
if(oid.equals(PKCSObjectIdentifiers.digestedData))
{
seq = (ASN1Sequence)Parser.writeBytes2DERObj(data);
return new DigestedData(seq);
}
if(oid.equals(PKCSObjectIdentifiers.encryptedData))
{
seq = (ASN1Sequence)Parser.writeBytes2DERObj(data);
return new EncryptedData(seq);
}
if(oid.equals(PKCSObjectIdentifiers.data))
return new DEROctetString(data);
else
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("not support contentType:")).append(oid.getId()).append(" in EncryptionContentInfo."))));
}
public DEREncodable decryptObjectFromEncryptedED(EncryptedData encryptedData, JKey jkey)
throws Exception
{
EncryptedContentInfo eci = encryptedData.getEncryptedContentInfo();
return decryptObjectFromEncryptedECI(eci, jkey);
}
public ContentInfo generateEncryptedDataContent(EncryptedData encryptedData)
{
return new ContentInfo(PKCSObjectIdentifiers.encryptedData, encryptedData);
}
public EncryptedData getEncryptedDataFromContent(ContentInfo contentInfo)
throws Exception
{
if(!contentInfo.getContentType().equals(PKCSObjectIdentifiers.encryptedData))
throw new Exception("the Content type is not EncryptedData.");
else
return EncryptedData.getInstance(contentInfo.getContent());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -