# AES 256 Encryption Using Scripting?

**URL:** <https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [November 22, 2021, 3:08am UTC](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324 "2021-11-22T03:08:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shanmukharao.C](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/shanmukharao.c/32/29664_2.png) [@Shanmukharao.C](https://forum.inductiveautomation.com/u/Shanmukharao.C)\
**Post date:** [November 22, 2021, 3:08am UTC](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324/1 "2021-11-22T03:08:18Z")

</div>

Hi,

I need to encrypt a string using AES 256. Is there a way to do it within Ignition?  
I found 128 bit Encryption code and it's working fine. could any body Suggest me what the code modification need to be done for get 256bit Encryption ???.

> [@AES Encryption using Scripting](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/3):
>
> 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:

---

<div class="post-metadata">

**Author:** ![Shanmukharao.C](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/shanmukharao.c/32/29664_2.png) [@Shanmukharao.C](https://forum.inductiveautomation.com/u/Shanmukharao.C)\
**Post date:** [November 22, 2021, 3:09am UTC](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324/2 "2021-11-22T03:09:47Z")

</div>

**#AES 128 Bit Script**  
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
```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 23, 2021, 3:49pm UTC](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324/3 "2021-11-23T15:49:02Z")

</div>

I adapted this AES 256 encryption Java example straight from this [link](https://howtodoinjava.com/java/java-security/aes-256-encryption-decryption/) You should be able to adapt the decryption part from my example below.

Working code

```auto
from javax.crypto import Cipher
from javax.crypto import SecretKey
from javax.crypto import SecretKeyFactory
from javax.crypto.spec import IvParameterSpec
from javax.crypto.spec import PBEKeySpec
from javax.crypto.spec import SecretKeySpec
from java.nio.charset import StandardCharsets
from java.security.spec import KeySpec
from java.util import Base64

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

SECRET_KEY = 'one_key_to_rule_them_all'
SALT = 'salt_of_the_earth'

def encrypt(strToEncrypt):
	encryptedText = ''
	try:
		iv = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
		ivspec = IvParameterSpec(iv)
		
		factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
		spec = PBEKeySpec(list(SECRET_KEY), SALT.encode("UTF-8"), 65536, 256)
		tmp = factory.generateSecret(spec)
		secretKey = SecretKeySpec(tmp.getEncoded(), "AES")	
		cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
		cipherText = cipher.doFinal(strToEncrypt.encode("UTF-8"))
		encoder = Base64.getEncoder()
		encryptedText = encoder.encodeToString(cipherText)
		
	except Exception, e:
		logger.warnf("Encrypt Exception: %s",e)
		
	return encryptedText

print encrypt("foo")

```

---

<div class="post-metadata">

**Author:** ![Shanmukharao.C](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/shanmukharao.c/32/29664_2.png) [@Shanmukharao.C](https://forum.inductiveautomation.com/u/Shanmukharao.C)\
**Post date:** [November 24, 2021, 5:42am UTC](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324/4 "2021-11-24T05:42:54Z")

</div>

Thank you so much
