Example usage for javax.crypto Cipher init

List of usage examples for javax.crypto Cipher init

Introduction

In this page you can find the example usage for javax.crypto Cipher init.

Prototype

public final void init(int opmode, Certificate certificate) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate.

Usage

From source file:uploadProcess.java

public static boolean decrypt(File inputFolder, String fileName, String patientID, String viewKey) {
    try {//w  ww .  j a v  a  2 s.com

        String ukey = GetKey.getPatientKey(patientID);
        if (viewKey.equals(ukey)) {
            //Download File from Cloud..
            DropboxUpload download = new DropboxUpload();
            download.downloadFile(fileName, StoragePath.getDropboxDir() + patientID, inputFolder);

            File inputFile = new File(inputFolder.getAbsolutePath() + File.separator + fileName);
            FileInputStream fis = new FileInputStream(inputFile);
            File outputFolder = new File(inputFolder.getAbsolutePath() + File.separator + "temp");
            if (!outputFolder.exists()) {
                outputFolder.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(
                    outputFolder.getAbsolutePath() + File.separator + fileName);
            byte[] k = ukey.getBytes();
            SecretKeySpec key = new SecretKeySpec(k, "AES");
            Cipher enc = Cipher.getInstance("AES");
            enc.init(Cipher.DECRYPT_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();
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static byte[] decrypt(byte[] aesKeyEncrypted, PrivateKey privKey) {
    byte[] aesKey = null;
    try {/*from   w  w  w.  j ava2  s .c o m*/
        Cipher cipher = Cipher.getInstance(ALGORITHM_ASYM);
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        aesKey = cipher.doFinal(aesKeyEncrypted);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return aesKey;
}

From source file:com.alliander.osgp.oslp.OslpUtils.java

private static byte[] createEncryptedHash(final byte[] message, final PrivateKey privateKey)
        throws GeneralSecurityException {

    final byte[] hash = createHash(message);

    // Encrypt the hash
    final Cipher cipher = Cipher.getInstance(FALLBACK_CIPHER);
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    return cipher.doFinal(hash);
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static Object decrypt(String encryptedObject, byte[] aesKey) {
    Object object = null;//from  w  ww  . jav  a 2  s .c om
    SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM);
    try {
        byte[] cipherText = new BASE64Decoder().decodeBuffer(encryptedObject);
        Cipher cipher = Cipher.getInstance(ALGORITHM_SYM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] serialized_obj = cipher.doFinal(cipherText);
        object = (Object) SerializationUtils.deserialize(serialized_obj);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return object;
}

From source file:com.screenslicer.common.Crypto.java

private static String encodeHelper(String plainText, String encryptionKey) {
    if (plainText == null || encryptionKey == null) {
        return null;
    }/* ww  w .java 2s.  c  om*/
    try {
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(DigestUtils.sha256(encryptionKey), "AES"));
        return Base64.encodeBase64String(aesCipher.doFinal(plainText.getBytes("utf-8")));
    } catch (Exception e) {
        return null;
    }
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
    ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
    for (SecurityKeyData dr : devices) {
        KeyHolder kh = new KeyHolder();
        kh.setCounter(dr.getCounter());//from  www. j a  v a2 s.c  om
        kh.setEnrollmentTime(dr.getEnrollmentTime());
        kh.setKeyHandle(dr.getKeyHandle());
        kh.setPublicKey(dr.getPublicKey());
        kh.setTransports(dr.getTransports());
        keys.add(kh);
    }

    String json = gson.toJson(keys);
    EncryptedMessage msg = new EncryptedMessage();

    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
    if (key == null) {
        throw new Exception("Queue message encryption key not found");
    }

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
    msg.setIv(cipher.getIV());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DeflaterOutputStream compressor = new DeflaterOutputStream(baos,
            new Deflater(Deflater.BEST_COMPRESSION, true));

    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();

    String b64 = new String(Base64.encodeBase64(baos.toByteArray()));

    return b64;

}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static String encrypt(Serializable object, byte[] aesKey) {
    SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM);
    String object_encrypted = null;
    try {/*  w w  w  . j  a  va  2  s . com*/
        final Cipher cipher = Cipher.getInstance(ALGORITHM_SYM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = SerializationUtils.serialize(object);
        byte[] cipherText = cipher.doFinal(data);
        object_encrypted = new BASE64Encoder().encode(cipherText);
    } catch (Exception e) {
        e.printStackTrace();
        return "bbb";
    }
    return object_encrypted;
}

From source file:com.netcrest.pado.internal.security.AESCipher.java

private static byte[] decryptBinaryToBinary(byte[] pke, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(pke, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(encrypted);
}

From source file:com.netcrest.pado.internal.security.AESCipher.java

private static byte[] encryptBinaryToBinary(byte[] privateKeyEncrypted, byte[] clearBinary) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(privateKeyEncrypted, "AES");

    // Instantiate the cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clearBinary);
    return encrypted;
}

From source file:com.alliander.osgp.oslp.OslpUtils.java

private static boolean validateEncryptedHash(final byte[] message, final byte[] securityKey,
        final PublicKey publicKey) throws GeneralSecurityException {

    // Calculate hash of message
    final byte[] verifyHash = createHash(message);

    try {/*from w ww.j  a v  a2s  .  c  o m*/
        // Decrypt security key hash
        final Cipher cipher = Cipher.getInstance(FALLBACK_CIPHER);
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        final byte[] messageHash = cipher.doFinal(securityKey);

        // Verify calculated and received hash
        return Arrays.equals(messageHash, verifyHash);
    } catch (final BadPaddingException e) {
        LOGGER.error("unexpected exception", e);
        return false;
    }
}