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.qubole.quark.catalog.db.encryption.AESEncrypt.java

public String convertToDatabaseColumn(String phrase) throws SQLException {
    try {//from   w  ww.  j av a2s .c  o m
        Cipher encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(Hex.encodeHex(encryptCipher.doFinal(phrase.getBytes("UTF-8"))));
    } catch (Exception e) {
        throw new SQLException(e);
    }
}

From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java

@Override
public byte[] encrypt(byte[] data) {
    try {//from  ww w .  j ava  2s  . c  o  m
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key, getIV());
        byte[] encrypted = cipher.doFinal(data);
        return encrypted;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException
            | InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new CryptException(e);
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

From source file:com.rdonasco.security.utils.EncryptionUtil.java

public static String encryptWithPassword(String stringToEncrypt, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] encryptedBytes = pbeCipher.doFinal(stringToEncrypt.getBytes());
    return Base64.encodeBase64String(encryptedBytes);
}

From source file:com.centurylink.mdw.util.CryptUtil.java

public static String encrypt(String input) throws GeneralSecurityException {
    String padded = pad(input, MAX_LENGTH);
    cipher.init(Cipher.ENCRYPT_MODE, key64);
    byte[] encrypted = cipher.doFinal(padded.getBytes());
    return encodeBase64(encrypted);
}

From source file:de.thischwa.pmcms.tool.DESCryptor.java

public String encrypt(String plainTxt) throws CryptorException {
    if (plainTxt == null || plainTxt.trim().length() == 0)
        return null;
    if (key == null)
        key = buildKey();/*  w  w  w  . jav a  2  s . com*/
    try {
        byte[] cleartext = plainTxt.getBytes(encoding);
        Cipher cipher = Cipher.getInstance(algorithm); // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, key);
        String encrypedPwd = Base64.encodeBase64String(cipher.doFinal(cleartext));
        return encrypedPwd;
    } catch (Exception e) {
        throw new CryptorException(e);
    }
}

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  va  2s .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);
    byte[] cipherText = cipher.doFinal(message);

    log("cipherText", cipherText);

    return cipherText;
}

From source file:cn.lynx.emi.license.GenerateLicense.java

private static final String encrypt(String key, String data) {
    byte[] corekey = Base64.decodeBase64(key);

    PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey);

    try {//from  w w w  .ja va2s  .c om
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key privateKey = keyFactory.generatePrivate(pkspec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
        System.out.println("after encrypt, len=" + encData.length);
        return Base64.encodeBase64String(encData);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.salesmanager.core.util.EncryptionUtil.java

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

    // value = StringUtils.rightPad(value, 16,"*");
    // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    byte[] inpbytes = value.getBytes();
    byte[] encrypted = cipher.doFinal(inpbytes);
    return new String(bytesToHex(encrypted));

}

From source file:in.mtap.iincube.mongoser.codec.crypto.Psyfer.java

public static Psyfer getInstance(String secretKey) throws NoSuchAlgorithmException,
        UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException {
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    byte[] key = digest.digest(secretKey.getBytes("UTF-8"));
    key = Arrays.copyOf(key, 16);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    Cipher eCipher = Cipher.getInstance("AES");
    Cipher deCipher = Cipher.getInstance("AES");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    return new Psyfer(eCipher, deCipher);
}