# 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:** 1\
**Showing post:** 3

<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")

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324)._
