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.esri.geoevent.datastore.Crypto.java

static public String doEncrypt(String stringToEncrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, encryptionKey);
    byte[] encVal = c.doFinal(stringToEncrypt.getBytes());
    String encodedEncryptedString = new String(Base64.encodeBase64(encVal));
    return encodedEncryptedString;
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a string.//from   w  w w .  ja  v a  2 s.c  om
 *
 * @param c The string to encrypt.
 * @return The encrypted string in HEX.
 */
public static String encrypt(String c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c.getBytes());
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt string", e);
        return null;
    }
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea cifrar// ww  w  . j  av a2s. c o m
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param cleartext el texto sin cifrar a encriptar
 * @return el texto cifrado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException
 */
public static String encrypt(String key, String iv, 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.sshutils.utils.CryptHelper.java

public static String encrypt(String strToEncrypt) {
    try {//from  w  w  w. j  a  va 2s . co m

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * /*from ww w . j av  a 2  s . c o m*/
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String encode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte e[] = cipher.doFinal(data.getBytes("UTF-8"));
        return replaceChar(Base64.encodeBase64String(e));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (UnsupportedEncodingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);/*w  ww .j a  v  a2 s. com*/

    return encryptedToken;
}

From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java

/**
 * Encrypts a given string using AES./*  w w w .j  a v a 2s . co m*/
 * 
 * @param value
 *            // String to encrypt.
 * @return // Returns encrypted string.
 */
public static String encrypt(String value) {
    try {
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

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

        byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8"));
        System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Logi.GSeries.Libraries.Encryption.java

public static String encrypt(String decryptedString, String password) {
    try {/*from  ww  w .  ja v  a2  s.c om*/
        // build the initialization vector (randomly).
        SecureRandom random = new SecureRandom();
        byte initialVector[] = new byte[16];
        //generate random 16 byte IV AES is always 16bytes
        random.nextBytes(initialVector);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
        byte[] encrypted = cipher.doFinal(decryptedString.getBytes());
        byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length];
        System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length);
        System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length);
        return Base64.encodeBase64String(encryptedWithIV);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:com.amazonaws.tvm.AESEncryption.java

public static byte[] encrypt(String clearText, 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.ENCRYPT_MODE, getKey(key), params);
    return cipher.doFinal(clearText.getBytes());
}

From source file:encrypt.algorithms.AESCBC.java

public String encrypt(String key1, String key2, String value) {
    try {//from   ww  w  .  j ava  2  s.  c  o m
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        // System.out.println("encrypted string:"
        //       + Base64.encodeBase64String(encrypted));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}