Search in sources :

Example 1 with InputDecryptor

use of com.github.zhenwei.pkix.operator.InputDecryptor in project LinLong-Java by zhenwei1108.

the class CMSEncryptedData method getContentStream.

public CMSTypedStream getContentStream(InputDecryptorProvider inputDecryptorProvider) throws CMSException {
    try {
        EncryptedContentInfo encContentInfo = encryptedData.getEncryptedContentInfo();
        InputDecryptor decrytor = inputDecryptorProvider.get(encContentInfo.getContentEncryptionAlgorithm());
        ByteArrayInputStream encIn = new ByteArrayInputStream(encContentInfo.getEncryptedContent().getOctets());
        return new CMSTypedStream(encContentInfo.getContentType(), decrytor.getInputStream(encIn));
    } catch (Exception e) {
        throw new CMSException("unable to create stream: " + e.getMessage(), e);
    }
}
Also used : InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) EncryptedContentInfo(com.github.zhenwei.pkix.util.asn1.cms.EncryptedContentInfo)

Example 2 with InputDecryptor

use of com.github.zhenwei.pkix.operator.InputDecryptor in project LinLong-Java by zhenwei1108.

the class EncryptedValueParser method decryptValue.

private byte[] decryptValue(ValueDecryptorGenerator decGen) throws CRMFException {
    if (value.getValueHint() != null) {
        throw new UnsupportedOperationException();
    }
    InputDecryptor decryptor = decGen.getValueDecryptor(value.getKeyAlg(), value.getSymmAlg(), value.getEncSymmKey().getBytes());
    InputStream dataIn = decryptor.getInputStream(new ByteArrayInputStream(value.getEncValue().getBytes()));
    try {
        byte[] data = Streams.readAll(dataIn);
        if (padder != null) {
            return padder.getUnpaddedData(data);
        }
        return data;
    } catch (IOException e) {
        throw new CRMFException("Cannot parse decrypted data: " + e.getMessage(), e);
    }
}
Also used : InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 3 with InputDecryptor

use of com.github.zhenwei.pkix.operator.InputDecryptor in project LinLong-Java by zhenwei1108.

the class JceAsymmetricValueDecryptorGenerator method getValueDecryptor.

public InputDecryptor getValueDecryptor(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CRMFException {
    Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
    final Cipher dataCipher = helper.createContentCipher(secretKey, contentEncryptionAlgorithm);
    return new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataIn) {
            return new CipherInputStream(dataIn, dataCipher);
        }
    };
}
Also used : CipherInputStream(com.github.zhenwei.provider.jcajce.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) CipherInputStream(com.github.zhenwei.provider.jcajce.io.CipherInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 4 with InputDecryptor

use of com.github.zhenwei.pkix.operator.InputDecryptor in project LinLong-Java by zhenwei1108.

the class BcRSAKeyTransEnvelopedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    CipherParameters secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
    final Object dataCipher = EnvelopedDataHelper.createContentCipher(false, secretKey, contentEncryptionAlgorithm);
    return new RecipientOperator(new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataIn) {
            if (dataCipher instanceof BufferedBlockCipher) {
                return new CipherInputStream(dataIn, (BufferedBlockCipher) dataCipher);
            } else {
                return new CipherInputStream(dataIn, (StreamCipher) dataCipher);
            }
        }
    });
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputStream(java.io.InputStream) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 5 with InputDecryptor

use of com.github.zhenwei.pkix.operator.InputDecryptor in project LinLong-Java by zhenwei1108.

the class JceKEKEnvelopedRecipient method getRecipientOperator.

public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
    Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentEncryptionAlgorithm, encryptedContentEncryptionKey);
    final Cipher dataCipher = contentHelper.createContentCipher(secretKey, contentEncryptionAlgorithm);
    return new RecipientOperator(new InputDecryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataOut) {
            return new CipherInputStream(dataOut, dataCipher);
        }
    });
}
Also used : CipherInputStream(com.github.zhenwei.provider.jcajce.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) CipherInputStream(com.github.zhenwei.provider.jcajce.io.CipherInputStream) InputStream(java.io.InputStream) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) Cipher(javax.crypto.Cipher) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

InputDecryptor (com.github.zhenwei.pkix.operator.InputDecryptor)16 InputStream (java.io.InputStream)14 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)12 CipherInputStream (com.github.zhenwei.provider.jcajce.io.CipherInputStream)9 Cipher (javax.crypto.Cipher)9 RecipientOperator (com.github.zhenwei.pkix.cms.RecipientOperator)8 Key (java.security.Key)6 InputDecryptorProvider (com.github.zhenwei.pkix.operator.InputDecryptorProvider)4 IOException (java.io.IOException)4 PrivateKey (java.security.PrivateKey)4 SecretKey (javax.crypto.SecretKey)4 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)3 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)3 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)3 CipherInputStream (com.github.zhenwei.core.crypto.io.CipherInputStream)3 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)2 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)2 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)2