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:Main.java

/**
 * More flexible AES encrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector// ww  w .  j  a  v a2s. c  om
 * @param message in bytes (assumed it's already been decoded)
 * @return Encrypted cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    byte[] cipherText = cipher.doFinal(message);

    log("cipherText", cipherText);

    return cipherText;
}

From source file:Main.java

/**
 * Initializes the Cipher for use.//from  www.  j  a  va  2s .c om
 */
public static void initCipher(Cipher cipher, int mode, SecretKey secretKey,
        AlgorithmParameterSpec parameterSpec) {
    try {
        if (parameterSpec != null) {
            cipher.init(mode, secretKey, parameterSpec);
        } else {
            cipher.init(mode, secretKey);
        }
    } catch (InvalidKeyException e) {
        throw new IllegalArgumentException("Unable to initialize due to invalid secret key", e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new IllegalStateException("Unable to initialize due to invalid decryption parameter spec", e);
    }
}

From source file:com.cl.roadshow.crypto.AESCtr.java

/**
 * Private encryption method.//  w  w w . jav  a 2 s.  com
 * 
 * @param keystring
 * @param message
 * @param bits
 * @return bytearray containing encrypted message
 * @throws Exception
 */
private static byte[] encrypt(String keystring, String message, int bits) throws Exception {
    byte[] encValue = null;
    SecureRandom random = new SecureRandom();
    byte[] nonceBytes = new byte[8];
    random.nextBytes(nonceBytes);
    IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16));

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key, nonce);
    byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8"));
    encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length);
    for (int i = 0; i < ciphertextWithoutNonce.length; i++) {
        encValue[i + 8] = ciphertextWithoutNonce[i];
    }

    return encValue;
}

From source file:Main.java

/**
 * More flexible AES decrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector/*from w  w  w  .j  a  va  2 s .com*/
 * @param decodedCipherText in bytes (assumed it's already been decoded)
 * @return Decrypted message cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

From source file:com.amazonaws.cognito.sync.demo.client.server.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    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);
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desEncrypt(String pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//w ww .  j  av a  2  s.c  o  m
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd.getBytes());
}

From source file:com.wms.studio.security.utils.Digests.java

public static byte[] desDecrypt(byte[] pwd, byte[] key) throws Exception {
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??//  w  ww. j av  a 2s  . com
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, random);
    return cipher.doFinal(pwd);
}

From source file:Main.java

/**
 * More flexible AES encrypt that doesn't encode
 *
 * @param key     AES key typically 128, 192 or 256 bit
 * @param iv      Initiation Vector/*w  w w . j a va 2  s  .  c o  m*/
 * @param message in bytes (assumed it's already been decoded)
 * @return Encrypted cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    //        cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(message);

    log("cipherText", cipherText);

    return cipherText;
}

From source file:clases.Seguridad.java

public static String encriptar(String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(CI);
    SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), ALG);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:com.avbravo.avbravoutils.crypto.Encriptador.java

public static String encrypt(String key, String cleartext) throws Exception {

    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}