Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何存储加密数据,而不是解密数据以使用它_Python_Encryption_Cryptography - Fatal编程技术网

Python 如何存储加密数据,而不是解密数据以使用它

Python 如何存储加密数据,而不是解密数据以使用它,python,encryption,cryptography,Python,Encryption,Cryptography,我有以下加密和解密数据的代码: def encrypt(self): private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048, backend=default_backend() ) public_key = private_key.public_key() message = b"THE SECRET

我有以下加密和解密数据的代码:

 def encrypt(self):

    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )

    public_key = private_key.public_key()

    message = b"THE SECRET KEY IS VERY SECRET!"

    ciphertext = public_key.encrypt(
        message,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA1()),
            algorithm=hashes.SHA1(),
            label=None
        )
    )
    print(ciphertext)


def decrypt(self):

    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )

    public_key = private_key.public_key()


    with open('test.txt', 'r') as test:

        message = test.read()

    
    plaintext = private_key.decrypt(
        message,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA1()),
            algorithm=hashes.SHA1(),
            label=None
        )
    )
    print(plaintext)
我对
密码学是新手,不知道它是如何工作的。我想要的是,我加密数据,将其存储在一个文件中,当我需要这些数据时,我可以再次解密它。加密工作得很好,但每次我尝试解密它时,都会出现以下错误:

密文长度必须等于密钥大小


encrypt
decrypt
方法中,每次调用都会生成一个新的密钥对,也就是说,加密和解密是使用不属于一起的公钥和私钥完成的,因此解密必须失败

encrypt
中,密文根本不会写入文件
test.txt
(或者您是手动执行此操作的?)

此外,密文由二进制数据组成,因此I/O访问必须在二进制模式下进行(用a
b
标记)。另一种可能是使用合适的二进制到字符串编码,如Base64

在下面的Python3代码中,密钥对生成一次,公钥用于加密,私钥用于解密。密文被写入
pathCiphertextFile
的文件中,并从那里加载以进行解密。读写是在二进制模式下完成的(
rb
wb
)。然后解密成功:

来自cryptography.hazmat.primitives.rsa
来自cryptography.hazmat.primitives.不对称导入填充
从cryptography.hazmat.primitives导入哈希
从cryptography.hazmat.backends导入默认\u后端
def encrypt(消息、路径密码文本文件、公钥):
密文=公钥。加密(
消息
padding.OAEP(
mgf=padding.MGF1(算法=hashes.SHA1()),
算法=hashes.SHA1(),
标签=无
)
)
以open(pathCiphertextFile,'wb')作为测试:
测试写入(密文)
def解密(pathCiphertextFile,私钥):
以open(pathCiphertextFile,'rb')作为测试:
密文=test.read()
明文=私钥。解密(
密文,
padding.OAEP(
mgf=padding.MGF1(算法=hashes.SHA1()),
算法=hashes.SHA1(),
标签=无
)
)
返回明文
message=b“密钥非常机密!”
pathCiphertextFile=b“”
private\u key=rsa.generate\u private\u key(
公共指数=65537,
密钥大小=2048,
backend=默认值_backend()
)
ciphertext=加密(消息,路径ciphertext文件,私钥。公钥())
decryptedText=解密(路径密文文件,私钥)
打印(解密文本)

更新:发布的错误消息表明消息长度大于您使用的密钥大小(2048位)。正如Woodstock在评论中指出的,消息的长度不能超过密钥大小,即256字节。根据应用的填充(出于安全原因,这是强制性的),长度将进一步减少,例如,对于SHA1到214字节的OAEP,请参阅。因此,大量数据在实践中使用对称加密(如AES)或结合对称和非对称加密的a进行加密:数据使用对称加密进行加密,双方使用非对称加密交换对称密钥。

这里还有一个问题。。。根据定义,RSA只能直接对数据有效载荷进行加密,加密长度不超过其模数
n
,在本例中为2048位(256字节)。此外,如果需要非对称方案,请不要使用RSA,而是使用椭圆曲线加密。别用纱布,它坏了。