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:testFileHandler.java

public void encrypt(String message, String secretKey, javax.swing.JTextArea ciphertextField) throws Exception {

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    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);
    // Encode the string into bytes using utf-8
    byte[] plainTextBytes = message.getBytes("utf-8");
    // Encrypt/*from w w  w . j av a2  s.  c o  m*/
    byte[] buf = cipher.doFinal(plainTextBytes);
    // Encode bytes to base64 to get a string
    byte[] base64Bytes = Base64.encodeBase64(buf);
    String base64EncryptedString = new String(base64Bytes);

    ciphertextField.setText(base64EncryptedString);
}

From source file:es.jamisoft.comun.utils.cipher.CifradoPBE3DES.java

License:asdf

public CifradoPBE3DES(char secret[], byte salt[], int count) {
    secretKey = generatePBKey(secret);/*  ww w.  j  av a2 s  .com*/

    try {
        pbeCipher = Cipher.getInstance("PBEWithMD5AndTripleDES");
        paramSpec = new PBEParameterSpec(salt, count);
    } catch (Exception e) {
        System.out.println("Error en la inicializacion de la clase de cifrado:" + e);
        e.printStackTrace();
    }
}

From source file:com.bytecode.util.Crypto.java

private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception {
    byte[] decValue = null;
    byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16);
    IvParameterSpec nonce = new IvParameterSpec(nonceBytes);

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key, nonce);
    decValue = c.doFinal(message, 8, message.length - 8);

    return decValue;
}

From source file:com.wabacus.util.DesEncryptTools.java

public static String decrypt(String encryptedString) {
    try {// ww  w.j  a  v  a 2  s . c o  m
        if (KEY_OBJ == null) {
            log.warn("" + encryptedString);
            return encryptedString;
        }
        byte[] b = base64Decode(encryptedString);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, KEY_OBJ);
        return new String(c1.doFinal(b));
    } catch (Exception e) {
        throw new WabacusConfigLoadingException("" + encryptedString + "", e);
    }
}

From source file:com.searchcode.app.util.AESEncryptor.java

public byte[] decrypt(byte[] cipherText) throws Exception {
    SecretKeySpec secretKey = new SecretKeySpec(this.key, this.ALGORITHM);
    Cipher cipher = Cipher.getInstance(this.ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    return cipher.doFinal(cipherText);
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * return encrypted value of string// ww w  . j a  v  a 2  s. c o  m
 *
 * @param str unencrypted string
 * @return encrypted string
 */
public static String encrypt(String str) {

    String retVal = null;
    if (str != null && str.length() > 0) {
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
            byte[] encVal = c.doFinal(str.getBytes());
            retVal = new String(Base64.encodeBase64(encVal));
        } catch (Exception ex) {
            log.error(ex.toString(), ex);
        }

    }
    return retVal;
}

From source file:mx.bigdata.cfdi.CFDv3Debugger.java

public void dumpDigests() throws Exception {
    System.err.println(cfd.getOriginalString());
    byte[] digest = cfd.getDigest();
    CFDv3.dump("Digestion generada", digest, System.err);
    String certStr = cfd.document.getCertificado();
    Base64 b64 = new Base64();
    byte[] cbs = b64.decode(certStr);
    X509Certificate cert = KeyLoader.loadX509Certificate(new ByteArrayInputStream(cbs));
    cert.checkValidity();/*  w  w  w .  j a  v a2 s. co  m*/
    String sigStr = cfd.document.getSello();
    byte[] signature = b64.decode(sigStr);
    CFDv3.dump("Digestion firmada", signature, System.err);
    Cipher dec = Cipher.getInstance("RSA");
    dec.init(Cipher.DECRYPT_MODE, cert);
    byte[] result = dec.doFinal(signature);
    CFDv3.dump("Digestion decriptada", result, System.err);
    ASN1InputStream aIn = new ASN1InputStream(result);
    ASN1Sequence seq = (ASN1Sequence) aIn.readObject();
    ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
    CFDv3.dump("Sello", sigHash.getOctets(), System.err);
}

From source file:com.clustercontrol.commons.util.CryptUtil.java

private static String encrypt(String key, String word) {
    if (word == null) {
        return null;
    }/*from  w w w.  j  av  a2s .  c om*/
    // ?
    SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), algorithm);
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(algorithm);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, sksSpec);
    } catch (InvalidKeyException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    byte[] encrypted = null;
    try {
        encrypted = cipher.doFinal(word.getBytes());
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    return Base64.encodeBase64String(encrypted);
}

From source file:com.dasol.util.AES256Util.java

public String aesEncode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
    String enStr = new String(Base64.encodeBase64(encrypted));

    return enStr;
}

From source file:com.commander4j.util.JCipher.java

private Cipher getCipher(int cipherMode) throws Exception {
    String encryptionAlgorithm = "AES";
    SecretKeySpec keySpecification = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), encryptionAlgorithm);
    Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
    cipher.init(cipherMode, keySpecification);

    return cipher;
}