List of usage examples for org.bouncycastle.crypto.io CipherOutputStream CipherOutputStream
public CipherOutputStream(OutputStream os, AEADBlockCipher cipher)
From source file:com.github.flbaue.jcrypttool.v1.EncryptionRunnable.java
License:Apache License
@Override public void run() { progress.start();/*from w w w . j a v a2s .co m*/ final byte[] salt = generateSalt(); final byte[] key = generateKey(encryptionSettings.password, salt); final byte[] iv; final PaddedBufferedBlockCipher cipher; try (OutputStream fileOutputStream = new BufferedOutputStream( new FileOutputStream(encryptionSettings.outputFile))) { cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); iv = generateIV(); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); /* System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)"); System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)"); System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)"); */ InputStream in = null; OutputStream out = null; try { writeInitBlock(fileOutputStream, salt, iv); in = new BufferedInputStream(new FileInputStream(encryptionSettings.inputFile)); out = new GZIPOutputStream(new CipherOutputStream(fileOutputStream, cipher)); processStreams(in, out); } catch (IOException e) { throw new RuntimeException(e); } finally { closeStream(in); closeStream(out); } } catch (IOException e) { throw new RuntimeException(e); } progress.setFinished(); }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public OutputStream encryptedOutputStream(final Path path, final String password) throws IOException, EncryptionFailedException { try {/* w ww . j a v a 2 s .c o m*/ final byte[] salt = generateSalt(); final byte[] key = generateKey(password, salt); final byte[] iv = generateIV(); final byte[] fileInitBlock = generateOutputInitBlock(salt, iv); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path)); out.write(fileInitBlock); return new CipherOutputStream(out, cipher); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new EncryptionFailedException(e); } }
From source file:com.github.horrorho.inflatabledonkey.cache.StreamCryptor.java
License:Open Source License
public CipherOutputStream newCipherOutputStream(OutputStream os, byte[] password) throws IOException { byte[] salt = randomBytes(saltLength); byte[] nonce = randomBytes(nonceLength); os.write(salt);// w w w . j a va2 s .co m os.write(nonce); byte[] dk = kdf.apply(password, salt); GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce); cipher.init(true, parameters); return new CipherOutputStream(os, cipher); }
From source file:com.hanhuy.keepassj.StandardAesEngine.java
License:Open Source License
private static OutputStream CreateOutputStream(OutputStream s, boolean bEncrypt, byte[] pbKey, byte[] pbIV) { byte[] pbLocalIV = new byte[16]; System.arraycopy(pbIV, 0, pbLocalIV, 0, 16); byte[] pbLocalKey = new byte[32]; System.arraycopy(pbKey, 0, pbLocalKey, 0, 32); try {//from w ww . java 2 s . c o m BlockCipher aes = AesEngines.createAesEngine(); KeyParameter key = new KeyParameter(pbLocalKey); ParametersWithIV iv = new ParametersWithIV(key, pbLocalIV); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes)); cipher.init(true, iv); // Cipher r = Cipher.getInstance("AES/CBC/PKCS5Padding"); // IvParameterSpec ivspec = new IvParameterSpec(pbLocalIV); // SecretKeySpec keyspec = new SecretKeySpec(pbLocalKey, "AES"); // r.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); return new CipherOutputStream(s, cipher); } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:com.javacreed.api.secureproperties.bouncycastle.TwoFishCipherFactory.java
License:Apache License
@Override protected OutputStream wrapInToCipheredOutputStream(final OutputStream out) throws Exception { final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); cipher.init(true, new KeyParameter(key.getBytes("UTF-8"))); final CipherOutputStream stream = new CipherOutputStream(out, cipher); return stream; }
From source file:com.raphfrk.craftproxyliter.LocalSocket.java
License:Open Source License
public void setAES() { BufferedBlockCipher in = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); in.init(false, new ParametersWithIV(new KeyParameter(this.ptc.getSecretKey().getEncoded()), this.ptc.getSecretKey().getEncoded(), 0, 16)); BufferedBlockCipher out = new BufferedBlockCipher(new CFBBlockCipher(new AESFastEngine(), 8)); out.init(true, new ParametersWithIV(new KeyParameter(this.ptc.getSecretKey().getEncoded()), this.ptc.getSecretKey().getEncoded(), 0, 16)); this.in = new DataInputStream(new CipherInputStream(this.in, in)); this.out = new DataOutputStream(new CipherOutputStream(this.out, out)); pin = new ProtocolInputStream(this.in, 255 * 16 * 1024); pout = new ProtocolOutputStream(this.out); }
From source file:de.tntinteractive.portalsammler.engine.CryptoHelper.java
License:Open Source License
public static CipherOutputStream createAesEncryptStream(final OutputStream os, final byte[] key, final SecureRandom srand) { final PaddedBufferedBlockCipher cipher = initAes(key, srand, true); return new CipherOutputStream(os, cipher); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java
License:Apache License
@Override public void decrypt(InputStream inputStream, OutputStream outputStream) throws InvalidCipherTextException, IOException, DecoderException { final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream, FileEncrypter.ENCODING); final BufferedBlockCipher cipher = createCipher(tarInputStream); //Advance to the next entry in the tar file tarInputStream.getNextTarEntry();/*from ww w . ja v a 2 s.c om*/ //Create digest output stream used to generate digest while decrypting final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester()); //Do a streaming decryption of the file output final CipherOutputStream cipherOutputStream = new CipherOutputStream( new TeeOutputStream(outputStream, digestOutputStream), cipher); IOUtils.copy(tarInputStream, cipherOutputStream); cipherOutputStream.close(); //Capture the hash of the decrypted output final byte[] hashBytes = digestOutputStream.getDigest(); verifyOutputHash(tarInputStream, hashBytes); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java
License:Apache License
@Override public void encrypt(String fileName, int size, InputStream inputStream, OutputStream outputStream) throws InvalidCipherTextException, IOException { final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream, 512, ENCODING);//from w ww . j ava2 s . co m final BufferedBlockCipher cipher = createCipher(tarArchiveOutputStream); startEncryptedFile(fileName, size, tarArchiveOutputStream, cipher); //Setup cipher output stream, has to protect from close as the cipher stream must close but the tar cannot get closed yet final CipherOutputStream cipherOutputStream = new CipherOutputStream( new CloseShieldOutputStream(tarArchiveOutputStream), cipher); //Setup digester final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester()); //Perform streaming encryption and hashing of the file IOUtils.copy(inputStream, new TeeOutputStream(cipherOutputStream, digestOutputStream)); cipherOutputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); //Capture the hash code of the encrypted file digestOutputStream.close(); final byte[] hashBytes = digestOutputStream.getDigest(); this.writeHashfile(tarArchiveOutputStream, hashBytes); //Close the TAR stream, nothing else should be written to it tarArchiveOutputStream.close(); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java
License:Apache License
@Override public OutputStream encrypt(String fileName, int size, OutputStream outputStream) throws InvalidCipherTextException, IOException, DecoderException { final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(outputStream, ENCODING); final BufferedBlockCipher cipher = createCipher(tarOutputStream); startEncryptedFile(fileName, size, tarOutputStream, cipher); //Protect the underlying TAR stream from being closed by the cipher stream final CloseShieldOutputStream closeShieldTarStream = new CloseShieldOutputStream(tarOutputStream); //Setup the encrypting cipher stream final CipherOutputStream chipherStream = new CipherOutputStream(closeShieldTarStream, cipher); //Generate a digest of the pre-encryption data final GeneralDigest digest = this.createDigester(); final DigestOutputStream digestOutputStream = new DigestOutputStream(digest); //Write data to both the digester and cipher final TeeOutputStream teeStream = new TeeOutputStream(digestOutputStream, chipherStream); return new EncryptingOutputStream(teeStream, tarOutputStream, digest); }