Search in sources :

Example 1 with CipherInputStream

use of com.github.zhenwei.core.crypto.io.CipherInputStream 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 2 with CipherInputStream

use of com.github.zhenwei.core.crypto.io.CipherInputStream in project LinLong-Java by zhenwei1108.

the class BcPKCS12PBEInputDecryptorProviderBuilder method build.

public InputDecryptorProvider build(final char[] password) {
    return new InputDecryptorProvider() {

        public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) {
            final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm());
            PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters());
            CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), digest, engine.getBlockSize(), pbeParams, password);
            engine.init(false, params);
            return new InputDecryptor() {

                public AlgorithmIdentifier getAlgorithmIdentifier() {
                    return algorithmIdentifier;
                }

                public InputStream getInputStream(InputStream input) {
                    return new CipherInputStream(input, engine);
                }

                public GenericKey getKey() {
                    return new GenericKey(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
                }
            };
        }
    };
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) InputDecryptorProvider(com.github.zhenwei.pkix.operator.InputDecryptorProvider) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputDecryptor(com.github.zhenwei.pkix.operator.InputDecryptor) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) CipherInputStream(com.github.zhenwei.core.crypto.io.CipherInputStream) InputStream(java.io.InputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with CipherInputStream

use of com.github.zhenwei.core.crypto.io.CipherInputStream in project LinLong-Java by zhenwei1108.

the class BcPasswordEnvelopedRecipient method getRecipientOperator.

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

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return contentEncryptionAlgorithm;
        }

        public InputStream getInputStream(InputStream dataOut) {
            if (dataCipher instanceof BufferedBlockCipher) {
                return new CipherInputStream(dataOut, (BufferedBlockCipher) dataCipher);
            } else {
                return new CipherInputStream(dataOut, (StreamCipher) dataCipher);
            }
        }
    });
}
Also used : 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) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) RecipientOperator(com.github.zhenwei.pkix.cms.RecipientOperator) StreamCipher(com.github.zhenwei.core.crypto.StreamCipher) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 CipherInputStream (com.github.zhenwei.core.crypto.io.CipherInputStream)3 InputDecryptor (com.github.zhenwei.pkix.operator.InputDecryptor)3 InputStream (java.io.InputStream)3 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)2 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)2 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)2 RecipientOperator (com.github.zhenwei.pkix.cms.RecipientOperator)2 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 PaddedBufferedBlockCipher (com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher)1 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 InputDecryptorProvider (com.github.zhenwei.pkix.operator.InputDecryptorProvider)1