AES 256 Encryption Using Scripting?

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

I adapted this AES 256 encryption Java example straight from this link You should be able to adapt the decryption part from my example below.

Working code

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

Thank you so much

1 Like