Example usage for javax.crypto Cipher ENCRYPT_MODE

List of usage examples for javax.crypto Cipher ENCRYPT_MODE

Introduction

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

Prototype

int ENCRYPT_MODE

To view the source code for javax.crypto Cipher ENCRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to encryption mode.

Usage

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

public CipherOutputStream encrypt(OutputStream out) 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.ENCRYPT_MODE, key, IV);
    return new CipherOutputStream(new Base64OutputStream(out), cipher);
}

From source file:com.searchcode.app.util.AESEncryptor.java

public byte[] encrypt(byte[] plainText) throws Exception {
    SecretKeySpec secretKey = new SecretKeySpec(this.key, this.ALGORITHM);
    Cipher cipher = Cipher.getInstance(this.ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    return cipher.doFinal(plainText);
}

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 . ja va2 s .  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:com.hp.application.automation.tools.EncryptionUtils.java

public static String Encrypt(String text, String key) throws Exception {

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] keyBytes = new byte[16];
    byte[] b = key.getBytes("UTF-8");
    int len = b.length;
    if (len > keyBytes.length)
        len = keyBytes.length;/*from   www  . ja v a 2s.  c o m*/
    System.arraycopy(b, 0, keyBytes, 0, len);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    byte[] results = cipher.doFinal(text.getBytes("UTF-8"));

    return Base64.encodeBase64String(results);
}

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 av  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);
    //        cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(message);

    log("cipherText", cipherText);

    return cipherText;
}

From source file:com.earldouglas.xjdl.io.LicenseCreator.java

public String encryptLicense(License license, String key)
        throws Exception, NoSuchAlgorithmException, NoSuchPaddingException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(license);
    objectOutputStream.close();/*from   w w w.j a v a  2 s .c  o m*/
    byte[] serializedLicense = byteArrayOutputStream.toByteArray();

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] encryptedLicense = cipher.doFinal(serializedLicense);
    String encodedLicense = Base64.encodeBase64String(encryptedLicense);
    encodedLicense = encodedLicense.replaceAll("\n", "");
    encodedLicense = encodedLicense.replaceAll("\r", "");
    return encodedLicense;
}

From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java

public CryptographyServiceImplBlowfish()
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);/*from  w  ww  .j a  va  2 s. c o  m*/
    key = keyGenerator.generateKey();
    cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
    cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}

From source file:com.example.license.DESUtil.java

/**
 * ?//from w  w  w.  j  a  va 2 s  . co m
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String encrypt(String data, String key) throws Exception {
    Key deskey = keyGenerator(key);
    // Cipher??
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

From source file:com.paypal.utilities.AESDecrypt.java

public AESDecrypt(String encryptedString) throws Exception {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
    SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

    encrypt = Base64.decodeBase64(encryptedString.getBytes());

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] iv = extractIV();
    dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}

From source file:com.bconomy.autobit.Encryption.java

public static byte[] encrypt(byte[] cleartext, byte[] key) {
    if (keySpec == null)
        keySpec = new SecretKeySpec(key, "AES");
    Cipher aes;/*from  w w w  . j av  a 2  s .c  om*/
    try {
        aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes.init(Cipher.ENCRYPT_MODE, keySpec);
        return aes.doFinal(cleartext);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        ex.printStackTrace();
    }
    return new byte[0];
}