Class RawEncryption
-
- All Implemented Interfaces:
public class RawEncryption
This class provides methods to encrypt and decrypt byte streams and byte arrays. The algorithm used to encrypt and decrypt is AES with GCM and no padding. The easiest way to generate and save encryption keys is by using EncryptionUtil, but it is not required to do so.
-
-
Method Summary
Modifier and Type Method Description static void
encryptFile(@NonNull() String unencryptedFilePath, @NonNull() String encryptedFilePath, @NonNull() Array<byte> encryptionKey)
Encrypts a file and saves the encrypted contents to another file. static void
decryptFile(@NonNull() String encryptedFilePath, @NonNull() String decryptedFilePath, @NonNull() Array<byte> encryptionKey)
Decrypts a file and saves the decrypted contents to another file. static void
encryptStream(@NonNull() InputStream inputStream, @NonNull() OutputStream outputStream, @NonNull() Array<byte> encryptionKey)
Encrypts the contents of a stream and passes the encrypted contents to another stream. static void
decryptStream(@NonNull() InputStream inputStream, @NonNull() OutputStream outputStream, @NonNull() Array<byte> encryptionKey)
Decrypts the contents of a stream and passes the decrypted contents to another stream. static Array<byte>
encrypt(@NonNull() Array<byte> data, @NonNull() Array<byte> encryptionKey)
Encrypts the contents of the given byte array with the given encryption key. static Array<byte>
decrypt(@NonNull() Array<byte> encryptedData, @NonNull() Array<byte> encryptionKey)
Decrypts the contents of the given byte array with the given encryption key. -
-
Method Detail
-
encryptFile
static void encryptFile(@NonNull() String unencryptedFilePath, @NonNull() String encryptedFilePath, @NonNull() Array<byte> encryptionKey)
Encrypts a file and saves the encrypted contents to another file.
- Parameters:
unencryptedFilePath
- The complete filepath to the original, unencrypted file.encryptedFilePath
- The complete filepath to where the encrypted file should be written.encryptionKey
- The key used to encrypt the file, and which can be used to decrypt the file later.
-
decryptFile
static void decryptFile(@NonNull() String encryptedFilePath, @NonNull() String decryptedFilePath, @NonNull() Array<byte> encryptionKey)
Decrypts a file and saves the decrypted contents to another file.
- Parameters:
encryptedFilePath
- The complete filepath to the encrypted file.decryptedFilePath
- The complete filepath to where the decrypted file should be written.encryptionKey
- The key used to decrypt the file, and which can be used to decrypt the file later.
-
encryptStream
static void encryptStream(@NonNull() InputStream inputStream, @NonNull() OutputStream outputStream, @NonNull() Array<byte> encryptionKey)
Encrypts the contents of a stream and passes the encrypted contents to another stream. This method can be used anywhere streams are used. For example, a file can be encrypted by passing this method a FileInputStream and a FileOutputStream. This method does not close any streams, that is the responsibility of the caller.
- Parameters:
inputStream
- The stream where unencrypted bytes will be read from.outputStream
- The stream where encrypted bytes will be written to.encryptionKey
- The key used to encrypt the bytes, and which can be used to decrypt the encrypted bytes later.
-
decryptStream
static void decryptStream(@NonNull() InputStream inputStream, @NonNull() OutputStream outputStream, @NonNull() Array<byte> encryptionKey)
Decrypts the contents of a stream and passes the decrypted contents to another stream. This method can be used anywhere streams are used. For example, a file can be decrypted by passing this method a FileInputStream and a FileOutputStream. This method does not close any streams, that is the responsibility of the caller.
- Parameters:
inputStream
- The stream where encrypted bytes will be read from.outputStream
- The stream where decrypted bytes will be written to.encryptionKey
- The key used to decrypt the bytes.
-
encrypt
@NonNull() static Array<byte> encrypt(@NonNull() Array<byte> data, @NonNull() Array<byte> encryptionKey)
Encrypts the contents of the given byte array with the given encryption key.
- Parameters:
data
- The unencrypted bytes.encryptionKey
- The key that will be used to encrypt the data, and can be used later to decrypt the encrypted bytes.- Returns:
The encrypted bytes.
-
decrypt
@NonNull() static Array<byte> decrypt(@NonNull() Array<byte> encryptedData, @NonNull() Array<byte> encryptionKey)
Decrypts the contents of the given byte array with the given encryption key.
- Parameters:
encryptedData
- The encrypted bytes.encryptionKey
- The key used to decrypt the bytes.- Returns:
The decrypted bytes.
-
-
-
-