我正在使用Python +Pycrypto叉中的以下代码来使用RSA PKCS#1 OAEP SHA256 (RSA/ECB/OAEPWithSHA-256AndMGF1Padding)加密消息:
from Crypto.Cipher import PKCS1_OAEP
from Cryptodome.Hash import SHA256
cipher = PKCS1_OAEP.new(key=self.key, hashAlgo=SHA256))
ciphertext = cipher.encrypt(cek)并使用以下Java代码对其进行解密:
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cek = cipher.doFinal(ciphertext);然而,我得到了:
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadOAEP(RSAPadding.java:499)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:293)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)发布于 2018-05-17 15:05:02
在Sun中,RSA/ECB/OAEPWithSHA-256AndMGF1Padding实际上意味着:
另一方面,在使用PKCS1_OAEP.new(hashAlgo=SHA256)时,Pycrypto (包括Pycryptodome)假设如下
为了使Pycrypto与Sun兼容,您需要通过传递MGF1参数来配置Pycrypto的OAEP MGF1函数以使用mgfunc:
from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.Hash import SHA256, SHA1
from Cryptodome.Signature import pss
cipher = PKCS1_OAEP.new(key=self.key, hashAlgo=SHA256, mgfunc=lambda x,y: pss.MGF1(x,y, SHA1))
ciphertext = cipher.encrypt(cek)值得注意的是,根据breaking down RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING的说法,BouncyCastle为Hash和MGF1使用SHA256的方式与Pycrypto一样。
https://stackoverflow.com/questions/50394730
复制相似问题