Search in sources :

Example 1 with PaddedBufferedBlockCipher

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

the class DESExample method process.

private void process() {
    /*
     * Setup the DESede cipher engine, create a PaddedBufferedBlockCipher
     * in CBC mode.
     */
    cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
    if (encrypt) {
        performEncrypt(key);
    } else {
        performDecrypt(key);
    }
    // after processing clean up the files
    try {
        in.close();
        out.flush();
        out.close();
    } catch (IOException closing) {
        System.err.println("exception closing resources: " + closing.getMessage());
    }
}
Also used : PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher) IOException(java.io.IOException) DESedeEngine(com.github.zhenwei.core.crypto.engines.DESedeEngine)

Example 2 with PaddedBufferedBlockCipher

use of com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher 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 PaddedBufferedBlockCipher

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

the class PEMUtilities method crypt.

static byte[] crypt(boolean encrypt, byte[] bytes, char[] password, String dekAlgName, byte[] iv) throws PEMException {
    byte[] ivValue = iv;
    String blockMode = "CBC";
    BlockCipher engine;
    BlockCipherPadding padding = new PKCS7Padding();
    KeyParameter sKey;
    // Figure out block mode and padding.
    if (dekAlgName.endsWith("-CFB")) {
        blockMode = "CFB";
        padding = null;
    }
    if (dekAlgName.endsWith("-ECB") || "DES-EDE".equals(dekAlgName) || "DES-EDE3".equals(dekAlgName)) {
        // ECB is actually the default (though seldom used) when OpenSSL
        // uses DES-EDE (des2) or DES-EDE3 (des3).
        blockMode = "ECB";
        ivValue = null;
    }
    if (dekAlgName.endsWith("-OFB")) {
        blockMode = "OFB";
        padding = null;
    }
    // Figure out algorithm and key size.
    if (dekAlgName.startsWith("DES-EDE")) {
        // "DES-EDE" is actually des2 in OpenSSL-speak!
        // "DES-EDE3" is des3.
        boolean des2 = !dekAlgName.startsWith("DES-EDE3");
        sKey = getKey(password, 24, iv, des2);
        engine = new DESedeEngine();
    } else if (dekAlgName.startsWith("DES-")) {
        sKey = getKey(password, 8, iv);
        engine = new DESEngine();
    } else if (dekAlgName.startsWith("BF-")) {
        sKey = getKey(password, 16, iv);
        engine = new BlowfishEngine();
    } else if (dekAlgName.startsWith("RC2-")) {
        int keyBits = 128;
        if (dekAlgName.startsWith("RC2-40-")) {
            keyBits = 40;
        } else if (dekAlgName.startsWith("RC2-64-")) {
            keyBits = 64;
        }
        sKey = new RC2Parameters(getKey(password, keyBits / 8, iv).getKey(), keyBits);
        ;
        engine = new RC2Engine();
    } else if (dekAlgName.startsWith("AES-")) {
        byte[] salt = iv;
        if (salt.length > 8) {
            salt = new byte[8];
            System.arraycopy(iv, 0, salt, 0, 8);
        }
        int keyBits;
        if (dekAlgName.startsWith("AES-128-")) {
            keyBits = 128;
        } else if (dekAlgName.startsWith("AES-192-")) {
            keyBits = 192;
        } else if (dekAlgName.startsWith("AES-256-")) {
            keyBits = 256;
        } else {
            throw new EncryptionException("unknown AES encryption with private key: " + dekAlgName);
        }
        sKey = getKey(password, keyBits / 8, salt);
        engine = new AESEngine();
    } else {
        throw new EncryptionException("unknown encryption with private key: " + dekAlgName);
    }
    if (blockMode.equals("CBC")) {
        engine = new CBCBlockCipher(engine);
    } else if (blockMode.equals("CFB")) {
        engine = new CFBBlockCipher(engine, engine.getBlockSize() * 8);
    } else if (blockMode.equals("OFB")) {
        engine = new OFBBlockCipher(engine, engine.getBlockSize() * 8);
    }
    try {
        BufferedBlockCipher c;
        if (padding == null) {
            c = new BufferedBlockCipher(engine);
        } else {
            c = new PaddedBufferedBlockCipher(engine, padding);
        }
        if (// ECB block mode
        ivValue == null) {
            c.init(encrypt, sKey);
        } else {
            c.init(encrypt, new ParametersWithIV(sKey, ivValue));
        }
        byte[] out = new byte[c.getOutputSize(bytes.length)];
        int procLen = c.processBytes(bytes, 0, bytes.length, out, 0);
        procLen += c.doFinal(out, procLen);
        if (procLen == out.length) {
            return out;
        } else {
            byte[] rv = new byte[procLen];
            System.arraycopy(out, 0, rv, 0, procLen);
            return rv;
        }
    } catch (Exception e) {
        throw new EncryptionException("exception using cipher - please check password and data.", e);
    }
}
Also used : RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) AESEngine(com.github.zhenwei.core.crypto.engines.AESEngine) OFBBlockCipher(com.github.zhenwei.core.crypto.modes.OFBBlockCipher) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher) CFBBlockCipher(com.github.zhenwei.core.crypto.modes.CFBBlockCipher) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) BlockCipher(com.github.zhenwei.core.crypto.BlockCipher) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) OFBBlockCipher(com.github.zhenwei.core.crypto.modes.OFBBlockCipher) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BlowfishEngine(com.github.zhenwei.core.crypto.engines.BlowfishEngine) RC2Engine(com.github.zhenwei.core.crypto.engines.RC2Engine) EncryptionException(com.github.zhenwei.pkix.openssl.EncryptionException) PEMException(com.github.zhenwei.pkix.openssl.PEMException) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) PKCS7Padding(com.github.zhenwei.core.crypto.paddings.PKCS7Padding) CFBBlockCipher(com.github.zhenwei.core.crypto.modes.CFBBlockCipher) DESEngine(com.github.zhenwei.core.crypto.engines.DESEngine) BlockCipherPadding(com.github.zhenwei.core.crypto.paddings.BlockCipherPadding) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) EncryptionException(com.github.zhenwei.pkix.openssl.EncryptionException) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher) DESedeEngine(com.github.zhenwei.core.crypto.engines.DESedeEngine)

Example 4 with PaddedBufferedBlockCipher

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

the class BrokenJCEBlockCipher method engineSetMode.

protected void engineSetMode(String mode) {
    String modeName = Strings.toUpperCase(mode);
    if (modeName.equals("ECB")) {
        ivLength = 0;
        cipher = new PaddedBufferedBlockCipher(cipher.getUnderlyingCipher());
    } else if (modeName.equals("CBC")) {
        ivLength = cipher.getUnderlyingCipher().getBlockSize();
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(cipher.getUnderlyingCipher()));
    } else if (modeName.startsWith("OFB")) {
        ivLength = cipher.getUnderlyingCipher().getBlockSize();
        if (modeName.length() != 3) {
            int wordSize = Integer.parseInt(modeName.substring(3));
            cipher = new PaddedBufferedBlockCipher(new OFBBlockCipher(cipher.getUnderlyingCipher(), wordSize));
        } else {
            cipher = new PaddedBufferedBlockCipher(new OFBBlockCipher(cipher.getUnderlyingCipher(), 8 * cipher.getBlockSize()));
        }
    } else if (modeName.startsWith("CFB")) {
        ivLength = cipher.getUnderlyingCipher().getBlockSize();
        if (modeName.length() != 3) {
            int wordSize = Integer.parseInt(modeName.substring(3));
            cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(cipher.getUnderlyingCipher(), wordSize));
        } else {
            cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(cipher.getUnderlyingCipher(), 8 * cipher.getBlockSize()));
        }
    } else {
        throw new IllegalArgumentException("can't support mode " + mode);
    }
}
Also used : PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) OFBBlockCipher(com.github.zhenwei.core.crypto.modes.OFBBlockCipher) CFBBlockCipher(com.github.zhenwei.core.crypto.modes.CFBBlockCipher) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher)

Aggregations

PaddedBufferedBlockCipher (com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher)4 CBCBlockCipher (com.github.zhenwei.core.crypto.modes.CBCBlockCipher)3 DESedeEngine (com.github.zhenwei.core.crypto.engines.DESedeEngine)2 CFBBlockCipher (com.github.zhenwei.core.crypto.modes.CFBBlockCipher)2 OFBBlockCipher (com.github.zhenwei.core.crypto.modes.OFBBlockCipher)2 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 BlockCipher (com.github.zhenwei.core.crypto.BlockCipher)1 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)1 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 AESEngine (com.github.zhenwei.core.crypto.engines.AESEngine)1 BlowfishEngine (com.github.zhenwei.core.crypto.engines.BlowfishEngine)1 DESEngine (com.github.zhenwei.core.crypto.engines.DESEngine)1 RC2Engine (com.github.zhenwei.core.crypto.engines.RC2Engine)1 CipherInputStream (com.github.zhenwei.core.crypto.io.CipherInputStream)1 BlockCipherPadding (com.github.zhenwei.core.crypto.paddings.BlockCipherPadding)1 PKCS7Padding (com.github.zhenwei.core.crypto.paddings.PKCS7Padding)1 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)1 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)1 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)1