Example usage for javax.crypto CipherOutputStream CipherOutputStream

List of usage examples for javax.crypto CipherOutputStream CipherOutputStream

Introduction

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

Prototype

public CipherOutputStream(OutputStream os, Cipher c) 

Source Link

Document

Constructs a CipherOutputStream from an OutputStream and a Cipher.

Usage

From source file:DesEncrypter.java

public void encrypt(InputStream in, OutputStream out) throws Exception {
    out = new CipherOutputStream(out, ecipher);

    int numRead = 0;
    while ((numRead = in.read(buf)) >= 0) {
        out.write(buf, 0, numRead);// www.  jav a 2 s . c  om
    }
    out.close();
}

From source file:MainClass.java

public static void write(byte[] bytes, OutputStream out) throws Exception {
    CipherOutputStream cos = new CipherOutputStream(out, m_encrypter);
    cos.write(bytes, 0, bytes.length);// www . ja  v a 2s . c o  m
    cos.close();
}

From source file:com.scorpio4.util.io.IOStreamCrypto.java

public CipherOutputStream encrypt(OutputStream out) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException {
    final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec);
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance(cipherTransformation);
    cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    return new CipherOutputStream(new Base64OutputStream(out), cipher);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String encryptStringImpl(Context context, final String plainText) {
    String encryptedText = plainText;
    try {//  w w  w.j a va2  s.c  om
        final KeyStore keyStore = getKeyStore(context);

        PublicKey publicKey = keyStore.getCertificate(KEY_ALIAS).getPublicKey();

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        cipherOutputStream.write(plainText.getBytes("UTF-8"));
        cipherOutputStream.close();

        byte[] bytes = outputStream.toByteArray();
        encryptedText = Base64.encodeToString(bytes, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedText;
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey));

    SecretKey key = null;//from w  w  w  .  j  a  va 2  s.c  om
    Cipher cipher = null;
    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        key = secretKeyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DES");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:org.orbeon.oxf.main.SecureResource.java

private void encrypt() {
    try {/*from   w w  w  .ja va 2s  .  c o m*/
        FileOutputStream archiveFile = new FileOutputStream(archiveName);

        ZipOutputStream zip = new ZipOutputStream(new CipherOutputStream(archiveFile,
                SecureUtils.getEncryptingCipher(SecureResourceManagerImpl.getPassword(), true)));
        File rr = new File(resourceRoot);
        archiveAndEncrypt(rr, zip);
        zip.finish();
        zip.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:uploadProcess.java

public static boolean encrypt(File inputFolder, File outputFolder, String fileName, String patientID) {
    try {/*from  w  w w . j  av a2 s  .c  o  m*/

        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:at.tfr.securefs.util.Main.java

public void execute() throws Exception {

    Cipher cipher = null;//  w w w  .  j  a va 2 s.  c om

    if (test) {
        nrOfShares = KeyConstants.nrOfSharesForTest;
        threshold = KeyConstants.thresholdForTest;
        modulus = KeyConstants.modulusForTest;
        shares = KeyConstants.sharesForTest;
    }

    configuration.setBasePath(basePath);

    secret = new Shamir().combine(nrOfShares, threshold, modulus, shares);

    secretBean.setSecret(secret);
    Path filePath = basePath.resolve(file);

    cipher = sksBean.getCipher(configuration.getSalt(), mode);

    OutputStream outputStream = outFile == null ? System.out
            : new FileOutputStream(basePath.resolve(outFile).toFile());

    CipherOutputStream os = new CipherOutputStream(outputStream, cipher);

    IOUtils.copy(new FileInputStream(filePath.toFile()), os);
    os.close();

}

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

public byte[] encryptPassowrd(String password) {

    Cipher cipher;//  w w w .ja v a2 s.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);
    }
}

From source file:corner.util.crypto.Cryptor.java

/**
 * IO?,IO??,./*ww  w  .j a  v  a 2  s.c om*/
 * @param outFileName ??. 
 * @param keyFile ??.
 * @return ??.
 */
public static OutputStream encryptFileIO(String outFileName, String keyFile) {
    if (keyFile == null) {
        try {
            return new FileOutputStream(outFileName);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    SecretKey key = null;

    //?
    ObjectInputStream keyis;
    try {
        keyis = new ObjectInputStream(new FileInputStream(keyFile));
        key = (SecretKey) keyis.readObject();
        keyis.close();
    } catch (FileNotFoundException e) {
        log.error("file not found!", e);
        throw new RuntimeException("file not found", e);
    } catch (IOException e) {
        log.error("io occour exception", e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        log.error("Class Not Found  exception", e);
        throw new RuntimeException(e);
    }

    //keyCipher
    Cipher cipher = null;
    //?Cipher?

    try {
        cipher = Cipher.getInstance("DES");
        //?

        cipher.init(Cipher.ENCRYPT_MODE, key);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }

    //???

    //
    CipherOutputStream out = null;
    try {
        out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)), cipher);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    return out;

}