Skip to content
Advertisement

unable to use OAEP decryption in python

Here’s my code:

JavaScript

Here’s the error I get:

JavaScript

What am I doing wrong? I’m running Python 3.8.3.

Advertisement

Answer

The mgfunc parameter (3rd parameter) for the mask generation function is incorrectly specified in the posted code. According to the description of Crypto.Cipher.PKCS1_OAEP.new():

mgfunc (callable) – A mask generation function that accepts two parameters: a string to use as seed, and the lenth of the mask to generate, in bytes. If not specified, the standard MGF1 consistent with hashAlgo is used (a safe choice).

where hashAlgo (2nd parameter) denotes the OAEP digest.

The use of MGF1 with an explicitly specified digest is described in the documentation in the context of Crypto.Signature.pss, see Crypto.Signature.pss.MGF1() and Crypto.Signature.pss.new(). However, MGF1 is also available in Crypto.Cipher.PKCS1_OAEP (where it is imported from Crypto.Signature.pss in the source code).

Since by default MGF1 is used with the OAEP digest specified in the 2nd parameter (hashAlgo), an explicit specification of the mask generation function or MGF1 is necessary whenever the two digests differ, i.e., as in this example, where the OAEP digest is MD5, and the MGF1 digest is SHA1.

If the following line is used in the code:

JavaScript

then the decryption works and b’test’ is returned as the decrypted value.

Please note that MD5 and SHA1 are deprecated. RFC8017 recommends only SHA-1 and SHA-2 for RSAES-OAEP.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement