Example usage for javax.crypto.spec IvParameterSpec IvParameterSpec

List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec

Introduction

In this page you can find the example usage for javax.crypto.spec IvParameterSpec IvParameterSpec.

Prototype

public IvParameterSpec(byte[] iv, int offset, int len) 

Source Link

Document

Creates an IvParameterSpec object using the first len bytes in iv, beginning at offset inclusive, as the IV.

Usage

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static String encrypt(String input, String key) throws Exception {
    byte[] crypted = null;
    javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(key.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(INIT_VECTOR, 0, INIT_VECTOR.length));
    crypted = cipher.doFinal(input.getBytes());
    return new String(Hex.encode(crypted));
}

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static byte[] encrypt(String input, String key, byte[] iv) throws Exception {
    javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(key.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(INIT_VECTOR, 0, INIT_VECTOR.length));
    return cipher.doFinal(input.getBytes());
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * AES Encryption CBC Mode with PKCS5 Padding
 *
 * @param dataB64 Data to encrypt Base64 encoded
 * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long.
 * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long
 * @return Encrypted data Base64 Encoded
 * @throws NoSuchAlgorithmException/*  w  ww. ja v a2 s .  com*/
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String aesEncrypt(String dataB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String encryptedB64 = null;

    final byte[] dataBytes = Base64.decodeBase64(dataB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(AES_CIPHER);

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretBytes, "AES"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes));
    return encryptedB64;
}

From source file:com.sshtools.j2ssh.transport.cipher.BlowfishCbc.java

/**
 *
 *
 * @param mode/* w w w . ja v  a 2  s .  c om*/
 * @param iv
 * @param keydata
 *
 * @throws AlgorithmOperationException
 */
public void init(int mode, byte[] iv, byte[] keydata) throws AlgorithmOperationException {
    try {
        cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");

        // Create a 16 byte key
        byte[] actualKey = new byte[16];
        System.arraycopy(keydata, 0, actualKey, 0, actualKey.length);

        SecretKeySpec keyspec = new SecretKeySpec(actualKey, "Blowfish");

        // Create the cipher according to its algorithm
        cipher.init(((mode == ENCRYPT_MODE) ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), keyspec,
                new IvParameterSpec(iv, 0, cipher.getBlockSize()));
    } catch (NoSuchPaddingException nspe) {
        log.error("Blowfish initialization failed", nspe);
        throw new AlgorithmOperationException("No Padding not supported");
    } catch (NoSuchAlgorithmException nsae) {
        log.error("Blowfish initialization failed", nsae);
        throw new AlgorithmOperationException("Algorithm not supported");
    } catch (InvalidKeyException ike) {
        log.error("Blowfish initialization failed", ike);
        throw new AlgorithmOperationException("Invalid encryption key");

        /*} catch (InvalidKeySpecException ispe) {
             throw new AlgorithmOperationException("Invalid encryption key specification");*/
    } catch (InvalidAlgorithmParameterException ape) {
        log.error("Blowfish initialization failed", ape);
        throw new AlgorithmOperationException("Invalid algorithm parameter");
    }
}

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static String decrypt(String input, String key, byte[] iv) throws Exception {
    byte[] output = null;
    javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(key.getBytes()));
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv, 0, iv.length));
    output = cipher.doFinal(Hex.decode(input));
    return new String(output);
}

From source file:com.vmware.bdd.security.EncryptionGuard.java

/**
 * Get a cipher instance when need it, not share between multiple threads
 * because cipher is not thread safe./*from  ww w .j a v  a 2  s . c  o  m*/
 * 
 * @param opmode
 *           the operation mode
 * @param key
 *           the key
 * @return initialized cipher instance
 */
private static Cipher getCiperInternal(int opmode, Key key) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance(TRANSFORMATION);
    IvParameterSpec ips = new IvParameterSpec(IV_PARAMETER, 0, 16);
    cipher.init(opmode, key, ips);
    return cipher;
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * AES Decryption CBC Mode with PKCS5 Padding
 *
 * @param encryptedB64 Encrypted Data Base64 encoded.
 * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long.
 * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long
 * @return Original data Base64 encoded.
 * @throws NoSuchAlgorithmException//w  w w .j a va 2s .  c o m
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String aesDecrypt(String encryptedB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String dataB64 = null;

    final byte[] encryptedBytes = Base64.decodeBase64(encryptedB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(AES_CIPHER);

    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretBytes, "AES"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    dataB64 = Base64.encodeBase64String(cipher.doFinal(encryptedBytes));
    return dataB64;
}

From source file:org.candlepin.common.config.EncryptedConfiguration.java

public void toDecrypt(String property) throws ConfigurationException {
    if (passphrase == null) {
        log.debug("Passphrase is null.  Skipping decrypt.");
        return;//from www.j a v  a  2 s  . c  o  m
    }

    if (!containsKey(property)) {
        log.debug("Can't decrypt missing property: {}", property);
        return;
    }

    String toDecrypt = getString(property);
    if (!toDecrypt.startsWith("$1$")) {
        // this is not an encrypted password, just return it
        log.debug("Value for {} is not an encrypted string", property);
        return;
    }

    // remove the magic string
    toDecrypt = toDecrypt.substring(3);

    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // NOTE: we are creating a 64byte digest here,
        // but only the first 16 bytes are used as the iv
        String ivString = passphrase + passphrase;
        String iv = DigestUtils.sha256Hex(ivString);
        String passphraseDigest = DigestUtils.sha256Hex(passphrase);

        // FIXME: katello-password creates a 64 byte key, but somewhere
        // it gets truncated to 32 bytes, so we have to do that here.
        SecretKeySpec spec = new SecretKeySpec(Arrays.copyOfRange(passphraseDigest.getBytes(), 0, 32), "AES");

        cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(iv.getBytes(), 0, 16));

        // NOTE: the encrypted password is stored hex base64
        byte[] b64bytes = Base64.decodeBase64(toDecrypt);
        String decrypted = new String(cipher.doFinal(b64bytes));
        setProperty(property, decrypted);
    } catch (Exception e) {
        log.error("Failure trying to decrypt value of {}", property, e);
        throw new ConfigurationException(e);
    }

}

From source file:org.candlepin.config.EncryptedValueConfigurationParser.java

public String decryptValue(String toDecrypt, String passphrase) {
    log.info("decrypt called");
    if (!toDecrypt.startsWith("$1$")) {
        // this is not an ecnrypted password, just return it
        log.debug("this is not an encrypted string");
        return toDecrypt;
    }//  ww  w. ja  va  2s .  c o  m

    // remove the magic string
    toDecrypt = toDecrypt.substring(3);

    try {
        Cipher cipher;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // NOTE: we are creating a 64byte digest here,
        // but only the first 16 bytes are used as the iv
        String ivString = passphrase + passphrase;
        String iv = DigestUtils.sha256Hex(ivString);
        String passphraseDigest = DigestUtils.sha256Hex(passphrase);

        // FIXME: katello-password creates a 64 byte key, but somewhere
        // it gets truncated to 32 bytes, so we have to do that here.
        SecretKeySpec spec = new SecretKeySpec(Arrays.copyOfRange(passphraseDigest.getBytes(), 0, 32), "AES");

        cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(iv.getBytes(), 0, 16));

        log.info("gh10");
        // NOTE: the encrypted password is stored hex base64
        byte[] b64bytes = Base64.decodeBase64(toDecrypt);
        String plaintext = new String(cipher.doFinal(b64bytes));

        return plaintext;
    } catch (Exception e) {
        log.info("Failure trying to decrypt" + toDecrypt, e);
        throw new RuntimeException(e);
    }
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * TripleDES (EDE) Encryption CBC Mode with PKCS5 padding
 *
 * @param dataB64 Data to encrypt Base64 encoded.
 * @param secretB64 Encryption secret Base64 encoded. Secret must be at least 24 bytes. Only the first 24 bytes will be used.
 * @param ivB64 Initialization Vector Base64 encoded. Only first 8 bytes will be used.
 * @return Encrypted data Base64 encoded.
 * @throws NoSuchAlgorithmException/*w ww .  j a v a  2 s  . c om*/
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String tripleDesEncrypt(String dataB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String encryptedB64 = null;

    final byte[] dataBytes = Base64.decodeBase64(dataB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(DESEDE_CIPHER);

    DESedeKeySpec keySpec = new DESedeKeySpec(secretBytes);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keySpec.getKey(), "DESede"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes));
    return encryptedB64;
}