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:com.ikon.util.SecureStore.java

/**
 * DES decoder/*from  ww w .ja v  a  2 s  .  c  o m*/
 */
public static byte[] desDecode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:enc_mods.aes.java

public String getDecryptedString(String str) {
    String decrypted = "";
    try {/*  ww w .  ja  v  a  2  s .c  o m*/
        cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretkey);
        decrypted = new String(cipher.doFinal(Base64.decodeBase64(str)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decrypted;
}

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

private static Cipher initCipher(int mode, String key, String initVector) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

    SecretKeySpec secretKeySpec = new SecretKeySpec(hexStringToByteArray(key), "AES");
    IvParameterSpec initVectorSpec = new IvParameterSpec(hexStringToByteArray(initVector));
    cipher.init(mode, secretKeySpec, initVectorSpec);

    return cipher;
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodec.java

/**
 * {@inheritDoc}//from   w  w w .  j  av  a2s .c  om
 * @see net.sf.hajdbc.codec.Codec#encode(java.lang.String)
 */
@Override
public String encode(String value) throws SQLException {
    try {
        Cipher cipher = Cipher.getInstance(this.key.getAlgorithm());

        cipher.init(Cipher.ENCRYPT_MODE, this.key);

        return new String(Base64.encodeBase64(cipher.doFinal(value.getBytes())));
    } catch (GeneralSecurityException e) {
        throw new SQLException(e);
    }
}

From source file:com.frame.Conf.Utilidades.java

/**
 * //from   ww  w .j a va  2 s  . co m
 * @param keypass Es la llave plublica para enctriptar el texto
 * @param texto Texto a encriptar
 * @return retorna el hash del texto encriptado
 * @throws Exception 
 */
public String Encriptar(String keypass, String texto) throws Exception {

    String secretKey = keypass; //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) {
        throw new Exception(ex.getMessage());
    }
    return base64EncryptedString;
}

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

private static final String _decrypt(String data) {
    byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY);
    byte[] rawData = Base64.decodeBase64(data);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey);
    try {//from   ww  w  . jav  a 2  s.  c o  m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return new String(cipher.doFinal(rawData), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

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

public static String decrypt(String encryptedString, String password) {
    try {/*  w w w.ja va 2  s.  c  o m*/
        byte[] encryptedWithIV = Base64.decodeBase64(encryptedString);
        byte initialVector[] = new byte[16];
        byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length];
        System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length);
        System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
        byte[] original = cipher.doFinal(encrypted);
        return new String(original);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:Conexion.newClass.java

public String Decode(String textoEncriptado) throws Exception {

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

    try {/*w w  w . j  a va  2 s. 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;
}

From source file:cl.niclabs.tscrypto.common.encryption.KeyChain.java

public byte[] decrypt(String rsaKeyAlias, String encryptedData) {
    byte[] decrypted = null;

    try {/*  ww  w.  ja va 2  s  .  c o m*/
        Cipher decipher = Cipher.getInstance("RSA");
        decipher.init(Cipher.DECRYPT_MODE, getPrivateKey(rsaKeyAlias));
        decrypted = decipher.doFinal(Base64.decodeBase64(encryptedData));
    } catch (NoSuchAlgorithmException | UnrecoverableEntryException | KeyStoreException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return decrypted;
}

From source file:com.qubole.quark.catalog.db.encryption.AESEncrypt.java

public String convertToEntityAttribute(String phrase) throws SQLException {
    try {/* w  w w .ja  v  a  2  s  .co  m*/
        Cipher decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(decryptCipher.doFinal(Hex.decodeHex(phrase.toCharArray())));
    } catch (Exception e) {
        throw new SQLException(e);
    }
}