# AES (SubServers.Bungee)

net.ME1312.SubServers.Bungee.Network

## Class AES

- java.lang.Object
- - net.ME1312.SubServers.Bungee.Network.AES

    public final class AES
    extends java.lang.Object

A class to perform password-based AES encryption and decryption in CBC mode. 128, 192, and 256-bit encryption are supported, provided that the latter two are permitted by the Java runtime's jurisdiction policy files.
The public interface for this class consists of the static methods `encrypt(int, java.lang.String, java.io.InputStream, java.io.OutputStream)` and `decrypt(java.lang.String, java.io.InputStream, java.io.OutputStream)`, which encrypt and decrypt arbitrary streams of data, respectively.

Author:
  dweymouth@gmail.com

### Nested Class Summary

Nested Classes
`static class` `AES.InvalidAESStreamException` Thrown if an attempt is made to decrypt an invalid AES stream.
`static class` `AES.InvalidKeyLengthException` Thrown if an attempt is made to encrypt a stream with an invalid AES key length.
`static class` `AES.InvalidPasswordException` Thrown if an attempt is made to decrypt a stream with an incorrect password.
`static class` `AES.StrongEncryptionNotAvailableException` Thrown if 192- or 256-bit AES encryption or decryption is attempted, but not available on the particular Java platform.

### Constructor Summary

Constructors
Constructor and Description
`AES()`

### Method Summary

All Methods Static Methods Concrete Methods
`static NamedContainer<java.lang.Integer,java.lang.String>` `decrypt(java.lang.String password, byte[] input)` Decrypts a stream of data that was encrypted by `encrypt(int, java.lang.String, java.io.InputStream, java.io.OutputStream)`.
`static int` `decrypt(java.lang.String password, java.io.InputStream input, java.io.OutputStream output)` Decrypts a stream of data that was encrypted by `encrypt(int, java.lang.String, java.io.InputStream, java.io.OutputStream)`.
`static void` `encrypt(int keyLength, java.lang.String password, java.io.InputStream input, java.io.OutputStream output)` Encrypts a stream of data.
`static byte[]` `encrypt(int keyLength, java.lang.String password, java.lang.String input)` Encrypts a stream of data.

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### AES

    public AES()

### Method Detail

#### encrypt

    public static void encrypt(int keyLength,
                               java.lang.String password,
                               java.io.InputStream input,
                               java.io.OutputStream output)
                        throws AES.InvalidKeyLengthException,
                               AES.StrongEncryptionNotAvailableException,
                               java.io.IOException

Encrypts a stream of data. The encrypted stream consists of a header followed by the raw AES data. The header is broken down as follows:

- keyLength: AES key length in bytes (valid for 16, 24, 32) (1 byte)
- salt: pseudorandom salt used to derive keys from password (16 bytes)
- authentication key (derived from password and salt, used to check validity of password upon decryption) (8 bytes)
- IV: pseudorandom AES initialization vector (16 bytes)

Parameters:
  `keyLength` - key length to use for AES encryption (must be 128, 192, or 256)
  `password` - password to use for encryption
  `input` - an arbitrary byte stream to encrypt
  `output` - stream to which encrypted data will be written
Throws:
  `AES.InvalidKeyLengthException` - if keyLength is not 128, 192, or 256
  `AES.StrongEncryptionNotAvailableException` - if keyLength is 192 or 256, but the Java runtime's jurisdiction policy files do not allow 192- or 256-bit encryption
  `java.io.IOException`

#### encrypt

    public static byte[] encrypt(int keyLength,
                                 java.lang.String password,
                                 java.lang.String input)
                          throws java.io.IOException,
                                 AES.StrongEncryptionNotAvailableException,
                                 AES.InvalidKeyLengthException

Encrypts a stream of data. The encrypted stream consists of a header followed by the raw AES data. The header is broken down as follows:

- keyLength: AES key length in bytes (valid for 16, 24, 32) (1 byte)
- salt: pseudorandom salt used to derive keys from password (16 bytes)
- authentication key (derived from password and salt, used to check validity of password upon decryption) (8 bytes)
- IV: pseudorandom AES initialization vector (16 bytes)

Parameters:
  `keyLength` - key length to use for AES encryption (must be 128, 192, or 256)
  `password` - password to use for encryption
  `input` - an arbitrary string to encrypt
Returns:
  string containing the encrypted data
Throws:
  `AES.InvalidKeyLengthException` - if keyLength is not 128, 192, or 256
  `AES.StrongEncryptionNotAvailableException` - if keyLength is 192 or 256, but the Java runtime's jurisdiction policy files do not allow 192- or 256-bit encryption
  `java.io.IOException`

#### decrypt

    public static int decrypt(java.lang.String password,
                              java.io.InputStream input,
                              java.io.OutputStream output)
                       throws AES.InvalidPasswordException,
                              AES.InvalidAESStreamException,
                              java.io.IOException,
                              AES.StrongEncryptionNotAvailableException

Decrypts a stream of data that was encrypted by `encrypt(int, java.lang.String, java.io.InputStream, java.io.OutputStream)`.

Parameters:
  `password` - the password used to encrypt/decrypt the stream
  `input` - stream of encrypted data to be decrypted
  `output` - stream to which decrypted data will be written
Returns:
  the key length for the decrypted stream (128, 192, or 256)
Throws:
  `AES.InvalidPasswordException` - if the given password was not used to encrypt the data
  `AES.InvalidAESStreamException` - if the given input stream is not a valid AES-encrypted stream
  `AES.StrongEncryptionNotAvailableException` - if the stream is 192 or 256-bit encrypted, and the Java runtime's jurisdiction policy files do not allow for AES-192 or 256
  `java.io.IOException`

#### decrypt

    public static NamedContainer<java.lang.Integer,java.lang.String> decrypt(java.lang.String password,
                                                                             byte[] input)
                                                                      throws java.io.IOException,
                                                                             AES.StrongEncryptionNotAvailableException,
                                                                             AES.InvalidAESStreamException,
                                                                             AES.InvalidPasswordException

Decrypts a stream of data that was encrypted by `encrypt(int, java.lang.String, java.io.InputStream, java.io.OutputStream)`.

Parameters:
  `password` - the password used to encrypt/decrypt the string
  `input` - string of encrypted data to be decrypted
Returns:
  the key length for the decrypted string (128, 192, or 256) and the string of decrypted data
Throws:
  `AES.InvalidPasswordException` - if the given password was not used to encrypt the data
  `AES.InvalidAESStreamException` - if the given input stream is not a valid AES-encrypted stream
  `AES.StrongEncryptionNotAvailableException` - if the stream is 192 or 256-bit encrypted, and the Java runtime's jurisdiction policy files do not allow for AES-192 or 256
  `java.io.IOException`