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.jsmartframework.web.manager.TagEncrypter.java

private static Cipher getEncryptCipher(HttpServletRequest request) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_TAG_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_TAG_ENCRYPT_CIPHER, encryptCipher);
    }// www. jav a  2 s  .  c o m
    return encryptCipher;
}

From source file:corner.services.impl.DESedeEncryptServiceImpl.java

public byte[] encrypt(byte[] src, byte[] key) {
    try {//from  ww  w  . jav  a2s .  co m
        SecretKey deskey = new SecretKeySpec(key, Algorithm);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        return c1.doFinal(src);
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

From source file:com.haulmont.timesheets.EncryptDecrypt.java

public EncryptDecrypt(String key) {
    try {/*from ww  w  .j a  v a  2s . c  om*/
        String data = new StringBuilder(SALT + key).reverse().toString();
        SecretKeySpec secretKey = new SecretKeySpec(DigestUtils.md5(data), "AES");
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(INIT_VECTOR);
        eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        eCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        dCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
    } catch (Exception e) {
        throw new RuntimeException("Exception while init cipher:", e);
    }
}

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Encryption /*from  ww  w  .  ja  v a 2 s.  c o m*/
 * @param content content to be encrypted
 * @param encryptKey encryption key
 * @return 
 * @throws Exception 
 */
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:com.seer.datacruncher.utils.CryptoUtil.java

public CryptoUtil() {
    try {//from   w w  w.j  a va2s  .c o  m
        String myEncryptionKey = cryWorm(Reserved.ENCRYPTIONKEY, Reserved.CHECK, 0);
        String myEncryptionSchema = DESEDE_ENCRYPTION_SCHEMA;
        byte[] keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        KeySpec myKeySpec = new DESedeKeySpec(keyAsBytes);
        SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionSchema);
        cipher = Cipher.getInstance(myEncryptionSchema);
        key = mySecretKeyFactory.generateSecret(myKeySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

private void dumpDigests() throws Exception {
    System.err.println(cfd.getCadenaOriginal());
    String certStr = cfd.document.getCertificado();
    Base64 b64 = new Base64();
    byte[] cbs = b64.decode(certStr);
    X509Certificate cert = (X509Certificate) KeyLoaderFactory
            .createInstance(KeyLoaderEnumeration.PUBLIC_KEY_LOADER, new ByteArrayInputStream(cbs)).getKey();
    cert.checkValidity();//w  w  w.  j av  a  2  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:io.manasobi.utils.CryptoUtils.java

/**
 *  ./*from  w  w  w. j  av  a  2s  .  c om*/
 *
 * @param keyHex generateHexKey ? ? ?? Hex ? ? 
 * @param data    ?? byte 
 * 
 * @return  ? ??
 */
public static byte[] encryptByDES(String keyHex, byte[] data) {

    SecretKey key = getSecretDESKeyFromHex(keyHex);

    byte[] encryptedData = null;

    try {

        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.ENCRYPT_MODE, key);

        encryptedData = cipher.doFinal(data);

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return encryptedData;
}

From source file:cipher.UsableCipher.java

public UsableCipher(Transformation transformation, String key) throws Exception {
    this.cipher = Cipher.getInstance(transformation.asString());
    this.transformation = transformation;
    this.key = generateSecretKey(key, transformation.cipherName());
}

From source file:net.sf.jftp.config.Crypto.java

public static String Encrypt(String str) {
    // create cryptography object
    SecretKeyFactory factory;// w w  w .jav a  2  s  .com
    try {
        factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchAlgorithmException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    }

    // init key
    SecretKey key;

    try {
        key = factory.generateSecret(new PBEKeySpec(PASSWORD));
    } catch (InvalidKeySpecException e) {
        // The password is hard coded - this exception can't be the case
        return "";
    }

    // init cipher
    Cipher pbeCipher;
    try {
        pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    } catch (NoSuchPaddingException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    } catch (NoSuchAlgorithmException e) {
        // We could try another algorithm, but it is highly unlikely that this would be the case
        return "";
    }

    try {
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    } catch (InvalidKeyException e) {
        return "";
    } catch (InvalidAlgorithmParameterException e) {
        return "";
    }

    // encode & return encoded string
    try {
        return base64Encode(pbeCipher.doFinal(str.getBytes()));
    } catch (IllegalBlockSizeException e) {
        return "";
    } catch (BadPaddingException e) {
        return "";
    }
}

From source file:com.parleys.server.frontend.web.ipad.filters.AESEncrypter.java

private AESEncrypter(final Key key) {
    try {/*from  ww w.  j  a  v a 2 s .  c  o  m*/
        ecipher = Cipher.getInstance("AES");
        dcipher = Cipher.getInstance("AES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);
    } catch (Exception e) {
        LOGGER.fatal(e);
        throw new RuntimeException("Error generating key");
    }
}