Example usage for javax.crypto Cipher init

List of usage examples for javax.crypto Cipher init

Introduction

In this page you can find the example usage for javax.crypto Cipher init.

Prototype

public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate and a source of randomness.

Usage

From source file:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

/**
 *Mtodo que permite la desencriptacin de la contrasea
 * @param encrypted/*from www  . j  a  va 2  s. c  o m*/
 * @return
 * @throws Exception
 */
public static String decrypt(String encrypted) throws Exception {
    String key = "02AE31B79CCCB2A3"; //llave
    String iv = "0123456789ABCDEF";
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:com.aqnote.shared.encrypt.symmetric.Blowfish.java

/**
 * ?//from  w ww . ja va 2  s .  c om
 * 
 * @param cipher
 *            Cipher
 * @param opmode
 *            operation mode (ENCRYPT_MODE?DECRYPT_MODE?WRAP_MODE 
 *            UNWRAP_MODE)
 * @param key
 *            Key
 * @param iv
 *            AlgorithmParameterSpec
 */
private static void initCipher(Cipher cipher, int opmode, Key key, AlgorithmParameterSpec iv) {
    try {
        cipher.init(opmode, key, iv);
    } catch (InvalidKeyException e) {
    } catch (InvalidAlgorithmParameterException e) {
    }
}

From source file:com.fujitsu.dc.common.auth.token.LocalToken.java

/**
 * ??.//from   w  w  w. j ava  2 s. c om
 * @param in 
 * @param ivBytes 
 * @return ???
 */
public static String encode(final String in, final byte[] ivBytes) {
    // IV??CELL?URL??????
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(ivBytes));
        byte[] cipherBytes = cipher.doFinal(in.getBytes(CharEncoding.UTF_8));
        return DcCoreUtils.encodeBase64Url(cipherBytes);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:io.personium.common.auth.token.LocalToken.java

/**
 * ??.// w  ww. jav a 2  s .c om
 * @param in 
 * @param ivBytes 
 * @return ???
 */
public static String encode(final String in, final byte[] ivBytes) {
    // IV??CELL?URL??????
    Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(ivBytes));
        byte[] cipherBytes = cipher.doFinal(in.getBytes(CharEncoding.UTF_8));
        return PersoniumCoreUtils.encodeBase64Url(cipherBytes);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

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

private static byte[] encrypt(String clearText, String key, byte[] iv) {
    try {//from  ww  w . j  a  v a2  s . c o m
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
        return cipher.doFinal(clearText.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt.", e);
    }
}

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 o  m
        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.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ???/*from ww w  . jav a 2 s.c  o m*/
 * <dl>
 * <dt>?
 * <dd>AES/CBC/PKCS5Padding???????
 * </dl>
 * @param key ?
 * @param iv ?
 * @param input 
 * @return ?
 */
public static byte[] encrypt(final Key key, final IvParameterSpec iv, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance(ALGO_CIPHER_BIG);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ???/*w  w w  .  j av  a  2 s .co m*/
 * <dl>
 * <dt>?
 * <dd>AES/CBC/PKCS5Padding???????
 * </dl>
 * @param key ?
 * @param iv ?
 * @param input 
 * @return ?
 */
public static byte[] decrypt(final Key key, final IvParameterSpec iv, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance(ALGO_CIPHER_BIG);
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * /*from   w ww.  j a  v a  2s  . c om*/
 * This method is used for decrypt by using Algorithm - AES/CBC/PKCS5Padding
 * 
 * @param String
 * @return String
 * @throws Exception
 */
@SuppressWarnings("static-access")
public static String decryptAESPBKDF2(String encryptedText) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO);
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {

        LOGGER.error("error " + e.getMessage());
    } catch (BadPaddingException e) {
        LOGGER.error("error " + e.getMessage());
    }

    return new String(decryptedTextBytes);
}

From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java

public static String decrypt(byte[] key, String ciphertext)
        throws NoSuchAlgorithmException, IOException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    if (!ciphertext.startsWith(FWKNOP_ENCRYPTION_HEADER)) {
        ciphertext = FWKNOP_ENCRYPTION_HEADER.concat(ciphertext);
    }//from w  ww  . j a v a2  s  . c  o m
    // we need to remove Salted__ from the salt_and_ciphertext therefore -> SALT_LEN +/- 8

    byte[] salt_and_ciphertext = Base64.decodeBase64(ciphertext);
    byte[] salt = new byte[SALT_LEN];
    byte[] cipher = new byte[salt_and_ciphertext.length - SALT_LEN - 8];
    System.arraycopy(salt_and_ciphertext, 8, salt, 0, SALT_LEN);
    System.arraycopy(salt_and_ciphertext, 8 + SALT_LEN, cipher, 0, cipher.length);
    byte[][] key_and_iv = deriveKeyAndIV(salt, key);

    SecretKeySpec enc_key;
    enc_key = new SecretKeySpec(key_and_iv[0], "AES");
    Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec(key_and_iv[1]);
    aes.init(Cipher.DECRYPT_MODE, enc_key, iv);
    byte[] plain = aes.doFinal(cipher);

    return new String(plain, "UTF-8");
}