0

I am using pyjnius and have installed Java version 17.0.6 on my ubuntu running on an Raspberry pi 400. Using Python 3.11 and Cython 0.29 is installed. I am running the following code -

Cipher = autoclass('javax.crypto.Cipher')
cipher = Cipher.getInstance(self.AES_CBS_ALGORITHM)
SecretKeySpec = autoclass('javax.crypto.spec.SecretKeySpec')
secretKeySpec = SecretKeySpec(key, self.AES_ALGORITH) # key is a sha 256 aes key in the byte array format
IvParameterSpec = autoclass('javax.crypto.spec.IvParameterSepc')
ivParameterSpec = IvParameterSpec(initial_vector) # initial_vector is a byte array

cipher.init(cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec)
plain_text = cipher.doFinal(plain_text) # plain_text is a byte array message digest

In the above code, I am getting an error at the cipher.init() function

The error I get is - Invalid instance of 'javax/crypto/spec/SecretKeySpec' passed for a 'java/security/cert/Certifcate'

This basically means that for the second parameter I have passed, it is expecting a Certificate class instance instead of SecretKeySpec instance. But, I have checked documentation of the Cipher class in Java 17 and I see the Cipher.init() function that takes Key as the second parameter. It also has another format of Cipher.init() function that takes Certificate as the second parameter.

Please see this URL - https://docs.oracle.com/en/java/javase/17/docs/api/java.base/javax/crypto/Cipher.html

How do I make sure pyjnius considers the Cipher.init() function implementation that takes Key as parameter instead of Certificate?

I had first installed openJDK. I removed that java installation and installed Java 17. I have updated the JAVA_HOME and PATH variables appropriately in the etc/environment file. I restarted the raspberry pi, uninstalled pyjnius and re-installed it again using pip install to make sure it is able to see the new Java 17. But still I get the same error.

My expectation is that the cipher.init() function accepts the SecretKeySpec as the second parameter as per the documentation.

Please help. thank you in advance!

Mrudula Y
  • 1
  • 2

0 Answers0