Package com.github.perdia.crypto
Class Crypto
java.lang.Object
com.github.perdia.crypto.Crypto
Class for handling the encryption and decryption of queries
-
Constructor Summary
ConstructorDescriptionCrypto()
Constructor for the Crypto class, in which the AES key gets hashed using the SHA3-Shake128 algorithm and then stored in an instance variable. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
decrypt
(byte[] bytes) Decrypts the given byte array using AES decryption with the hashed key, also removes the buffer of zeroes at the end of the array.byte[]
encrypt
(byte[] bytes) Encrypts the given byte array using AES encryption with the hashed key, also adds a buffer of zeroes to the end of the array so the length becomes a multiple of 16.
-
Constructor Details
-
Crypto
public Crypto()Constructor for the Crypto class, in which the AES key gets hashed using the SHA3-Shake128 algorithm and then stored in an instance variable.
-
-
Method Details
-
encrypt
public byte[] encrypt(byte[] bytes) Encrypts the given byte array using AES encryption with the hashed key, also adds a buffer of zeroes to the end of the array so the length becomes a multiple of 16.- Parameters:
bytes
- Byte array to be encrypted- Returns:
- Encrypted byte array including the buffer If the key given in the .env file is invalid, an InvalidKeyException is thrown and caught.
- Throws:
AssertionError
- An AssertionError gets thrown if any of the following exceptions get thrown and caught: InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException or BadPaddingException. This is because the following exceptions are made impossible to be thrown by how the code is written.
-
decrypt
public byte[] decrypt(byte[] bytes) Decrypts the given byte array using AES decryption with the hashed key, also removes the buffer of zeroes at the end of the array.- Parameters:
bytes
- Byte array to be decrypted- Returns:
- Decrypted byte array If the padding at the end of the encrypted byte array is incorrect, a BadPaddingException is thrown and caught. If the length of the encrypted byte array is not a multiple of 16, an IllegalBlockSizeException is thrown and caught. This may be because of a server-side error. If the key given in the .env file is invalid, an InvalidKeyException is thrown and caught.
- Throws:
AssertionError
- An AssertionError gets thrown if any of the following exceptions get thrown and caught: InvalidAlgorithmParameterException, NoSuchAlgorithmException or NoSuchPaddingException. This is because the following exceptions are made impossible to be thrown by how the code is written.
-