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.rdonasco.security.utils.EncryptionUtil.java

public static String decryptWithPassword(String encryptedString, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] decryptedBytes = pbeCipher.doFinal(Base64.decodeBase64(encryptedString));
    return new String(decryptedBytes);
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static String rsaDecrypt(String[] string) throws Exception {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrivateKey privateKey = (PrivateKey) readKeyFromFile(Config.getProperty("escidoc.aa.private.key.file"),
            false);/*from w w w  .jav  a2  s  .com*/
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    for (String part : string) {
        byte[] inArr = Base64.decodeBase64(part.getBytes("UTF-8"));
        baos.write(cipher.doFinal(inArr));
        baos.flush();
    }

    return new String(baos.toByteArray(), "UTF-8");

}

From source file:Clases.cCifrado.java

public String Desencriptar(String textoEncriptado) {

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

    try {/*from   ww  w  .j a  v  a  2s .  com*/
        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:CipherSocket.java

public InputStream getInputStream() throws IOException {
    InputStream is = delegate == null ? super.getInputStream() : delegate.getInputStream();
    Cipher cipher = null;/* ww  w  .jav  a 2s. com*/
    try {
        cipher = Cipher.getInstance(algorithm);
        int size = cipher.getBlockSize();
        byte[] tmp = new byte[size];
        Arrays.fill(tmp, (byte) 15);
        IvParameterSpec iv = new IvParameterSpec(tmp);
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Failed to init cipher: " + e.getMessage());
    }
    CipherInputStream cis = new CipherInputStream(is, cipher);
    return cis;
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

private static Cipher getCipher(int mode) throws Exception {
    final SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(MessageCryptoUtil.AES_KEY.toCharArray()),
            "AES");
    final Cipher cipher = Cipher.getInstance("AES");
    cipher.init(mode, skeySpec);/*from  w  w  w.  ja v  a  2  s.  c om*/
    return cipher;
}

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

public String convertToEntityAttribute(String phrase) throws SQLException {
    try {//from w  w  w . j  av  a  2 s .c  o 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);
    }

}

From source file:com.jsmartframework.web.manager.TagEncrypter.java

private static Cipher getDecryptCipher(HttpServletRequest request) throws Exception {
    Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_TAG_DECRYPT_CIPHER);
    if (decryptCipher == null) {
        decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_TAG_DECRYPT_CIPHER, decryptCipher);
    }//from   w  w  w.ja v  a 2 s  .  c o  m
    return decryptCipher;
}

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

/**
 * This function is used to decrypt incoming data.
 * @param key Encryption key/*from www .j a  v  a 2s  . c  o m*/
 * @param data Data string which should be decrypted.
 * @return The decrypted data string.
 */
public static String decrypt(String key, String data) {
    byte[] decryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

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

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

public static boolean encrypt(File inputFolder, File outputFolder, String fileName, String patientID) {
    try {/*w w  w . ja  va  2  s.  com*/

        String ukey = GetKey.getPatientKey(patientID);
        File filePath = new File(inputFolder.getAbsolutePath() + File.separator + fileName);
        FileInputStream fis = new FileInputStream(filePath);
        File outputFile = new File(outputFolder.getAbsolutePath() + File.separator + fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        //            System.out.println(key);
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();

        //Upload File to cloud
        DropboxUpload upload = new DropboxUpload();
        upload.uploadFile(outputFolder, fileName, StoragePath.getDropboxDir() + patientID);
        DeleteDirectory.delete(outputFolder);
        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java

public byte[] encryptPassowrd(String password) {

    Cipher cipher;/* w w  w  .  j a  v  a  2s .  c  o  m*/
    try {
        cipher = Cipher.getInstance(this.algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, this.keyPair.getPublic());

        ByteArrayOutputStream baosEncryptedData = new ByteArrayOutputStream();
        CipherOutputStream cos = new CipherOutputStream(baosEncryptedData, cipher);

        cos.write(password.getBytes("UTF-8"));
        cos.flush();
        cos.close();

        return baosEncryptedData.toByteArray();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new IllegalStateException(e.getMessage(), e);
    }
}