A cryptographic library that aims to ease down the process of symmetric encryption/decryption for the developers.
Clone the project
git clone https://link-to-project
Go to the project directory
cd my-project
Install dependencies
mvn clean install
-
Throught the library, the algorithms are used in the specified notation "AES_GCM_128_NoPadding". The breakdown of which is Algorithm+Mode+KeyLenght+Padding.
-
The library uses safe defaults for the creation of Initialization Vector (IV) during encryption, which is returned back to the user for further usage (decryption).
-
The default Algorithm of the library, unless specified by the user, is "AES_GCM_128_NoPadding". If you dont specify the algorithm in parameter of the encryption/decryption builder, it will automatically pick the DEFAULT one.
SymmetricBuilder.encryption()
SymmetricBuilder.decryption()
-
Enum class SymmetricAlgorithm contains a list of the algorithms that are supported currently by the library, provided that they are set as SECURE in the configuration file.
-
When you dont want to use the DEFAULT algorithm for encryption/decryption purposes, please make sure to specify the correct ALGORITHM from the SymmetricAlgorithm class while creating the builder.
SymmetricBuilder.encryption(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding)
SymmetricBuilder.decryption(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding)
- Only the Algorithms in SymmetricAlgorithm class are currently supported. The algorithms in SymmetricAlgorithm class must also be declared as secure in the applications.yml file when extending the library.
In order to make sure you have seamless experience while encryption and decryption it is very IMPORTANT to make sure the encoding and decoding is done correctly. It should be done everytime you convert from String to bytes[] and vice versa.
- When providing an input to the builder for Encryption, make sure to ENCODE it using the correct encoding format.
byte[] plainText = "Hello World 121@#".getBytes(StandardCharsets.UTF_8);
OR (As the default encoding in java is UTF-8)
byte[] plainText = "Hello World 121@#".getBytes();
- When you get the plainText bytes[] back after the Decryption process, simlary it should be DECODED to get back the correct plain text.
new String(plainTextBytes[] , StandardCharsets.UTF_8);
- You want to geneate a symmetric key using the library defaults?
SecretKey secretKey = KeyGenerator.generateSymmetricKey();
- You want to geneate a symmetric key specifying an algorithm for which the you want to use the key?
SecretKey secretKey = KeyGenerator.generateSymmetricKey(SymmetricAlgorithm.AES_GCM_128_NoPadding);
- You want to encrypt/decrypt the data using the Safe Defaults from the library without worrying about the Safe Algorithm, Key?
byte[] plainText = "Hello World".getBytes(StandardCharsets.UTF_8);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption()
.generateKey()
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption()
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();
- You want to encrypt/decrypt the data using the Safe Default Algorithm from the library, but you want to specify the key yourself? [Allowed but not Recommended! Should only be used if the secure key is already generated, Prefer using the Library Default Key Generation]
Example1: Generating the key yourself, and loading any key using the loadKey method
byte[] plainText = "Hello World 121@#".getBytes(StandardCharsets.UTF_8);
byte[] key = new byte[16];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(key);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption()
.loadKey(key)
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption()
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();
Example2: Generating the key from the library, and loading any key using the loadKey method [Fine, but PREFFERED is to use the generateKey method]
byte[] plainText = "Hello World 121@#".getBytes(StandardCharsets.UTF_8);
byte[] key = SymmetricKeyGenerator.generateSymmetricKey(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding)
.loadKey(key)
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption()
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();
- You want to encrypt/decrypt the data providing a specific Algorithm from the library, but you want to use the key generated by the library ?
byte[] plainText = "1232F #$$^%$^ Hello World".getBytes(StandardCharsets.UTF_8);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption(SymmetricAlgorithm.AES_GCM_256_NoPadding)
.generateKey()
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption(symmetricCipher.symmetricAlgorithm())
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();
- You want to encrypt/decrypt the data providing a specific Algorithm from the library and providing the Key ?
byte[] plainText = "Hello World JCA WRAPPER".getBytes(StandardCharsets.UTF_8);
SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.AES_GCM_192_NoPadding;
SecretKey secretKey = KeyGenerator.generateSymmetricKey(symmetricAlgorithm);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption(symmetricAlgorithm)
.generateKey()
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption(symmetricCipher.symmetricAlgorithm())
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();
- You want to encrypt/decrypt the data using AES_GCM and also want to sepcify the Associated Data ?
byte[] plainText = "Hello World JCA WRAPPER Using GCM With AEAD".getBytes(StandardCharsets.UTF_8);
byte[] associatedData = "I am associated data".getBytes(StandardCharsets.UTF_8);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption(SymmetricAlgorithm.AES_GCM_128_NoPadding)
.generateKey()
.plaintext(plainText, associatedData)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption(symmetricCipher.symmetricAlgorithm())
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext(), associatedData)
.decrypt();
- You want to encrypt/decrypt the data using AES_CBC algorithm ?
byte[] plainText = "TESTING CBC 128 With PKCS5 PADDING".getBytes(StandardCharsets.UTF_8);
SymmetricCipher symmetricCipher =
SymmetricBuilder.encryption(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding)
.generateKey()
.plaintext(plainText)
.encrypt();
SymmetricPlain symmetricPlain =
SymmetricBuilder.decryption(SymmetricAlgorithm.AES_CBC_128_PKCS5Padding)
.key(symmetricCipher.key())
.iv(symmetricCipher.iv())
.cipherText(symmetricCipher.ciphertext())
.decrypt();