FileDocCategorySizeDatePackage
PasswordRecipientInformation.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)6201Wed Oct 01 10:55:28 BST 2008org.bouncycastle.cms

PasswordRecipientInformation

public class PasswordRecipientInformation extends RecipientInformation
the RecipientInfo class for a recipient who has been sent a message encrypted using a password.

Fields Summary
private org.bouncycastle.asn1.cms.PasswordRecipientInfo
_info
private org.bouncycastle.asn1.x509.AlgorithmIdentifier
_encAlg
Constructors Summary
public PasswordRecipientInformation(org.bouncycastle.asn1.cms.PasswordRecipientInfo info, org.bouncycastle.asn1.x509.AlgorithmIdentifier encAlg, InputStream data)

        super(encAlg, AlgorithmIdentifier.getInstance(info.getKeyEncryptionAlgorithm()), data);

        this._info = info;
        this._encAlg = encAlg;
        this._rid = new RecipientId();
    
Methods Summary
public CMSTypedStreamgetContentStream(java.security.Key key, java.lang.String prov)
decrypt the content and return an input stream.

        return getContentStream(key, CMSUtils.getProvider(prov));
    
public CMSTypedStreamgetContentStream(java.security.Key key, java.security.Provider prov)
decrypt the content and return an input stream.

        try
        {
            AlgorithmIdentifier kekAlg = AlgorithmIdentifier.getInstance(_info.getKeyEncryptionAlgorithm());
            ASN1Sequence        kekAlgParams = (ASN1Sequence)kekAlg.getParameters();
            byte[]              encryptedKey = _info.getEncryptedKey().getOctets();
            String              kekAlgName = DERObjectIdentifier.getInstance(kekAlgParams.getObjectAt(0)).getId();
            Cipher keyCipher = Cipher.getInstance(
                                        CMSEnvelopedHelper.INSTANCE.getRFC3211WrapperName(kekAlgName), prov);

            IvParameterSpec ivSpec = new IvParameterSpec(ASN1OctetString.getInstance(kekAlgParams.getObjectAt(1)).getOctets());
            keyCipher.init(Cipher.UNWRAP_MODE, new SecretKeySpec(((CMSPBEKey)key).getEncoded(kekAlgName), kekAlgName), ivSpec);

            AlgorithmIdentifier aid = _encAlg;
            String              alg = aid.getObjectId().getId();
            Key                 sKey = keyCipher.unwrap(
                                        encryptedKey, alg, Cipher.SECRET_KEY);

            return getContentFromSessionKey(sKey, prov);
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new CMSException("can't find algorithm.", e);
        }
        catch (InvalidKeyException e)
        {
            throw new CMSException("key invalid in message.", e);
        }
        catch (NoSuchPaddingException e)
        {
            throw new CMSException("required padding not supported.", e);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new CMSException("invalid iv.", e);
        }
    
public java.lang.StringgetKeyDerivationAlgOID()
return the object identifier for the key derivation algorithm, or null if there is none present.

return
OID for key derivation algorithm, if present.

        if (_info.getKeyDerivationAlgorithm() != null)
        {
            return _info.getKeyDerivationAlgorithm().getObjectId().getId();
        }

        return null;
    
public java.security.AlgorithmParametersgetKeyDerivationAlgParameters(java.lang.String provider)
return an AlgorithmParameters object representing the parameters to the key derivation algorithm to the recipient.

return
AlgorithmParameters object, null if there aren't any.

        return getKeyDerivationAlgParameters(CMSUtils.getProvider(provider));
    
public java.security.AlgorithmParametersgetKeyDerivationAlgParameters(java.security.Provider provider)
return an AlgorithmParameters object representing the parameters to the key derivation algorithm to the recipient.

return
AlgorithmParameters object, null if there aren't any.

        try
        {
            if (_info.getKeyDerivationAlgorithm() != null)
            {
                DEREncodable params = _info.getKeyDerivationAlgorithm().getParameters();
                if (params != null)
                {
                    AlgorithmParameters algP = AlgorithmParameters.getInstance(_info.getKeyDerivationAlgorithm().getObjectId().toString(), provider);

                    algP.init(params.getDERObject().getEncoded());

                    return algP;
                }
            }

            return null;
        }
        catch (Exception e)
        {
            throw new RuntimeException("exception getting encryption parameters " + e);
        }
    
public byte[]getKeyDerivationAlgParams()
return the ASN.1 encoded key derivation algorithm parameters, or null if there aren't any.

return
ASN.1 encoding of key derivation algorithm parameters.

        try
        {
            if (_info.getKeyDerivationAlgorithm() != null)
            {
                DEREncodable params = _info.getKeyDerivationAlgorithm().getParameters();
                if (params != null)
                {
                    return params.getDERObject().getEncoded();
                }
            }

            return null;
        }
        catch (Exception e)
        {
            throw new RuntimeException("exception getting encryption parameters " + e);
        }