2017-10-20 614 views

回答

0

感谢@mnistic它开始工作,虽然有一些修改。这里是最后的工作代码(你必须牢记的openssl_private_decrypt默认值):

from cryptography.hazmat.primitives import serialization 
from cryptography.hazmat.backends import default_backend 
from cryptography.hazmat.primitives.asymmetric import padding 

# It's critical that the file be opened in mode "rb"! 
with open("private.key", 'rb') as key_file: 
    private_key = serialization.load_pem_private_key(key_file.read(), password=None, backend=default_backend()) 

with open('encrypted_file', 'rb') as encrypted_file: 
    ciphertext = encrypted_file.read() 

plaintext = private_key.decrypt(ciphertext, padding.PKCS1v15()) 

请注意,ciphertext需要比键的最大块大小短(这是比特数RSA的关键字除以8)。希望对未来的Google员工有所帮助!

1

随着Cryptography module,您可以与安装:

$ pip install cryptography

假设你有存储在一个名为“路径/到/ key.pem”文件中的私钥,首先加载私钥:

from cryptography.hazmat.primitives import serialization 
with open("path/to/key.pem", "rb") as key_file: 
    private_key = serialization.load_pem_private_key(
     key_file.read(), 
     password=None, 
     backend=default_backend() 
    ) 

然后你解密:

plaintext = private_key.decrypt(
    ciphertext, 
    padding.OAEP(
     mgf=padding.MGF1(algorithm=hashes.SHA1()), 
      algorithm=hashes.SHA1(), 
      label=None 
    ) 
)