Search in sources :

Example 1 with CharToByteConverter

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

the class JceOpenSSLPKCS8DecryptorProviderBuilder method build.

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

        public InputDecryptor get(final AlgorithmIdentifier algorithm) throws OperatorCreationException {
            final Cipher cipher;
            try {
                if (PEMUtilities.isPKCS5Scheme2(algorithm.getAlgorithm())) {
                    PBES2Parameters params = PBES2Parameters.getInstance(algorithm.getParameters());
                    KeyDerivationFunc func = params.getKeyDerivationFunc();
                    EncryptionScheme scheme = params.getEncryptionScheme();
                    PBKDF2Params defParams = (PBKDF2Params) func.getParameters();
                    int iterationCount = defParams.getIterationCount().intValue();
                    byte[] salt = defParams.getSalt();
                    String oid = scheme.getAlgorithm().getId();
                    SecretKey key;
                    if (PEMUtilities.isHmacSHA1(defParams.getPrf())) {
                        key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, oid, password, salt, iterationCount);
                    } else {
                        key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, oid, password, salt, iterationCount, defParams.getPrf());
                    }
                    cipher = helper.createCipher(oid);
                    AlgorithmParameters algParams = helper.createAlgorithmParameters(oid);
                    algParams.init(scheme.getParameters().toASN1Primitive().getEncoded());
                    cipher.init(Cipher.DECRYPT_MODE, key, algParams);
                } else if (PEMUtilities.isPKCS12(algorithm.getAlgorithm())) {
                    PKCS12PBEParams params = PKCS12PBEParams.getInstance(algorithm.getParameters());
                    cipher = helper.createCipher(algorithm.getAlgorithm().getId());
                    cipher.init(Cipher.DECRYPT_MODE, new PKCS12KeyWithParameters(password, params.getIV(), params.getIterations().intValue()));
                } else if (PEMUtilities.isPKCS5Scheme1(algorithm.getAlgorithm())) {
                    PBEParameter params = PBEParameter.getInstance(algorithm.getParameters());
                    cipher = helper.createCipher(algorithm.getAlgorithm().getId());
                    cipher.init(Cipher.DECRYPT_MODE, new PBKDF1KeyWithParameters(password, new CharToByteConverter() {

                        public String getType() {
                            return "ASCII";
                        }

                        public byte[] convert(char[] password) {
                            // just drop hi-order byte.
                            return Strings.toByteArray(password);
                        }
                    }, params.getSalt(), params.getIterationCount().intValue()));
                } else {
                    throw new PEMException("Unknown algorithm: " + algorithm.getAlgorithm());
                }
                return new InputDecryptor() {

                    public AlgorithmIdentifier getAlgorithmIdentifier() {
                        return algorithm;
                    }

                    public InputStream getInputStream(InputStream encIn) {
                        return new CipherInputStream(encIn, cipher);
                    }
                };
            } catch (IOException e) {
                throw new OperatorCreationException(algorithm.getAlgorithm() + " not available: " + e.getMessage(), e);
            } catch (GeneralSecurityException e) {
                throw new OperatorCreationException(algorithm.getAlgorithm() + " not available: " + e.getMessage(), e);
            }
        }
    };
}
Also used : PBEParameter(com.github.zhenwei.core.asn1.pkcs.PBEParameter) PBES2Parameters(com.github.zhenwei.core.asn1.pkcs.PBES2Parameters) EncryptionScheme(com.github.zhenwei.core.asn1.pkcs.EncryptionScheme) 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) PBKDF1KeyWithParameters(com.github.zhenwei.provider.jcajce.PBKDF1KeyWithParameters) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CharToByteConverter(com.github.zhenwei.core.crypto.CharToByteConverter) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SecretKey(javax.crypto.SecretKey) InputDecryptorProvider(com.github.zhenwei.pkix.operator.InputDecryptorProvider) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) PEMException(com.github.zhenwei.pkix.openssl.PEMException) KeyDerivationFunc(com.github.zhenwei.core.asn1.pkcs.KeyDerivationFunc) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) Cipher(javax.crypto.Cipher) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) PKCS12KeyWithParameters(com.github.zhenwei.provider.jcajce.PKCS12KeyWithParameters) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

EncryptionScheme (com.github.zhenwei.core.asn1.pkcs.EncryptionScheme)1 KeyDerivationFunc (com.github.zhenwei.core.asn1.pkcs.KeyDerivationFunc)1 PBEParameter (com.github.zhenwei.core.asn1.pkcs.PBEParameter)1 PBES2Parameters (com.github.zhenwei.core.asn1.pkcs.PBES2Parameters)1 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)1 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 CharToByteConverter (com.github.zhenwei.core.crypto.CharToByteConverter)1 PEMException (com.github.zhenwei.pkix.openssl.PEMException)1 InputDecryptor (com.github.zhenwei.pkix.operator.InputDecryptor)1 InputDecryptorProvider (com.github.zhenwei.pkix.operator.InputDecryptorProvider)1 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)1 PBKDF1KeyWithParameters (com.github.zhenwei.provider.jcajce.PBKDF1KeyWithParameters)1 PKCS12KeyWithParameters (com.github.zhenwei.provider.jcajce.PKCS12KeyWithParameters)1 CipherInputStream (com.github.zhenwei.provider.jcajce.io.CipherInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AlgorithmParameters (java.security.AlgorithmParameters)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Cipher (javax.crypto.Cipher)1