pythonencryptionaesjcekspyjks

How to use a JCEKS Keystore in Python for AES Encryption?


I have a JCEKS file that contains a secret entry. I'm supposed to use this secret entry from the key store and use that to perform an AES encryption using Python.

I was able to load the KeyStore file in Python using the pyjks library in Python.

I'm able to view my secret entries by trying the following -

import jks

key_store = jks.KeyStore.load("path/to/keystore", "keystorepass")

key_store.entries

which return the following value

{
    'mysecretentry': <jks.jks.SecretKeyEntry at 0x7fd676e65130>
}

But I'm not sure how to access this key so that I can use this as my key in AES encryption

from Crypto.Cipher import AES

cipher = AES.new(mysecretentry, AES.MODE_CBC, iv)

Solution

  • We can get the secure key stored in the Keystore by simply

    secure_key = key_store.entries['mysecretentry'].__getattr__('key')
    

    This would return something like -

    b"^\x88H\x98\xda(\x04qQ\xd0\xe5o\x8d\xc6)'s`=\rj\xab\xbd\xd6*\x11\xefr\x1d\x15B\xd8"
    
    

    The above secure_key can be used for AES Encryption