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