# AES 256 GCM with IV vector - Encryption outside Singlestore DB and Decryption inside Singlestore

**URL:** <https://www.singlestore.com/forum/t/aes-256-gcm-with-iv-vector-encryption-outside-singlestore-db-and-decryption-inside-singlestore/6923>\
**Category:** Uncategorized\
**Created:** [June 29, 2023, 4:50am UTC](https://www.singlestore.com/forum/t/aes-256-gcm-with-iv-vector-encryption-outside-singlestore-db-and-decryption-inside-singlestore/6923 "2023-06-29T04:50:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![baskar.sks](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/b/eada6e/32.png) [@baskar.sks](https://www.singlestore.com/forum/u/baskar.sks)\
**Post date:** [June 29, 2023, 4:50am UTC](https://www.singlestore.com/forum/t/aes-256-gcm-with-iv-vector-encryption-outside-singlestore-db-and-decryption-inside-singlestore/6923/1 "2023-06-29T04:50:26Z")

</div>

Hi,

I am on version 7.9.X and using AES 256 GCM method of encryption with IV vector for encrypting data outside singlestore using java code.

```auto
public static String encrypt(final String plainText, final String encryptionKey) {
		try {
			final Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
			final byte[] key = encryptionKey.getBytes(CHARACTER_ENCODING);
			final SecretKey secretKey = new SecretKeySpec(key, AES_ENCRYPTION_ALGORITHM);

			// Generate a random nonce (IV)
			byte[] nonce = new byte[GCM_NONCE_LENGTH];
			SecureRandom secureRandom = new SecureRandom();
			secureRandom.nextBytes(nonce);
			
			// Initialize the cipher with GCM parameters
			GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmParameterSpec);

			final byte[] cipherText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

			// Encode the IV and ciphertext as Base64
			final Base64.Encoder encoder = Base64.getEncoder();
			String encodedIV = encoder.encodeToString(nonce);
			String encodedCiphertext = encoder.encodeToString(cipherText);

			// Concatenate the IV and ciphertext
			return encodedIV + ":" + encodedCiphertext;
		} catch (final Exception e) {
			LOGGER.info("An error occurred in encrypt method. Exception message is '{}'", e.getMessage());
			throw new RuntimeException(e);
		}
	}

```

It returns base64 encoded dynamic IV vector and the base64 encoded encrypted text.

Getting NULL when trying to decrypt inside singlestore using the query below.

`SELECT AES_DECRYPT(BASE_64('DUTJdkWfcjGQS73u6DQe/9fCYg2edr71dOBtDTjvBC4T1NUFYojN'), 'A1A2A3A4A5A6CAFE',BASE_64( '+pYvqdmYAjBS6N43'), 'aes-256-gcm'):>TEXT;`

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [June 30, 2023, 10:24pm UTC](https://www.singlestore.com/forum/t/aes-256-gcm-with-iv-vector-encryption-outside-singlestore-db-and-decryption-inside-singlestore/6923/2 "2023-06-30T22:24:09Z")

</div>

Looks like you are trying to decrypt with a key that is different from what was used to encrypt. Or maybe your BASE\_64 function is returning NULL. E.g.

```auto
singlestore> select aes_decrypt('foo','bar', null, 'aes-256-gcm');
+-----------------------------------------------+
| aes_decrypt('foo','bar', null, 'aes-256-gcm') |
+-----------------------------------------------+
| NULL |
+-----------------------------------------------+
1 row in set (0.06 sec)

singlestore> set @key = 'bar';
Query OK, 0 rows affected (0.02 sec)

singlestore> set @ct = aes_encrypt('foo', @key, null, 'aes-256-gcm');
Query OK, 0 rows affected (0.01 sec)

singlestore> select aes_decrypt(@ct, @key, null, 'aes-256-gcm');
+---------------------------------------------+
| aes_decrypt(@ct, @key, null, 'aes-256-gcm') |
+---------------------------------------------+
| foo |
+---------------------------------------------+
1 row in set (0.00 sec)

```
