Class EncryptingOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class EncryptingOutputStream
    extends java.io.OutputStream
    An output stream that encrypts data to another output stream. A lightweight yet secure hybrid encryption scheme is used. A random symmetric key is generated and encrypted using the receiver's public key. The supplied data is then encrypted using the symmetric key and sent to the underlying stream on a streaming basis. An HMAC checksum is also computed on an ongoing basis and appended to the output when the stream is closed. This class can be used in conjunction with DecryptingInputStream to transport data securely.
    • Constructor Summary

      Constructors 
      Constructor Description
      EncryptingOutputStream​(java.io.OutputStream wrapped, java.security.PublicKey receiverKey, java.lang.String algorithm, java.security.SecureRandom rand, int strength, java.lang.String mode, java.lang.String padding)
      Constructs an EncryptingOutputStream.
      EncryptingOutputStream​(java.io.OutputStream wrapped, java.security.PublicKey receiverKey, java.security.SecureRandom rand)
      Constructs an EncryptingOutputStream using default symmetric encryption parameters.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      void write​(byte[] b)  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EncryptingOutputStream

        public EncryptingOutputStream​(java.io.OutputStream wrapped,
                                      java.security.PublicKey receiverKey,
                                      java.security.SecureRandom rand)
                               throws java.io.IOException,
                                      java.security.NoSuchAlgorithmException,
                                      javax.crypto.NoSuchPaddingException,
                                      java.security.InvalidKeyException,
                                      javax.crypto.IllegalBlockSizeException,
                                      javax.crypto.BadPaddingException
        Constructs an EncryptingOutputStream using default symmetric encryption parameters.
        Parameters:
        wrapped - outputstream to store the encrypted data
        receiverKey - the receiver's public key for encrypting the symmetric key
        rand - a secure source of randomness
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
        java.security.NoSuchAlgorithmException - the no such algorithm exception
        javax.crypto.NoSuchPaddingException - the no such padding exception
        java.security.InvalidKeyException - the invalid key exception
        javax.crypto.BadPaddingException - the bad padding exception
        javax.crypto.IllegalBlockSizeException - the illegal block size exception
      • EncryptingOutputStream

        public EncryptingOutputStream​(java.io.OutputStream wrapped,
                                      java.security.PublicKey receiverKey,
                                      java.lang.String algorithm,
                                      java.security.SecureRandom rand,
                                      int strength,
                                      java.lang.String mode,
                                      java.lang.String padding)
                               throws java.io.IOException,
                                      java.security.NoSuchAlgorithmException,
                                      javax.crypto.NoSuchPaddingException,
                                      java.security.InvalidKeyException,
                                      javax.crypto.IllegalBlockSizeException,
                                      javax.crypto.BadPaddingException
        Constructs an EncryptingOutputStream.
        Parameters:
        wrapped - outputstream to store the encrypted data
        receiverKey - the receiver's public key for encrypting the symmetric key
        algorithm - symmetric encryption algorithm (e.g. "AES")
        rand - a secure source of randomness
        strength - the key size in bits (e.g. 128)
        mode - encryption mode (e.g. "CBC")
        padding - padding scheme (e.g. "PKCS5PADDING")
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
        java.security.NoSuchAlgorithmException - the no such algorithm exception
        javax.crypto.NoSuchPaddingException - the no such padding exception
        java.security.InvalidKeyException - the invalid key exception
        javax.crypto.BadPaddingException - the bad padding exception
        javax.crypto.IllegalBlockSizeException - the illegal block size exception
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException