diff --git a/app/src/main/java/lightcontainer/utils/AES_GCM.java b/app/src/main/java/lightcontainer/utils/AES_GCM.java new file mode 100644 index 0000000..5aeed9d --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/AES_GCM.java @@ -0,0 +1,126 @@ +package lightcontainer.utils; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Base64; + +public class AES_GCM { + // Constants + public static final int AES_KEY_SIZE = 256; + public static final int GCM_IV_LENGTH = 16; + public static final int GCM_TAG_LENGTH = 16; + + public static void main(String[] args) throws Exception + { + // Text pour test : + String plainText = "salut fils de pute"; + + String IV = generateIV(); + String key = generateSecretKey(); + + System.out.println("Original Text : " + plainText); + + byte[] cipherText = encrypt(plainText.getBytes(), key, IV); + System.out.println("Encrypted Text : " + Base64.getEncoder().encodeToString(cipherText)); + + String decryptedText = decrypt(cipherText, key, IV); + System.out.println("DeCrypted Text : " + decryptedText); + } + + /** + * Decoder to decode base64 vector to byte vector. + * @param base64Vector A base64 encoded vector. + * @return Byte vector. + */ + private static byte[] decodeBase64(String base64Vector) { + Base64.Decoder b64Decoder = Base64.getDecoder(); + return b64Decoder.decode(base64Vector); + } + + /** + * Encoder to encode vector to base64 string. + * @param rawVector A raw vector. + * @return A base64 encoded vector. + */ + private static String encodeBase64(byte[] rawVector) { + Base64.Encoder b64Encoder = Base64.getEncoder(); + return b64Encoder.encodeToString(rawVector); + } + + /** + * Generate a secret key base64 encoded. + * @return New Secret key b64 encoded. + */ + public static String generateSecretKey() throws NoSuchAlgorithmException { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(AES_KEY_SIZE); + SecretKey key = keyGenerator.generateKey(); + return encodeBase64(key.getEncoded()); + } + + /** + * Generate an IV (initialisation vector) base64 encoded. + * @return New generated IV b64 encoded. + */ + public static String generateIV() { + byte[] IV = new byte[GCM_IV_LENGTH]; + SecureRandom random = new SecureRandom(); + random.nextBytes(IV); + return encodeBase64(IV); + } + + /** + * Encrypt, with AES GCM. + * @param plainContent Content to encrypt. + * @param key Base64 encoded secret key. + * @param IV Base64 encoded vector. + * @return The encrypted cipherContent. + */ + public static byte[] encrypt(byte[] plainContent, String key, String IV) throws Exception + { + // Get Cipher Instance + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); + + // Create SecretKeySpec + SecretKeySpec keySpec = new SecretKeySpec(decodeBase64(key), "AES"); + + // Create GCMParameterSpec + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, decodeBase64(IV)); + + // Initialize Cipher for ENCRYPT_MODE + cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); + + // Perform Encryption + return cipher.doFinal(plainContent); + } + + /** + * Decrypt, with AES GCM. + * @param cipherContent The encrypted cipherContent + * @param key Base64 encoded secret key. + * @param IV Base64 encoded vector. + * @return The decrypted plainContent. + */ + public static String decrypt(byte[] cipherContent, String key, String IV) throws Exception + { + // Get Cipher Instance + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); + + // Create SecretKeySpec + SecretKeySpec keySpec = new SecretKeySpec(decodeBase64(key), "AES"); + + // Create GCMParameterSpec + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH*8, decodeBase64(IV)); + + // Initialize Cipher for DECRYPT_MODE + cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec); + + // Perform Decryption + return new String(cipher.doFinal(cipherContent)); + } +} \ No newline at end of file