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.")
时间: 2023-06-02 09:07:42 浏览: 363
这是 Python 中常用的加密模块引用,包括 hashlib、PKCS1_v1_5、DES、RSA等,可用于实现加密、签名等功能。其中 hashlib 可以生成各种散列算法的哈希值,PKCS1_v1_5 和 RSA 可以实现非对称加密和数字签名,DES 可以实现对称加密。
相关问题
from Crypto import Random from Crypto.PublicKey import RSA random_generator = Random.new().read rsa = RSA.generate(2048, random_generator) # 生成私钥 private_key = rsa.exportKey() #导出私钥 print(private_key.decode('utf-8')) #以utf-8格式解码并打印私钥 # 生成公钥 public_key = rsa.publickey().exportKey() #导出私钥 print(public_key.decode('utf-8')) #以utf-8格式解码并打印公钥 with open('rsa_private_key.pem', 'wb')as f: f.write(private_key) #打开私钥文件并进行写操作 with open('rsa_public_key.pem', 'wb')as f: f.write(public_key) #打开公钥文件并进行写操作 import base64 #导入base64编码 from Crypto.PublicKey import RSA #导入PulbicKey库 from Crypto.Hash import SHA #导入Hash库 from Crypto.Signature import PKCS1_v1_5 as PKCS1_signature #导入Signature库 from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher #导入Cipher库 # def get_key(key_file): with open(key_file) as f: data = f.read() key = RSA.importKey(data) return key def encrypt_data(msg): public_key = get_key('rsa_public_key.pem') cipher = PKCS1_cipher.new(public_key) encrypt_text = base64.b64encode(cipher.encrypt(bytes(msg.encode("utf8")))) return encrypt_text.decode('utf-8') def decrypt_data(encrypt_msg): private_key = get_key('rsa_private_key.pem') cipher = PKCS1_cipher.new(private_key) back_text = cipher.decrypt(base64.b64decode(encrypt_msg), 0) return back_text.decode('utf-8') def test_encrypt_decrypt(): msg = "coolpython.net" encrypt_text = encrypt_data(msg) decrypt_text = decrypt_data(encrypet_text) print(msg == decrypt_text) test_encrypt_decrypt() # Tru
这段代码的作用是导入了 Crypto 模块的 Random 和 RSA 子模块,用于生成随机数及生成 2048 位 RSA 公钥和私钥。其中,random_generator 通过读取 Random 模块的新实例化对象的 read() 方法来生成随机数,rsa 通过调用 RSA 模块的 generate() 方法生成指定位数的公私钥对。
pycryptodome用最新版的这个库会报错from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP ModuleNotFoundError: No module named 'Crypto'
根据您提供的引用内容,您使用的是pycryptodome库,而不是Crypto库。因此,您需要使用以下命令安装pycryptodome库:
```shell
pip install pycryptodome
```
安装完成后,您需要将您的代码中的Crypto更改为pycryptodome,例如:
```python
from Crypto.PublicKey import RSA # 更改为 from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP # 更改为 from Crypto.Cipher import PKCS1_OAEP
```
阅读全文