【发布时间】:2018-07-23 22:48:14
【问题描述】:
我正在尝试从已以 RSA 格式加密的 DER 编码文件中解密 AES 密钥。使用以下代码:
for key, value in configfile['File Extensions'].items():
if filename.endswith(value):
pemFile=r'C:\Users\Public\Music\Sample Music\pkcs7.pem'
with open(pemFile,'r') as f:
pem = f.read()
# remove the -----BEGIN PKCS7----- header/footer
pemsplit=pem.split('\\n')
j =''.join(pemsplit[1:-2])
env_der = base64.b64decode(j)
#print(env_der)
f.closed
# merge files and then compare
content, rest = decode(env_der, asn1Spec=rfc2315.ContentInfo())
assert content['contentType'] == rfc2315.envelopedData
myenvelop, rest = decode(content['content'], asn1Spec=rfc2315.EnvelopedData())
lenencryptedkey=len(myenvelop['recipientInfos'][0]['encryptedKey'])
#print(lenencryptedkey)
encryptedsessionkey=myenvelop['recipientInfos'][0]['encryptedKey']
from cryptography.hazmat.primitives.asymmetric import padding
try:
#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))
我得到这个结果:
Traceback (most recent call last):
File "C:/Users/VoxaiLap10/Desktop/pythonbible/cryptotest7-23-18_testpart1.py", line 111, 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.
错误似乎发生在 python crypt 模块中,但我无法弄清楚到底是什么问题。我考虑过私钥不是用于加密会话密钥的公钥的正确值的可能性;您认为这是导致错误的原因吗? 这是错误所在的相关 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函数得到的res值为-1;你知道这代表什么吗?
【问题讨论】:
-
编程问题与本网站无关,即使您正在编程的内容与密码学有关。特别是,您要问为什么您的 Python 实现会引发错误,而这更适合 Stack Overflow。
-
请不要交叉发布到其他网站。有时您必须对自己的问题保持耐心。
-
@James K Polk 抱歉交叉发帖;感谢您的提示。
标签: encryption rsa aes asn.1