# AES Encryption using Scripting

**URL:** <https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554>\
**Category:** Ignition\
**Created:** [May 1, 2019, 12:53pm UTC](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554 "2019-05-01T12:53:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theerthraj.munusamy](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@theerthraj.munusamy](https://forum.inductiveautomation.com/u/theerthraj.munusamy)\
**Post date:** [May 1, 2019, 12:53pm UTC](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/1 "2019-05-01T12:53:35Z")

</div>

Hi,

I need to encrypt a string using AES 128. Is there a way to do it within Ignition? I tried with PyCrypto library, it didn’t work.

Thanks,

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [May 1, 2019, 2:11pm UTC](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/2 "2019-05-01T14:11:50Z")

</div>

From what I see, PyCrypto uses C libraries and you need something compatible with Jython.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2019, 3:30pm UTC](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/3 "2019-05-01T15:30:09Z")

</div>

You could translate the Java code here into Jython _fairly_ easily; drop type information, curly braces, and add qualified imports (`from package import Class`) at the top of the file:

> **[Java AES Encryption Decryption Example - HowToDoInJava](https://howtodoinjava.com/java/java-security/java-aes-encryption-example/)**
>
> Java support many secure encryption algorithms but some of them are weak to be used in security-intensive applications. For example, the Data Encryption Standard (DES) encryption algorithm is considered highly insecure; messages encrypted using DES...

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 1, 2019, 3:47pm UTC](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/4 "2019-05-01T15:47:51Z")

</div>

I had a similar Idea.

Found this:  
[Java program to Encrypt/Decrypt String Using AES 128](https://www.includehelp.com/java-programs/encrypt-decrypt-string-using-aes-128-bits-encryption-algorithm.aspx)

And converted it to this:

```auto
from java.util import Base64
from javax.crypto import Cipher
from javax.crypto.spec import IvParameterSpec
from javax.crypto.spec import SecretKeySpec

import java.lang.Exception
logger = system.util.getLogger('encryptionLogger')

_encryptionKey = 'ABCDEFGHIJKLMNOP'
_characterEncoding = 'UTF-8'
_cipherTransformation = 'AES/CBC/PKCS5PADDING'
_aesEncryptionAlgorithem = 'AES'

def encrypt(plainText):
	encryptedText = ''
	try:
		cipher = Cipher.getInstance(_cipherTransformation)
		key = _encryptionKey.encode('utf-8')
		secretKey = SecretKeySpec(key,'AES')
		ivParameterSpec = IvParameterSpec(key)
		cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivParameterSpec)
		cipherText = cipher.doFinal(plainText.encode('UTF-8'))
		encoder = Base64.getEncoder()
		encryptedText = encoder.encodeToString(cipherText)
	except Exception, e:
		logger.warnf('Encrypt Exception: %s',e)
	
	return encryptedText
	

def decrypt(encryptedText):
	decryptedText = ''
	
	try:
		cipher = Cipher.getInstance(_cipherTransformation)
		key = _encryptionKey.encode('utf-8')
		secretKey = SecretKeySpec(key,'AES')
		ivParameterSpec = IvParameterSpec(key)
		cipher.init(Cipher.DECRYPT_MODE,secretKey,ivParameterSpec)
		decoder = Base64.getDecoder()
		cipherText = decoder.decode(encryptedText.encode('UTF-8'))
		decryptedText = ''.join(map(chr,cipher.doFinal(cipherText)))
	except Exception, e:
		logger.warnf('Decrypt Exception: %s',e)
	
	return decryptedText

```

ran the following as a test:

```auto
encryptedString = encrypt('test string')
print encryptedString
print decrypt(encryptedString)

```

Output was:

```auto
>>> 
HZX0KjZSzXpdUOinZmq45g==
test string
>>> 

```
