cipher = PKCS1_cipher.new(public_key)作用
时间: 2023-06-09 13:06:04 浏览: 77
这行代码的作用是创建一个RSA公钥加密器对象。具体地说,它使用PKCS#1 v1.5填充方案对数据进行加密,使用给定的公钥进行加密操作。这个加密器对象可以用来加密任意长度的二进制数据,生成经过加密的密文。
相关问题
将下面代码修改成正确格式 rom Crypto.Cipher import AES, PKCS1_OAEP from Crypto.Random import get_random_bytes from Crypto.Util.Padding import pad, unpad from Crypto.PublicKey import RSA # 生成RSA公私钥对 key = RSA.generate(2048) pub_key = key.publickey() # 加载明文文件 with open('p_text.txt', 'rb') as f: plain_text = f.read() # 生成随机密钥 session_key = get_random_bytes(16) # 用接收方公钥加密会话密钥 cipher_rsa = PKCS1_OAEP.new(pub_key) encrypted_key = cipher_rsa.encrypt(session_key) # 使用OFB模式加密明文文件 cipher_aes = AES.new(session_key, AES.MODE_OFB) cipher_text = cipher_aes.encrypt(pad(plain_text, AES.block_size)) # 保存密文文件 with open('c_text.txt', 'wb') as f: f.write(cipher_text) # 接收方用私钥解密会话密钥 cipher_rsa = PKCS1_OAEP.new(key) decrypted_key = cipher_rsa.decrypt(encrypted_key) # 用会话密钥解密密文文件 cipher_aes = AES.new(decrypted_key, AES.MODE_OFB) decrypted_text = unpad(cipher_aes.decrypt(cipher_text), AES.block_size) # 保存解密后的明文文件 with open('p1_text.txt', 'wb') as f: f.write(decrypted_text) # 比较明文文件和解密后的明文文件 with open('p_text.txt', 'rb') as f1, open('p1_text.txt', 'rb') as f2: if f1.read() == f2.read(): print('文件一致') else: print('文件不一致')
请注意,Python代码的格式对于程序的正确性非常重要。下面是修改后的代码:
```python
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
from Crypto.PublicKey import RSA
# 生成RSA公私钥对
key = RSA.generate(2048)
pub_key = key.publickey()
# 加载明文文件
with open('p_text.txt', 'rb') as f:
plain_text = f.read()
# 生成随机密钥
session_key = get_random_bytes(16)
# 用接收方公钥加密会话密钥
cipher_rsa = PKCS1_OAEP.new(pub_key)
encrypted_key = cipher_rsa.encrypt(session_key)
# 使用OFB模式加密明文文件
cipher_aes = AES.new(session_key, AES.MODE_OFB)
cipher_text = cipher_aes.encrypt(pad(plain_text, AES.block_size))
# 保存密文文件
with open('c_text.txt', 'wb') as f:
f.write(cipher_text)
# 接收方用私钥解密会话密钥
cipher_rsa = PKCS1_OAEP.new(key)
decrypted_key = cipher_rsa.decrypt(encrypted_key)
# 用会话密钥解密密文文件
cipher_aes = AES.new(decrypted_key, AES.MODE_OFB)
decrypted_text = unpad(cipher_aes.decrypt(cipher_text), AES.block_size)
# 保存解密后的明文文件
with open('p1_text.txt', 'wb') as f:
f.write(decrypted_text)
# 比较明文文件和解密后的明文文件
with open('p_text.txt', 'rb') as f1, open('p1_text.txt', 'rb') as f2:
if f1.read() == f2.read():
print('文件一致')
else:
print('文件不一致')
```
这段代码的主要修改是对缩进进行了修正,使其符合 Python 代码的语法要求。同时,可以看到这段代码使用了 Python 标准库中的 `Crypto` 模块,来实现 RSA 和 AES 算法的加密和解密。
import hashlib from Crypto.Cipher import PKCS1_v1_5 from Cryptodome.Cipher import DES from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 # 生成DES密钥 des_key = b"12345678" # 生成RSA密钥对 rsa_key = RSA.generate(1024) private_key = rsa_key.export_key() public_key = rsa_key.publickey().export_key() # 输入消息 message = b"hello world" # DES哈希变换 md5_hash = hashlib.md5(message).digest() # 将md5值转成length=8字节长的bytes类型,并加密 des_key_8 = md5_hash[:8] des = DES.new(des_key_8, DES.MODE_ECB) hash_value = des.encrypt(md5_hash) # RSA签名 rsa = RSA.import_key(private_key) signature_obj = PKCS1_v1_5.new(rsa) signature = signature_obj.sign(hash_value) # RSA验证 rsa = RSA.import_key(public_key) if signature_obj.verify(hash_value, signature): print("Signature is valid.") else: print("Signature is invalid.")
这是 Python 中常用的加密模块引用,包括 hashlib、PKCS1_v1_5、DES、RSA等,可用于实现加密、签名等功能。其中 hashlib 可以生成各种散列算法的哈希值,PKCS1_v1_5 和 RSA 可以实现非对称加密和数字签名,DES 可以实现对称加密。