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:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 *
 * @param data the data to encrypt//w w w  . java  2  s  .  c om
 * @return the encrypted BASE64 data
 * @throws CryptoException on encryption error
 */
public static String crypt(final String data) throws CryptoException {
    if (data == null) {
        return null;
    }

    try {
        final Cipher cipher = MessageCryptoUtil.getCipher(Cipher.ENCRYPT_MODE);
        final byte[] encrypted = cipher.doFinal(data.getBytes(Charset.forName("UTF-8")));
        return Base64.encodeBase64String(encrypted);
    } catch (final Exception e) {
        throw new CryptoException("Encryption of data failed!", e);
    }
}

From source file:com.sv.udb.controlador.CtrlContras.java

public String encrypt(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.bytecode.util.Crypto.java

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);//from  w w  w.jav a  2 s.  c  om
    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:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Encrypt./*  ww  w .  j a v  a  2 s  .  co  m*/
 *
 * @param Data the data
 * @return the string
 * @throws Exception the exception
 */
public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:architecture.common.BlowFishTest.java

@Test
public void testBlowfish() throws Exception {
    String Key = "password";
    byte[] KeyData = Key.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, KS);

    // encrypt message
    String inputText = "MyTextToEncrypt";
    byte[] encrypted = cipher.doFinal(inputText.getBytes());
    String encryptedString = Hex.encodeHexString(encrypted);
    System.out.println(" : " + encryptedString);

    cipher.init(Cipher.DECRYPT_MODE, KS);
    byte[] decrypt = cipher.doFinal(Hex.decodeHex(encryptedString.toCharArray()));
    System.out.println("   : " + new String(decrypt));

}

From source file:gsn.http.ac.Protector.java

public static String encrypt(String value) throws Exception {
    logger.debug("Encrypt key");
    Key key = generateKey();//from w w  w  . j  a v a  2s  .c o  m
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);

    String valueToEnc = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        eValue = new sun.misc.BASE64Encoder().encode(encValue);
        //eValue = Base64.encodeBase64String(encValue);
    }
    return eValue;
}

From source file:io.zipi.common.util.AesEncrypter.java

/**
 * Encrypt.// w ww .  j a v a 2  s  .c om
 * @param plainText the plain text
 * @param secretKey the secret key
 * @return the string
 */
public static String encrypt(String secretKey, String plainText) {
    if (plainText == null) {
        return null;
    }
    byte[] encrypted;
    Cipher cipher;
    try {
        SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

        cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    try {
        encrypted = cipher.doFinal(plainText.getBytes());
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        throw new RuntimeException(e);
    }
    return "$CRYPT::" + new String(Base64.encodeBase64(encrypted)); //$NON-NLS-1$
}

From source file:io.apiman.common.util.AesEncrypter.java

/**
 * Encrypt.//from w  w w .  j a  va 2s.c om
 * @param plainText the plain text
 * @param secretKey the secret key
 * @return the string
 */
public static String encrypt(String secretKey, String plainText) {
    if (plainText == null) {
        return null;
    }
    byte[] encrypted;
    Cipher cipher;
    try {
        SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

        cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    try {
        encrypted = cipher.doFinal(plainText.getBytes());
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    }
    return "$CRYPT::" + new String(Base64.encodeBase64(encrypted)); //$NON-NLS-1$
}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String encryptString(String clearText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String encrypted;//from   w w w  .  j  a v  a2 s.c  om
    byte[] key = password.getBytes();
    // Check the length of the password
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid password length.");
    }
    byte[] value = clearText.getBytes();

    // Encrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] encryptedBytes = cipher.doFinal(value);

    // Encode the bytes in base64
    encrypted = Base64.encodeBase64String(encryptedBytes);
    return encrypted;
}

From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para encriptar las contraseas/*from   w w w .  ja va 2  s .  co  m*/
 * @param texto
 * @return 
 */
public static String encriptar(String texto) {

    String secretKey = "zorbazorbas"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}