Example usage for javax.crypto.spec SecretKeySpec SecretKeySpec

List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec SecretKeySpec SecretKeySpec.

Prototype

public SecretKeySpec(byte[] key, String algorithm) 

Source Link

Document

Constructs a secret key from the given byte array.

Usage

From source file:com.ad.mediasharing.tvmclient.AESEncryption.java

private static SecretKeySpec getKey(String key) throws Exception {
    return new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
}

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  ww w. ja va 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;
}

From source file:Conexion.newClass.java

public String encode(String texto) throws EncoderException {
    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*www. j av a2 s. co 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 base64EncryptedString;
}

From source file:com.enviosya.client.tool.Tool.java

public String Encriptar(String texto) {

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

    try {/*from  w ww . j  a  v  a 2 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 e) {
        //Ac tengo que agregar el retorno de la exception
    }
    return base64EncryptedString;
}

From source file:it.geosolutions.figis.persistence.dao.util.PwEncoder.java

/**
 * Encoding the password on base64/*from   ww  w  .  j  av a2s .c o m*/
 * @param msg
 * @return
 */
public static String encode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = msg.getBytes();
        byte[] encrypted = cipher.doFinal(input);
        byte[] output = Base64.encodeBase64(encrypted);

        return new String(output);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (IllegalBlockSizeException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (BadPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException("Error while encoding", ex);
    }
}

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: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:com.jk.security.JKEncDec.java

/**
 * Encrypt.//from w w  w  . j  a  va  2  s.  com
 *
 * @param plainText
 *            the plain text
 * @return the string
 */
public static String encrypt(String plainText) {
    try {
        plainText = fixText(plainText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return toString(cipher.doFinal(plainText.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}

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:controlpac.EncryptHelper.java

public static String Desencriptar(String textoEncriptado) {
    String base64EncryptedString = "";
    try {/*  w w  w  .  j  a  v a2s .  co  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));//Desencodea el texto en base64
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); //Crea un hash con la clave elegida
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");//Crea la clave 
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key); //Inicia el descifrado
        byte[] plainText = decipher.doFinal(message);//Descifra el texto
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}