Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

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

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:io.cloudslang.content.database.utils.TripleDES.java

static byte[] encryptString(final byte[] text) throws Exception {
    final SecretKey key = new SecretKeySpec(
            TripleDES.md5Hash("NpWsCaJQj1LaXt)YYnzr\\%zP~RydB*3YGutr*@|A\\ckG3\\Yf%k"), ENCRYPTION_KEYSPECTYPE);
    final Cipher cipher = Cipher.getInstance(ENCRYPTION_MODE);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(text);
}

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

/**
 * Mtodo para encriptar las contraseas//ww  w  . j a  v a2s  .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;
}

From source file:info.bonjean.beluga.util.CryptoUtil.java

private static Cipher getCipher(String strKey, boolean encoder) {
    Cipher cipher = null;//from  w  w w .  j  ava 2 s . c  o m
    if (encoder)
        cipher = encryptCiphers.get(strKey);
    else
        cipher = decryptCiphers.get(strKey);

    if (cipher != null)
        return cipher;

    try {
        SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "Blowfish");
        cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        cipher.init(encoder ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key);

        if (encoder)
            encryptCiphers.put(strKey, cipher);
        else
            decryptCiphers.put(strKey, cipher);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return cipher;
}

From source file:com.os.util.PasswordDecoderEncoder.java

public static String encrypt(String plainPassword) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    key = convertHexToBytes(keyst);//from www .  j  av  a  2 s  .co m
    final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    final String encryptedString = Base64.encodeBase64String(cipher.doFinal(plainPassword.getBytes("UTF8")));
    System.out.println(encryptedString);
    String passwordEncrypted = encryptedString.trim();

    return passwordEncrypted;

}

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;// www  . j  ava2 s .  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:com.web.mavenproject6.utility.EncryptionUtil.java

public byte[] encrypt(String input) throws GeneralSecurityException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(input.getBytes());
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Given AES encryption algorithm, decrypts the string value.
 * @param encryptedData/*from   ww w  .j ava2  s  .com*/
 * @return decrypted String
 */
public static String decrypt(String encryptedData) {
    Key key = generateKey();
    try {
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        Base64 dec = new Base64();
        byte[] decodedValue = dec.decode(encryptedData.getBytes());
        byte[] decValue = c.doFinal(decodedValue);

        return new String(decValue);
    } catch (Exception e) {
        return null;
    }
}

From source file:Clases.cCifrado.java

public String Encriptar(String texto) {

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

    try {/*from  ww w  . j a v  a2  s .c  o m*/

        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 (Exception ex) {

        return "Ha habido un problema enviando datos";
    }
    return base64EncryptedString;
}

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

public static String decryptLanToken(String lanTokenKey, String encryptedToken) throws Exception {

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

    bytes = Arrays.copyOf(bytes, 16);

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

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

    return new String(cipher.doFinal(Base64.decodeBase64(encryptedToken)), Charset.forName("UTF-8"));
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de desencriptar la contrasea ingresada por el usuario *
 *//*  w  w  w .java  2  s  .c om*/
public static String decrypt(String encryptValue) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";

    try {
        byte[] message = Base64.decodeBase64(encryptValue.getBytes("utf-8"));
        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 decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = decipher.doFinal(message);
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}