Example usage for org.apache.commons.crypto.cipher CryptoCipherFactory getCryptoCipher

List of usage examples for org.apache.commons.crypto.cipher CryptoCipherFactory getCryptoCipher

Introduction

In this page you can find the example usage for org.apache.commons.crypto.cipher CryptoCipherFactory getCryptoCipher.

Prototype

public static CryptoCipher getCryptoCipher(String transformation, Properties props)
        throws GeneralSecurityException 

Source Link

Document

Gets a cipher instance for specified algorithm/mode/padding.

Usage

From source file:org.apache.spark.network.crypto.AuthEngine.java

private void initializeForAuth(String cipher, byte[] nonce, SecretKeySpec key) throws GeneralSecurityException {

    // commons-crypto currently only supports ciphers that require an initial vector; so
    // create a dummy vector so that we can initialize the ciphers. In the future, if
    // different ciphers are supported, this will have to be configurable somehow.
    byte[] iv = new byte[conf.ivLength()];
    System.arraycopy(nonce, 0, iv, 0, Math.min(nonce.length, iv.length));

    encryptor = CryptoCipherFactory.getCryptoCipher(cipher, cryptoConf);
    encryptor.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

    decryptor = CryptoCipherFactory.getCryptoCipher(cipher, cryptoConf);
    decryptor.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
}

From source file:org.apache.spark.network.sasl.aes.AesCipher.java

/**
 * Create the configuration message/*  w w  w . ja va 2 s .  c o m*/
 * @param conf is the local transport configuration.
 * @return Config message for sending.
 */
public static AesConfigMessage createConfigMessage(TransportConf conf) {
    int keySize = conf.aesCipherKeySize();
    Properties properties = conf.cryptoConf();

    try {
        int paramLen = CryptoCipherFactory.getCryptoCipher(AesCipher.TRANSFORM, properties).getBlockSize();
        byte[] inKey = new byte[keySize];
        byte[] outKey = new byte[keySize];
        byte[] inIv = new byte[paramLen];
        byte[] outIv = new byte[paramLen];

        CryptoRandom random = CryptoRandomFactory.getCryptoRandom(properties);
        random.nextBytes(inKey);
        random.nextBytes(outKey);
        random.nextBytes(inIv);
        random.nextBytes(outIv);

        return new AesConfigMessage(inKey, inIv, outKey, outIv);
    } catch (Exception e) {
        logger.error("AES config error", e);
        throw Throwables.propagate(e);
    }
}