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:aes_encryption.AES_Encryption.java

public static String encrypt(String strToEncrypt) {
    try {// www .j  a va2s  .  com
        Cipher cipher = Cipher.getInstance("AES_Encryption/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

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

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

    if (value == null || value.equals(""))
        return "";

    // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK
    // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    byte[] outText;
    outText = cipher.doFinal(hexToBytes(value));
    return new String(outText);

}

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

static public String doDecrypt(String stringToDecrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, encryptionKey);
    byte[] decodedValue = Base64.decodeBase64(stringToDecrypt.getBytes());
    byte[] decryptedValue = c.doFinal(decodedValue);
    String decryptedString = new String(decryptedValue);
    return decryptedString;
}

From source file:edu.tufts.vue.util.Encryption.java

public static String decrypt(String encrypted) {
    try {// w ww .j  av a 2  s .c  om

        if (cipher == null)
            cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        byte[] encryptedBytes = hex.decode(encrypted.getBytes());
        byte[] recoveredBytes = cipher.doFinal(encryptedBytes);
        String recovered = new String(recoveredBytes);
        return recovered;
    } catch (Exception ex) {
        ex.printStackTrace();
        return encrypted;
    }
}

From source file:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

/**
 *Mtodo que permite la desencriptacin de la contrasea
 * @param encrypted/*w  w  w . j  av  a 2 s  . co m*/
 * @return
 * @throws Exception
 */
public static String decrypt(String encrypted) throws Exception {
    String key = "02AE31B79CCCB2A3"; //llave
    String iv = "0123456789ABCDEF";
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java

public static String decrypt(String key, String data) {
    byte[] decryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {//from  w w  w.  j  av a2  s.  c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        //decryptedData = cipher.doFinal(Base64.decode(data));

        decryptedData = cipher.doFinal(Base64.decodeBase64(data));

        //return new String(decryptedData);

        return new String(decryptedData);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String desDecode(String texto, String chave) {
    Cipher dcipher;//from   w ww  .j a v  a2  s.  c  om
    SecretKey key;
    String decod = null;

    try {
        key = new SecretKeySpec(chave.getBytes(), 0, 8, "DES");
        dcipher = Cipher.getInstance("DES");
        dcipher.init(Cipher.DECRYPT_MODE, key);
        byte[] dec = Hex.decodeHex(texto.toCharArray());
        byte[] utf8 = dcipher.doFinal(dec);
        decod = new String(utf8, "UTF8");
    } catch (Exception e) {
        log.error(e);
    }

    return decod;
}

From source file:model.Encryption.java

/**
 * @param plainText/*w w  w.  j a v  a2 s  .co  m*/
 * @return
 * @throws Exception
 */
public String encrypt(String plainText) throws Exception {
    if (secret == null)
        generateSecretKey();

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));

    return new Base64().encodeBase64String(encryptedTextBytes);
}

From source file:BD.Encriptador.java

public static String Desencriptar(String textoEncriptado) throws Exception {

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

    try {/*from  w w  w.  jav  a  2s . c  o  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.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) {
    }
    return base64EncryptedString;
}