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) 

Source Link

Document

Creates an IvParameterSpec object using the bytes in iv as the IV.

Usage

From source file:jp.primecloud.auto.common.component.PasswordEncryptor.java

/**
 *
 * ?//from w w w  . ja  v  a  2s .  co  m
 *
 */
private void initialize() {
    ivParameterSpec = new IvParameterSpec(IV);

    try {
        secureRandom = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    // ?
    secureRandom.setSeed(System.currentTimeMillis());
    // ??
    characterSet = createCharacterSet();

    try {
        chipher = Cipher.getInstance(CIPHER_PARAM);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.charandeepmatta.oracle.sqldeveloper.DecodePasswordHash.java

private byte[] decryptPassword(final byte[] result) throws GeneralSecurityException {
    byte constant = result[0];
    if (constant != (byte) 5) {
        throw new IllegalArgumentException();
    }/*from   w w w. j  ava2 s  . co  m*/
    byte[] secretKey = new byte[8];
    System.arraycopy(result, 1, secretKey, 0, 8);
    byte[] encryptedPassword = new byte[result.length - 9];
    System.arraycopy(result, 9, encryptedPassword, 0, encryptedPassword.length);
    byte[] iv = new byte[8];
    for (int i = 0; i < iv.length; i++) {
        iv[i] = 0;
    }
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "DES"), new IvParameterSpec(iv));
    return cipher.doFinal(encryptedPassword);
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) {
    try {/*ww  w  . j av  a2s . c  om*/
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
        return cipher.doFinal(cipherBytes);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to decrypt.", e);
    }
}

From source file:com.google.u2f.gaedemo.impl.DataStoreImpl.java

@Override
public String storeSessionData(EnrollSessionData sessionData) {

    SecretKey key = new SecretKeySpec(SecretKeys.get().sessionEncryptionKey(), "AES");
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);//from ww  w. j  av a 2  s.c  o  m
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }

    SealedObject sealed;
    try {
        sealed = new SealedObject(sessionData, cipher);
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeException(e);
    }

    ByteArrayOutputStream out;
    try {
        out = new ByteArrayOutputStream();
        ObjectOutputStream outer = new ObjectOutputStream(out);

        outer.writeObject(sealed);
        outer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return Base64.encodeBase64URLSafeString(out.toByteArray());
}

From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String decrypt(String secretkey, String iv, String toDecrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return new String(cipher.doFinal(Base64.decodeBase64(toDecrypt)));
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * DES/*from   w  w w .j  a  v a2s.  com*/
 * 
 * @param data
 *            
 * @param key
 *            ???8?
 * @return ?
 */
public static String decode(String key, String data) {
    if (data == null) {
        return null;
    }
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key??8?
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        return new String(cipher.doFinal(hex2byte(data.getBytes())));
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new SecurityException(ex);
    }
}

From source file:CipherSocket.java

public InputStream getInputStream() throws IOException {
    InputStream is = delegate == null ? super.getInputStream() : delegate.getInputStream();
    Cipher cipher = null;/*from   ww w. j a  va2 s .co  m*/
    try {
        cipher = Cipher.getInstance(algorithm);
        int size = cipher.getBlockSize();
        byte[] tmp = new byte[size];
        Arrays.fill(tmp, (byte) 15);
        IvParameterSpec iv = new IvParameterSpec(tmp);
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Failed to init cipher: " + e.getMessage());
    }
    CipherInputStream cis = new CipherInputStream(is, cipher);
    return cis;
}

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

public CryptoAES(String transformation, Properties properties, byte[] inKey, byte[] outKey, byte[] inIv,
        byte[] outIv) throws IOException {
    checkTransformation(transformation);
    // encryptor/*from   w  w  w. ja  va 2s  .  co m*/
    encryptor = Utils.getCipherInstance(transformation, properties);
    try {
        SecretKeySpec outKEYSpec = new SecretKeySpec(outKey, "AES");
        IvParameterSpec outIVSpec = new IvParameterSpec(outIv);
        encryptor.init(Cipher.ENCRYPT_MODE, outKEYSpec, outIVSpec);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new IOException("Failed to initialize encryptor", e);
    }

    // decryptor
    decryptor = Utils.getCipherInstance(transformation, properties);
    try {
        SecretKeySpec inKEYSpec = new SecretKeySpec(inKey, "AES");
        IvParameterSpec inIVSpec = new IvParameterSpec(inIv);
        decryptor.init(Cipher.DECRYPT_MODE, inKEYSpec, inIVSpec);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new IOException("Failed to initialize decryptor", e);
    }

    integrity = new Integrity(outKey, inKey);
}

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

@Override
public InputStream createDecryptionStream(InputStream in) {
    try {/* ww  w  .ja va  2s  .  c o  m*/
        return new CryptoInputStream(cipherMode, properties, in, key, new IvParameterSpec(iv));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:CipherProvider.java

/**
 * Create cipher for encrypt or decrypt backup content
 *
 * @param passwd passwd for encryption//from w ww.  ja  v  a  2  s  .c  o m
 * @param mode   encrypt/decrypt mode
 *
 * @return instance of cipher
 */
private static Cipher getCipher(final String passwd, final int mode) {
    /* Derive the key, given password and salt. */
    Cipher cipher = null;
    try {
        SecretKeyFactory factory = null;
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        String salt = "slNadZlato#%^^&(&(5?@#5166?1561?#%^^*^&54431"; // only pseudorandom salt
        KeySpec spec = new PBEKeySpec(passwd.toCharArray(), salt.getBytes(), 65536, 128);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), CIPHER_TYPE);

        // initialization vector
        byte[] iv = Arrays.copyOfRange(DigestUtils.md5(passwd), 0, 16);
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

        // Cipher for encryption
        cipher = Cipher.getInstance(CIPHER_TYPE + "/CBC/PKCS5Padding");
        cipher.init(mode, secret, paramSpec);
    } catch (Exception e) {
        e.printStackTrace(); //Todo implementovat
    }
    return cipher;
}