Search in sources :

Example 1 with CipherOutputStream

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

the class BcPKCS12PBEOutputEncryptorBuilder method build.

public OutputEncryptor build(final char[] password) {
    if (random == null) {
        random = new SecureRandom();
    }
    final byte[] salt = new byte[20];
    random.nextBytes(salt);
    final PKCS12PBEParams pbeParams = new PKCS12PBEParams(salt, iterationCount);
    CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithm, digest, engine.getBlockSize(), pbeParams, password);
    engine.init(true, params);
    return new OutputEncryptor() {

        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return new AlgorithmIdentifier(algorithm, pbeParams);
        }

        public OutputStream getOutputStream(OutputStream out) {
            return new CipherOutputStream(out, engine);
        }

        public GenericKey getKey() {
            return new GenericKey(new AlgorithmIdentifier(algorithm, pbeParams), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
        }
    };
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) CipherOutputStream(com.github.zhenwei.core.crypto.io.CipherOutputStream) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) OutputStream(java.io.OutputStream) CipherOutputStream(com.github.zhenwei.core.crypto.io.CipherOutputStream) SecureRandom(java.security.SecureRandom) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OutputEncryptor(com.github.zhenwei.pkix.operator.OutputEncryptor) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Aggregations

PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 CipherOutputStream (com.github.zhenwei.core.crypto.io.CipherOutputStream)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 OutputEncryptor (com.github.zhenwei.pkix.operator.OutputEncryptor)1 OutputStream (java.io.OutputStream)1 SecureRandom (java.security.SecureRandom)1