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

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

Introduction

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

Prototype

String CLASSES_KEY

To view the source code for org.apache.commons.crypto.cipher CryptoCipherFactory CLASSES_KEY.

Click Source Link

Document

The configuration key of the CryptoCipher implementation class.

Usage

From source file:org.apache.commons.crypto.examples.CipherByteArrayExample.java

public static void main(String[] args) throws Exception {

    final SecretKeySpec key = new SecretKeySpec(getUTF8Bytes("1234567890123456"), "AES");
    final IvParameterSpec iv = new IvParameterSpec(getUTF8Bytes("1234567890123456"));

    Properties properties = new Properties();
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.OPENSSL.getClassName());
    //Creates a CryptoCipher instance with the transformation and properties.
    final String transform = "AES/CBC/PKCS5Padding";
    CryptoCipher encipher = Utils.getCipherInstance(transform, properties);
    System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());

    final String sampleInput = "hello world!";
    System.out.println("input:  " + sampleInput);

    byte[] input = getUTF8Bytes(sampleInput);
    byte[] output = new byte[32];

    //Initializes the cipher with ENCRYPT_MODE, key and iv.
    encipher.init(Cipher.ENCRYPT_MODE, key, iv);
    //Continues a multiple-part encryption/decryption operation for byte array.
    int updateBytes = encipher.update(input, 0, input.length, output, 0);
    System.out.println(updateBytes);
    //We must call doFinal at the end of encryption/decryption.
    int finalBytes = encipher.doFinal(input, 0, 0, output, updateBytes);
    System.out.println(finalBytes);
    //Closes the cipher.
    encipher.close();//from www  .j a  v a2 s.c  om

    System.out.println(Arrays.toString(Arrays.copyOf(output, updateBytes + finalBytes)));

    // Now reverse the process using a different implementation with the same settings
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, CipherProvider.JCE.getClassName());
    CryptoCipher decipher = Utils.getCipherInstance(transform, properties);
    System.out.println("Cipher:  " + encipher.getClass().getCanonicalName());

    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    byte[] decoded = new byte[32];
    decipher.doFinal(output, 0, updateBytes + finalBytes, decoded, 0);

    System.out.println("output: " + new String(decoded, StandardCharsets.UTF_8));
}

From source file:org.apache.hadoop.hbase.io.crypto.aes.CommonsCryptoAES.java

private static Properties readCryptoProps(Configuration conf) {
    Properties props = new Properties();

    props.setProperty(CryptoCipherFactory.CLASSES_KEY, conf.get(CIPHER_CLASSES_KEY, ""));
    props.setProperty(CryptoCipherFactory.JCE_PROVIDER_KEY, conf.get(CIPHER_JCE_PROVIDER_KEY, ""));

    return props;
}

From source file:org.apache.hadoop.hbase.ipc.ServerRpcConnection.java

/**
 * Set up cipher for rpc encryption with Apache Commons Crypto
 *
 * @throws FatalConnectionException/*from   w w  w  . j a va2 s  . co m*/
 */
private void setupCryptoCipher(final ConnectionHeader header,
        RPCProtos.ConnectionHeaderResponse.Builder chrBuilder) throws FatalConnectionException {
    // If simple auth, return
    if (saslServer == null)
        return;
    // check if rpc encryption with Crypto AES
    String qop = saslServer.getNegotiatedQop();
    boolean isEncryption = SaslUtil.QualityOfProtection.PRIVACY.getSaslQop().equalsIgnoreCase(qop);
    boolean isCryptoAesEncryption = isEncryption
            && this.rpcServer.conf.getBoolean("hbase.rpc.crypto.encryption.aes.enabled", false);
    if (!isCryptoAesEncryption)
        return;
    if (!header.hasRpcCryptoCipherTransformation())
        return;
    String transformation = header.getRpcCryptoCipherTransformation();
    if (transformation == null || transformation.length() == 0)
        return;
    // Negotiates AES based on complete saslServer.
    // The Crypto metadata need to be encrypted and send to client.
    Properties properties = new Properties();
    // the property for SecureRandomFactory
    properties.setProperty(CryptoRandomFactory.CLASSES_KEY,
            this.rpcServer.conf.get("hbase.crypto.sasl.encryption.aes.crypto.random",
                    "org.apache.commons.crypto.random.JavaCryptoRandom"));
    // the property for cipher class
    properties.setProperty(CryptoCipherFactory.CLASSES_KEY, this.rpcServer.conf
            .get("hbase.rpc.crypto.encryption.aes.cipher.class", "org.apache.commons.crypto.cipher.JceCipher"));

    int cipherKeyBits = this.rpcServer.conf.getInt("hbase.rpc.crypto.encryption.aes.cipher.keySizeBits", 128);
    // generate key and iv
    if (cipherKeyBits % 8 != 0) {
        throw new IllegalArgumentException("The AES cipher key size in bits" + " should be a multiple of byte");
    }
    int len = cipherKeyBits / 8;
    byte[] inKey = new byte[len];
    byte[] outKey = new byte[len];
    byte[] inIv = new byte[len];
    byte[] outIv = new byte[len];

    try {
        // generate the cipher meta data with SecureRandom
        CryptoRandom secureRandom = CryptoRandomFactory.getCryptoRandom(properties);
        secureRandom.nextBytes(inKey);
        secureRandom.nextBytes(outKey);
        secureRandom.nextBytes(inIv);
        secureRandom.nextBytes(outIv);

        // create CryptoAES for server
        cryptoAES = new CryptoAES(transformation, properties, inKey, outKey, inIv, outIv);
        // create SaslCipherMeta and send to client,
        //  for client, the [inKey, outKey], [inIv, outIv] should be reversed
        RPCProtos.CryptoCipherMeta.Builder ccmBuilder = RPCProtos.CryptoCipherMeta.newBuilder();
        ccmBuilder.setTransformation(transformation);
        ccmBuilder.setInIv(getByteString(outIv));
        ccmBuilder.setInKey(getByteString(outKey));
        ccmBuilder.setOutIv(getByteString(inIv));
        ccmBuilder.setOutKey(getByteString(inKey));
        chrBuilder.setCryptoCipherMeta(ccmBuilder);
        useCryptoAesWrap = true;
    } catch (GeneralSecurityException | IOException ex) {
        throw new UnsupportedCryptoException(ex.getMessage(), ex);
    }
}