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:com.haulmont.cuba.web.test.PasswordEncryptionTest.java

protected String decryptPassword(String password) {
    SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes());
    String result;/*from w  w w  .  java 2s.co m*/
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
        result = new String(cipher.doFinal(Hex.decodeHex(password.toCharArray())));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.dasol.util.AES256Util.java

public String aesEncode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
    String enStr = new String(Base64.encodeBase64(encrypted));

    return enStr;
}

From source file:org.craftercms.security.authentication.impl.CipheredAuthenticationCookie.java

/**
 * Encrypts the cookie values using the AES cipher and returns it as a Base 64 string.
 *///from w  ww  .j a  va 2 s  .c  o  m
protected String encrypt(String rawValue) throws CrafterSecurityException {
    try {
        byte[] iv = generateIv();

        Cipher cipher = Cipher.getInstance(CipheredAuthenticationCookieFactory.CIPHER_TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(iv));

        byte[] encryptedValue = cipher.doFinal(rawValue.getBytes("UTF-8"));

        // The IV can be sent in clear text, no security issue on that
        return Base64.encodeBase64String(encryptedValue) + COOKIE_SEP + Base64.encodeBase64String(iv);
    } catch (Exception e) {
        throw new CrafterSecurityException("Error while trying to encrypt cookie value " + rawValue, e);
    }
}

From source file:com.scorpio4.util.io.IOStreamCrypto.java

public CipherInputStream decrypt(InputStream in) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException {
    final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec);
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance(cipherTransformation);
    cipher.init(Cipher.DECRYPT_MODE, key, IV);
    return new CipherInputStream(new Base64InputStream(in), cipher);
}

From source file:org.sonar.runner.impl.SaltedAesCryptor.java

public SaltedAesCryptor(String encKey) throws Exception {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.update(encKey.getBytes("UTF-8"));
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

    this.secretKeySpec = new SecretKeySpec(keyBytes, "AES");
    byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    algorithmParameterSpec = new IvParameterSpec(iv);
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Decrypt the encrypted value with provided key
 *//*from w  w w . ja  va2s.  co m*/
public static String decrypt(String key, String encryptedValue) {
    String decryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedValue)));

    } catch (Exception ex) {
        System.out.println("Caught exception while decrypting string");
        ex.printStackTrace();
    }

    return decryptedString;
}

From source file:org.nuclos.common.CryptUtil.java

public static String decryptAES(byte[] bytes, byte[] key) {
    if (key.length != 16)
        throw new IllegalArgumentException(WRONG_PASSLEN_MESSAGE);

    try {//from   w ww .jav  a2 s  .c o  m
        IvParameterSpec ivSpec = new IvParameterSpec(key);
        SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
        Cipher cipher = Cipher.getInstance(AES_CBC_PAD);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
        return new String(cipher.doFinal(bytes), "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException("decyption failure", e);
    }
}

From source file:MainClass.java

public static IvParameterSpec createCtrIvForAES(int messageNumber, SecureRandom random) {
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);//from   w  w  w  .  j av a 2 s.  com
    ivBytes[0] = (byte) (messageNumber >> 24);
    ivBytes[1] = (byte) (messageNumber >> 16);
    ivBytes[2] = (byte) (messageNumber >> 8);
    ivBytes[3] = (byte) (messageNumber >> 0);
    for (int i = 0; i != 7; i++) {
        ivBytes[8 + i] = 0;
    }
    ivBytes[15] = 1;
    return new IvParameterSpec(ivBytes);
}

From source file:com.github.sshw.crypt.EncryptionBean.java

public String encrypt(String message, String password) throws Exception {
    Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
    Key key = keyFromPassword(password);
    IvParameterSpec ivb = new IvParameterSpec(key.getEncoded());
    encrypt.init(Cipher.ENCRYPT_MODE, key, ivb);
    byte[] cb = encrypt.doFinal(message.getBytes());
    String c = Base64.encodeBase64URLSafeString(cb);
    return c;/*  w w w .  j  a  v a2  s .c o  m*/
}

From source file:Main.java

static byte[] decryptJWE(String jwe, Key privRsaKey) {
    // Log.d("","decryptJWE");

    try {//  w  w w .j  av a  2s. c om
        // split jwe string
        StringTokenizer tokens = new StringTokenizer(jwe, ".");
        int count = tokens.countTokens();
        // Log.d("","parts.length: "+count);

        if (count != 5)
            return null;

        String jweProtectedHeader64 = tokens.nextToken();
        String jweEncrypted64 = tokens.nextToken();
        String jweInitVector64 = tokens.nextToken();
        String cryptedBytes64 = tokens.nextToken();
        String auth_tag64 = tokens.nextToken();

        // decrypt cek using private rsa key
        byte[] cek = decryptRsaB64(jweEncrypted64, privRsaKey);

        // check cek result byte array
        if (cek == null || cek.length == 0 || (cek.length % 2) != 0)
            return null;

        int keySize = cek.length / 2;
        Log.d("", "Decryption AES: " + keySize * 8);

        // build aes_key and hmac_key
        byte aes_key[] = new byte[keySize];
        byte hmac_key[] = new byte[keySize];

        System.arraycopy(cek, 0, hmac_key, 0, keySize);
        System.arraycopy(cek, keySize, aes_key, 0, keySize);

        // decode initialization vector
        byte[] iv_key = decodeB64(jweInitVector64);

        Log.d("", "hmac_key: " + bytesToHex(hmac_key));
        Log.d("", "aes_key:  " + bytesToHex(aes_key));
        Log.d("", "iv_key:   " + bytesToHex(iv_key));

        // decrypt content using aes_key and iv_key
        byte[] cryptedBytes = decodeB64(cryptedBytes64);
        Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding", "SC");
        decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key));
        byte[] decryptedBytes = decrypt.doFinal(cryptedBytes);

        Log.d("", "decryptedBytes:");
        Log.d("", bytesToHex(decryptedBytes));

        // validation verification
        byte[] aad = jweProtectedHeader64.getBytes();
        long al = aad.length * 8;

        // concatenate aad, iv_key, cryptedBytes and al 
        byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8];
        int offset = 0;
        System.arraycopy(aad, offset, hmacData, 0, aad.length);
        offset += aad.length;
        System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length);
        offset += iv_key.length;
        System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length);
        offset += cryptedBytes.length;
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putLong(al);
        System.arraycopy(buffer.array(), 0, hmacData, offset, 8);

        // compute hmac
        Mac hmac = Mac.getInstance("HmacSHA256", "SC");
        hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256"));
        byte[] hmacValue = hmac.doFinal(hmacData);

        // pick authentication tag
        byte[] authTag = Arrays.copyOf(hmacValue, 16);

        // validate authentication tag
        byte[] authTagRead = decodeB64(auth_tag64);
        for (int i = 0; i < 16; i++) {
            if (authTag[i] != authTagRead[i]) {
                Log.d("", "validation failed");
                return decryptedBytes;
            }
        }

        Log.d("", "validation success");

        // validation success
        return decryptedBytes;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}