# Recommended method for encrypting passwords before storing them in a database

**URL:** <https://forum.inductiveautomation.com/t/recommended-method-for-encrypting-passwords-before-storing-them-in-a-database/110158>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [August 25, 2025, 6:30pm UTC](https://forum.inductiveautomation.com/t/recommended-method-for-encrypting-passwords-before-storing-them-in-a-database/110158 "2025-08-25T18:30:29Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Adriano\_Generoso\_da](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/adriano_generoso_da/32/43895_2.png) [@Adriano\_Generoso\_da](https://forum.inductiveautomation.com/u/Adriano_Generoso_da)\
**Post date:** [September 1, 2025, 12:14pm UTC](https://forum.inductiveautomation.com/t/recommended-method-for-encrypting-passwords-before-storing-them-in-a-database/110158/11 "2025-09-01T12:14:42Z")

</div>

I have one example using SHA256 using Java Libraries, so we have the Secret and Salt to encrypt and decrypt

> 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, SECRET\_KEY, SALT):  
> encryptedText = ''  
> try:  
> iv = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
> ivspec = IvParameterSpec(iv)
> 
> ```python
> 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
> 
> ```
> 
> def decrypt(encryptedText, SECRET\_KEY, SALT):  
> decryptedText = ''
> 
> ```python
> 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.DECRYPT_MODE,secretKey,ivspec)
> 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
> 
> ```

Thanks for the posts:  
[AES Encryption using Scripting - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/aes-encryption-using-scripting/24554/3)  
[AES 256 Encryption Using Scripting? - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/aes-256-encryption-using-scripting/53324/3)

---

_[View the full topic](https://forum.inductiveautomation.com/t/recommended-method-for-encrypting-passwords-before-storing-them-in-a-database/110158)._
