List of usage examples for javax.crypto CipherOutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this output stream. 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); cos.close();//from w w w. j a va 2 s . c om }
From source file:Main.java
public static void encrypt(String fileIn, String fileOut, byte key[]) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream fis = new FileInputStream(fileIn); // This stream write the encrypted text. This stream will be wrapped by another stream. FileOutputStream fos = new FileOutputStream(fileOut); // Length is 32 bytes //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-256"); key = sha.digest(key);/*from w ww. j ava2 s. c o m*/ SecretKeySpec sks = new SecretKeySpec(key, "AES"); // Create cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sks); // Wrap the output stream CipherOutputStream cos = new CipherOutputStream(fos, cipher); // Write bytes int b; byte[] d = new byte[8]; while ((b = fis.read(d)) != -1) { cos.write(d, 0, b); } // Flush and close streams. cos.flush(); cos.close(); fis.close(); }
From source file:uploadProcess.java
public static boolean encrypt(File inputFolder, File outputFolder, String fileName, String patientID) { try {/* w w w .j a v a 2 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:uploadProcess.java
public static boolean decrypt(File inputFolder, String fileName, String patientID) { try {//from ww w .ja v a 2 s. c o m //Download File from Cloud.. DropboxUpload download = new DropboxUpload(); download.downloadFile(fileName, StoragePath.getDropboxDir() + patientID, inputFolder); String ukey = GetKey.getPatientKey(patientID); 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; } catch (Exception e) { System.out.println("Error: " + e); } return false; }
From source file:uploadProcess.java
public static boolean encrypt(String path, FileItemStream item, String patientID) { try {/*w w w . j a va 2s . 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: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 ww w .j a v a 2s .c o m*/ public static void encrypt(SecretKey key, InputStream in, OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IOException { // Create and initialize the encryption engine Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); // Create a special output stream to do the work for us CipherOutputStream cos = new CipherOutputStream(out, cipher); // Read from the input and write to the encrypting output stream 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:uploadProcess.java
public static boolean decrypt(File inputFolder, String fileName, String patientID, String viewKey) { try {/* www . j a v 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.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. ja v a2 s . c o m * @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:cd.education.data.collector.android.utilities.EncryptionUtils.java
private static void encryptFile(File file, EncryptedFormInformation formInfo) throws IOException, EncryptionException { File encryptedFile = new File(file.getParentFile(), file.getName() + ".enc"); if (encryptedFile.exists() && !encryptedFile.delete()) { throw new IOException( "Cannot overwrite " + encryptedFile.getAbsolutePath() + ". Perhaps the file is locked?"); }/*from w w w . j ava 2 s . c o m*/ // add elementSignatureSource for this file... formInfo.appendFileSignatureSource(file); RandomAccessFile randomAccessFile = null; CipherOutputStream cipherOutputStream = null; try { Cipher c = formInfo.getCipher(); randomAccessFile = new RandomAccessFile(encryptedFile, "rws"); ByteArrayOutputStream encryptedData = new ByteArrayOutputStream(); cipherOutputStream = new CipherOutputStream(encryptedData, c); InputStream fin = new FileInputStream(file); byte[] buffer = new byte[2048]; int len = fin.read(buffer); while (len != -1) { cipherOutputStream.write(buffer, 0, len); len = fin.read(buffer); } fin.close(); cipherOutputStream.flush(); cipherOutputStream.close(); randomAccessFile.write(encryptedData.toByteArray()); Log.i(t, "Encrpyted:" + file.getName() + " -> " + encryptedFile.getName()); } catch (Exception e) { String msg = "Error encrypting: " + file.getName() + " -> " + encryptedFile.getName(); Log.e(t, msg, e); e.printStackTrace(); throw new EncryptionException(msg, e); } finally { IOUtils.closeQuietly(cipherOutputStream); if (randomAccessFile != null) { randomAccessFile.close(); } } }
From source file:org.apache.synapse.commons.security.wrappers.CipherWrapper.java
/** * Returns the output of the cipher operation. * This expose the 'getSecret' abstraction and hide operation of the underlying cipher * * @param inputStream Input as a stream. This can be either cipher or plain text * @return Secret as String.This can be either cipher or plain text *///from w w w.j a va 2 s . com public String getSecret(InputStream inputStream) { InputStream sourceStream = null; if (cipherInformation.getInType() != null) { try { sourceStream = EncodingHelper.decode(inputStream, cipherInformation.getInType()); } catch (IOException e) { handleException("IOError when decoding the input stream for cipher ", e); } } else { sourceStream = inputStream; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); CipherOutputStream out = new CipherOutputStream(baos, cipher); byte[] buffer = new byte[64]; int length; try { while ((length = sourceStream.read(buffer)) != -1) { out.write(buffer, 0, length); } } catch (IOException e) { handleException("IOError when reading the input stream for cipher ", e); } finally { try { sourceStream.close(); out.flush(); out.close(); } catch (IOException ignored) { // ignore exception } } String secret; if (cipherInformation.getOutType() != null) { secret = EncodingHelper.encode(baos, cipherInformation.getOutType()); } else { secret = baos.toString(); } return secret; }