问题描述
好的,所以我找到了一种在将私钥导出到 PEM 字节时加密私钥的方法,就像这样(所有代码都来自 Nitratine 和 StackOverflow(谁会猜到)),用于名为 encrypterlib 的新模块
def generate_private_bytes_from_key(key: _RSAPrivateKey,password: bytes = None):
from cryptography.hazmat.primitives import serialization
return key.private_bytes(
encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.BestAvailableEncryption(password) if type(password) == bytes else serialization.NoEncryption()
)
但我无法解密:
Python 控制台:
Python 3.9.1 and some other text
>>> from encrypterlib import *
>>> private_key = asymmetric.generate_key()
>>> private_bytes = asymmetric.generate_private_bytes_from_key(private_key,password = b"p@ssw0rd")
>>> private_key_2 = asymmetric.generate_key_from_private_bytes(private_bytes,password = b"p@ssw0rd")
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
File "C:\Program Files\Python\lib\encrypterlib.py",line 30,in generate_key_from_private_bytes
return serialization.load_pem_private_key(
File "C:\Program Files\Python\lib\site-packages\cryptography\hazmat\primitives\serialization\base.py",line 18,in load_pem_private_key
return backend.load_pem_private_key(data,password)
File "C:\Program Files\Python\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py",line 1244,in load_pem_private_key
return self._load_key(
File "C:\Program Files\Python\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py",line 1465,in _load_key
raise TypeError(
TypeError: Password was not given but private key is encrypted
>>>
模块:
def generate_key_from_private_bytes(bytes: bytes,password: bytes = None):
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
return serialization.load_pem_private_key(
bytes,password=password if type(password) == bytes else None,backend=default_backend()
)
但密码学不承认我提供了密码这一事实。有什么帮助吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)