List of usage examples for javax.crypto CipherOutputStream close
public void close() throws IOException
From source file:uploadProcess.java
public static boolean encrypt(String path, FileItemStream item, String patientID) { try {//w ww . j a v a 2 s . c o m File mainFolder = new File(path + File.separator + "Encrypt"); if (!mainFolder.exists()) { mainFolder.mkdir(); } File patientFolder = new File(mainFolder + File.separator + patientID); if (!patientFolder.exists()) { patientFolder.mkdir(); } String ukey = GetKey.getPatientKey(patientID); InputStream fis = item.openStream(); FileOutputStream fos = new FileOutputStream( patientFolder.getAbsolutePath() + File.separator + item.getName()); 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(patientFolder, item.getName(), StoragePath.getDropboxDir() + patientID); DeleteDirectory.delete(patientFolder); return true; } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:org.panbox.core.crypto.CryptCore.java
public static byte[] _asymmetricDecrypt(byte[] symKey, PrivateKey pKey) throws InvalidKeyException, IOException { ASYMM_CIPHER.init(Cipher.DECRYPT_MODE, pKey); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(bos, ASYMM_CIPHER); cos.write(symKey);//from w w w. j a v a2 s. c om cos.flush(); cos.close(); byte[] byteArray = bos.toByteArray(); return byteArray; }
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Use the specified TripleDES key to encrypt bytes from the input stream and write them to the output stream. This * method uses CipherOutputStream to perform the encryption and write bytes at the same time. * /*from w w w. j a v a 2 s . c om*/ * @param key the key to use for encryption * @param in the input stream to encrypt * @param out the encrypted output stream * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws IOException */ public static void encrypt(final SecretKey key, final InputStream in, final OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IOException { // Create and initialize the encryption engine final Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); // Create a special output stream to do the work for us final CipherOutputStream cos = new CipherOutputStream(out, cipher); // Read from the input and write to the encrypting output stream final byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { cos.write(buffer, 0, bytesRead); } cos.close(); // For extra security, don't leave any plaintext hanging around memory. java.util.Arrays.fill(buffer, (byte) 0); }
From source file:org.panbox.core.crypto.CryptCore.java
public static byte[] encryptSymmetricKey(byte[] symKey, PublicKey pKey) throws SymmetricKeyEncryptionException { try {/* w ww .j a va 2s .c om*/ ASYMM_CIPHER.init(Cipher.ENCRYPT_MODE, pKey); ByteArrayOutputStream bos = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(bos, ASYMM_CIPHER); cos.write(symKey); cos.flush(); cos.close(); byte[] byteArray = bos.toByteArray(); return byteArray; } catch (Exception e) { throw new SymmetricKeyEncryptionException(e); } }
From source file:uploadProcess.java
public static boolean decrypt(File inputFolder, String fileName, String patientID, String viewKey) { try {//from www . j av a 2 s . c om 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:com.data.pack.Util.java
public static void copyFile(InputStream in, OutputStream out, int flag) throws IOException { byte[] buffer = new byte[1024]; int read;/*from ww w .j a v a 2 s .co m*/ try { Cipher encipher = null; try { encipher = Cipher.getInstance("AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Cipher decipher = null; try { decipher = Cipher.getInstance("AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } KeyGenerator kgen = null; try { kgen = KeyGenerator.getInstance("AES"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] keyStart = "fitnesSbridge".getBytes(); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(keyStart); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); // byte key[] = // {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; skey = kgen.generateKey(); // Lgo try { encipher.init(Cipher.ENCRYPT_MODE, skey); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } CipherInputStream cis = new CipherInputStream(in, encipher); try { decipher.init(Cipher.DECRYPT_MODE, skey); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } CipherOutputStream cos = new CipherOutputStream(out, decipher); try { if (flag == 2) { cos = new CipherOutputStream(out, encipher); } else { cos = new CipherOutputStream(out, decipher); } while ((read = in.read()) != -1) { cos.write(read); cos.flush(); } cos.flush(); cos.close(); in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { // TODO: handle exception } // // byte[] keyStart = "this is a key".getBytes(); // KeyGenerator kgen = KeyGenerator.getInstance("AES"); // SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // sr.setSeed(keyStart); // kgen.init(128, sr); // 192 and 256 bits may not be available // SecretKey skey = kgen.generateKey(); // byte[] key = skey.getEncoded(); // // // byte[] b = baos.toByteArray(); // while ((read = in.read(buffer)) != -1) { // // // decrypt // byte[] decryptedData = Util.decrypt(key,buffer); // out.write(decryptedData, 0, read); // } // } catch (NoSuchAlgorithmException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // catch (Exception e) { // // TODO: handle exception // } // }
From source file:at.tfr.securefs.util.Main.java
public void execute() throws Exception { Cipher cipher = null;// w w w . j a v a2s. 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:enc_mods.aes.java
/** * Encrypts the AES key to a file using an RSA public key *//*from www .j a va 2 s .c o m*/ public void saveKey(File out, File publicKeyFile) { try { // read public key to be used to encrypt the AES key byte[] encodedKey = new byte[(int) publicKeyFile.length()]; new FileInputStream(publicKeyFile).read(encodedKey); // create public key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey pk = kf.generatePublic(publicKeySpec); // write AES key cipher.init(Cipher.ENCRYPT_MODE, pk); CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), cipher); os.write(key); os.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java
public byte[] encryptPassowrd(String password) { Cipher cipher;//from w w w. j av a2 s.c om 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:org.codice.ddf.configuration.migration.CipherUtilsTest.java
@Test public void createEncryptedFile() throws IOException { CipherUtils cipherUtils = new CipherUtils(zipPath); Path encryptedFile = Paths.get(ddfHome.toString(), "encrypted.test.file"); FileOutputStream fos = new FileOutputStream(encryptedFile.toFile()); CipherOutputStream cipherOutputStream = cipherUtils.getCipherOutputStream(fos); IOUtils.write("foo", cipherOutputStream, StandardCharsets.UTF_8); cipherOutputStream.close(); assertThat(readFileToString(encryptedFile.toFile(), StandardCharsets.UTF_8), Matchers.not("foo")); }