# Encryption outside Singlestore DB and Descryption inside singlestore

**URL:** <https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298>\
**Category:** Security\
**Tags:** pipelines\
**Created:** [March 25, 2022, 5:41pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298 "2022-03-25T17:41:48Z")\
**Posts on this page:** 6\
**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:** [March 25, 2022, 5:41pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/1 "2022-03-25T17:41:48Z")

</div>

Hi,  
I am syncing Data from Oracle DB to Singlestore for reporting thru Kafka. There are a few fields which needs to be encrypted before sending to Kafka. So trying to encrypt the field using AES128 bit encryption method in a Java program and load the data in Singlestore.

Now, I am unable to decrypt the data in Singlestore which was encrypted with same AES128 bit mechanism using the Java program.

Here is the Java code for AES128 bit encryption.

private static final String encryptionKey = “A1A2A3A4A5A6CAFE”;  
private static final String characterEncoding = “UTF-8”;  
private static final String cipherTransformation = “AES/ECB/PKCS5PADDING”;  
private static final String aesEncryptionAlgorithem = “AES”;

```
/**
 * Method for Encrypt Plain String Data
 * @param plainText
 * @return encryptedText
 */
public static String encrypt(String plainText) {
    String encryptedText = "";
    try {
        Cipher cipher = Cipher.getInstance(cipherTransformation);
        byte[] key = encryptionKey.getBytes(characterEncoding);
        SecretKeySpec secretKey = new SecretKeySpec(key, aesEncryptionAlgorithem);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherText = cipher.doFinal(plainText.getBytes("UTF-8"));
        Base64.Encoder encoder = Base64.getEncoder();
        encryptedText = encoder.encodeToString(cipherText);

    } catch (Exception E) {
         System.err.println("Encrypt Exception : "+E.getMessage());
    }
    return encryptedText;
}
```

---

<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:** [March 25, 2022, 7:05pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/2 "2022-03-25T19:05:30Z")

</div>

Is it related to the issue identified on your other post?

> [@Aes\_encrypt and aes\_decrypt](https://www.singlestore.com/forum/t/aes-encrypt-and-aes-decrypt/4280):
>
> select hex(AES\_ENCRYPT(‘secret message’, ‘A1A2A3A4A5A6CAFE’)); It returns → 71990030B38EB0626A4F56B32E103A8F SELECT AES\_DECRYPT(unhex(‘71990030B38EB0626A4F56B32E103A8F’), ‘A1A2A3A4A5A6CAFE’); It returns → 73 65 63 72 65 74 20 6d 65 73 73 61 67 65 Just following the site example and not working as expected. Any isssue? Singlestore version → 7.6.11

---

<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:** [March 25, 2022, 7:25pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/3 "2022-03-25T19:25:57Z")

</div>

Nope, the encrypted value seems to be diff b/w the Java approach and singlestore. Also, getting error on calling decrypt.

---

<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:** [March 25, 2022, 8:51pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/4 "2022-03-25T20:51:42Z")

</div>

Do you have a sample java program to encrypt outside Singlestore that can be Decrypted in Singlestore.

---

<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:** [March 27, 2022, 12:55pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/5 "2022-03-27T12:55:16Z")

</div>

Sorry, its an issue from my side and Singlestore is working as expected.

---

<div class="post-metadata">

**Author:** ![MariaSilverhardt](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/mariasilverhardt/32/828_2.png) [@MariaSilverhardt](https://www.singlestore.com/forum/u/MariaSilverhardt)\
**Post date:** [March 28, 2022, 3:54pm UTC](https://www.singlestore.com/forum/t/encryption-outside-singlestore-db-and-descryption-inside-singlestore/4298/6 "2022-03-28T15:54:12Z")

</div>

Glad to hear it has been resolved Baskar! 🎉 Thanks for keeping us updated!
