我正在尝试从已以RSA格式加密的DER编码文件中解密AES密钥。使用以下代码:
with open(outfilename, "rb") as outfilename:
outfileread=outfilename.read()
#this next line decodes der-encoded file into a pyasn1 object (uses pyasn1 package)
content, rest= decode(outfileread, asn1Spec=rfc2315.ContentInfo())
content2, rest=decode(content['content'], asn1Spec=rfc2315.EnvelopedData())
lenencryptedkey=len(content2['recipientInfos'][0]['encryptedKey'])
print(lenencryptedkey)
encryptedsessionkey=content2['recipientInfos'][0]['encryptedKey']
from cryptography.hazmat.primitives.asymmetric import padding
#this next line is a command to decrypt the encrypted AES session key with the private key.
decryptedsessionkey=private_key.decrypt(bytes(encryptedsessionkey), padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),algorithm=hashes.SHA256(), label=None))
我得到以下结果:
256
<cdata 'unsigned char[]' owning 256 bytes>
<cdata 'size_t *' owning 4 bytes>
b'\x1b9j\xf2\xd3\xa1\xec\xa9V!&,\x85\xfd\x11\x83V\x16\xfc^\x1d4,u* \x82\xddU\x9a#\xc8\xf1\x1c!\xd6\x8b\x8dS\x17\xc7!\xc1\xd7\xeb\xa7\xa1\xc5\xbe\xf8\xee\x15\xd4(\xdat\xc5\x13\xa6\x147\xd6\xba~k\xa4(e@\xecl\x06\x80\x91\xb5\xa6\x11\xea6\xc0\xaa\xf4K0U\xfd\xdb\xdf\xe4\x0fTr\xaa\x0c\x1d\xaa\xa6\x9bg\xff[\xac>\x9d\xe1\x7f\\\x12\xf7\xbdL\x86\xadMPSA0\x80H\xc8/)\xcfq\xc3\xbc\xb9\x10\x809\xcc\xbf~\xbc_\x97\x84W\x0f6\n\x1e\xc3\xc5)\xdf\xc9IP\xd2\x90\xaa\x95\xd8\x0e\x84\x96\x88\xb3\xccP\x98Q\x17<\xed\xf1.\x96=\xcd\x1d\x08;\x87\xab:A\xb7\xa0\xdb_y\xcaJR\xb2\x8b7X\xfe\xc1\xf2\nb}\x18\x1e\x95T}V\xf0\xb5\x1f\xcf*!\x13q\xdf\x8db\xb5\xe6$s\xec\xe1\x92d\x94\x93\xd8\x9ari?\xfe\x94\xc2\xdb\xfb~[\xa1\xfa\x1c\x04\xb6#\xb5\tF\x00xk\x93\x1el\xd1\xa6\xf4\x06\xd1'
256
-1
121
Traceback (most recent call last):
File "C:\Users\VoxaiLap10\Desktop\pythonbible\cryptotestpemmp3_b_md5_7-19-18b.py", line 90, in <module>
decryptedsessionkey=private_key.decrypt(bytes(encryptedsessionkey), padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),algorithm=hashes.SHA256(), label=None))
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 362, in decrypt
return _enc_dec_rsa(self._backend, self, ciphertext, padding)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 68, in _enc_dec_rsa
return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 130, in _enc_dec_rsa_pkey_ctx
_handle_rsa_enc_dec_error(backend, key)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 161, in _handle_rsa_enc_dec_error
raise ValueError("Decryption failed.")
ValueError: Decryption failed.
这是相关的RSA.py库代码,其中错误来自:
outlen = backend._ffi.new("size_t *", buf_size)
buf = backend._ffi.new("unsigned char[]", buf_size)
res = crypt(pkey_ctx, buf, outlen, data, len(data))
print(buf)
print(outlen)
print(data)
print(len(data))
print(res)
if res <= 0:
_handle_rsa_enc_dec_error(backend, key)
return backend._ffi.buffer(buf)[:outlen[0]]
def _handle_rsa_enc_dec_error(backend, key):
errors = backend._consume_errors()
assert errors
assert errors[0].lib == backend._lib.ERR_LIB_RSA
if isinstance(key, _RSAPublicKey):
assert (errors[0].reason ==
backend._lib.RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE)
raise ValueError(
"Data too long for key size. Encrypt less data or use a "
"larger key size."
)
else:
decoding_errors = [
backend._lib.RSA_R_BLOCK_TYPE_IS_NOT_01,
backend._lib.RSA_R_BLOCK_TYPE_IS_NOT_02,
backend._lib.RSA_R_OAEP_DECODING_ERROR,
# Though this error looks similar to the
# RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE, this occurs on decrypts,
# rather than on encrypts
backend._lib.RSA_R_DATA_TOO_LARGE_FOR_MODULUS,
]
if backend._lib.Cryptography_HAS_RSA_R_PKCS_DECODING_ERROR:
decoding_errors.append(backend._lib.RSA_R_PKCS_DECODING_ERROR)
assert errors[0].reason in decoding_errors
print(errors[0].reason)
raise ValueError("Decryption failed.")
我在密码库中找不到crypt(pkey_ctx, buf, outlen, data, len(data))
函数,该函数提供的res = -1值导致了错误。有人知道res
代表什么或crypt
函数在哪里吗?