马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.首先介绍Crypto库
本次实行重要使用Crypto库实现浅易的加密解密
1.1首先是对称加密
从cipher中引入AES而且用于创建AES加密器,使用get_random_bytes生成密钥,并作为AES加密器的参数,对明文采用encrypt_and_digest库加密。
然后采用
cipher =AES.new(key,AES.MODE_EAX,nonce=cipher.nonce)
设计解密器并使用decrypt_and_verify进行解密
- # 对称加密算法
- from Crypto.Cipher import AES
- from Crypto.Random import get_random_bytes
- # 生成密钥
- key = get_random_bytes(16) # 16 字节的密钥
- # 创建 AES 加密器
- cipher = AES.new(key, AES.MODE_EAX) # 创建 AES 加密器,使用 EAX 模式
- # 待加密的明文
- plaintext = b'This is a secret message'
- # 加密
- ciphertext , tag = cipher.encrypt_and_digest(plaintext) # 加密并生成标签
- print("加密后的密文:", ciphertext)
- print("标签:", tag)
- # 初始化解密器
- cipher = AES.new(key,AES.MODE_EAX,nonce=cipher.nonce)
- # 解密
- decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) # 解密并验证标签
- print("解密后的明文:", decrypted_text)
复制代码 这段代码是用Python编写的,使用了pycryptodome库来实现RSA加密和解密过程,以下是对这段代码的具体解释:
1.2 非对称加密
导入必要的模块
- from Crypto.PublicKey import RSA
- from Crypto.Cipher import PKCS1_OAEP
复制代码
- Crypto.PublicKey模块提供了公钥加密算法的实现,RSA是此中的一种算法。
- Crypto.Cipher模块提供了加密算法的实现,PKCS1_OAEP是RSA加密的一种模式,它提供了一种更安全的加密方式,可以防止一些简单的攻击。
生成密钥对
- key_pair = RSA.generate(2048)
复制代码
- RSA.generate(2048)生成一个2048位的RSA密钥对,包罗公钥和私钥。
- key_pair是一个包含公钥和私钥的对象。
界说明文
- plaintext = b'This is the message to be encrypted'
复制代码
- 界说了一个明文消息,这是一个字节串,因为RSA加密需要对字节数据进行操作。
使用PKCS1_OAEP模式加密
- cipher = PKCS1_OAEP.new(key_pair.public_key())
- ciphertext = cipher.encrypt(plaintext)
- print("加密后的密文:", ciphertext)
复制代码
- PKCS1_OAEP.new(key_pair.public_key())创建了一个使用公钥的加密对象。
- cipher.encrypt(plaintext)使用公钥对明文进行加密,得到密文。
- 打印加密后的密文。
解密
- decipher = PKCS1_OAEP.new(key_pair)
- decrypted_text = decipher.decrypt(ciphertext)
- print("解密后的明文:", decrypted_text)
复制代码
- PKCS1_OAEP.new(key_pair)创建了一个使用私钥的解密对象。
- decipher.decrypt(ciphertext)使用私钥对密文进行解密,得到明文。
- 打印解密后的明文。
完整代码:
- from Crypto.PublicKey import RSA
- from Crypto.Cipher import PKCS1_OAEP
- # 生成密钥对key_pair = RSA.generate(2048)
- # 明文plaintext = b'This is the message to be encrypted'
- # 使用PKCS1_OAEP模式加密cipher = PKCS1_OAEP.new(key_pair.public_key())
- ciphertext = cipher.encrypt(plaintext)
- print("加密后的密文:", ciphertext)
- # 解密decipher = PKCS1_OAEP.new(key_pair)
- decrypted_text = decipher.decrypt(ciphertext)
- print("解密后的明文:", decrypted_text)
复制代码 1.3 哈希
完整代码:
- from Crypto.Hash import SHA256
- # 明文
- plaintext = b'This is the message to be hashed'
- # 哈希
- hash_value = SHA256.new(plaintext).digest()
- print("哈希值:", hash_value.hex())
复制代码 2.生成RSA密钥并生存
CreateRSA.py,这是用来生成RSA秘钥对儿的,生成的私钥会生存在zmy_rsa文件里,生成的公钥会生存在zmy_rsa.pub文件里。
- from Crypto.PublicKey import RSA
- def CreateRSAKeys():
- code = 'nooneknows'
- # 生成 2048 位的 RSA 密钥
- key = RSA.generate(2048)
- encrypted_key = key.exportKey(passphrase=code, pkcs=8, protection="scryptAndAES128-CBC")
- # 生成私钥
- with open('zmy_rsa', 'wb') as f:
- f.write(encrypted_key)
- # 生成公钥
- with open('zmy_rsa.pub', 'wb') as f:
- f.write(key.publickey().exportKey())
-
- CreateRSAKeys()
复制代码 这段代码使用Python的Crypto库生成2048位的RSA密钥对,将私钥用密码"nooneknows"加密后生存为zmy_rsa文件,同时将公钥生存为zmy_rsa.pub文件,实现了基本的非对称密钥生成功能。
3.文件加密
- 加密流程概述
读取目标文件(二进制模式)。
生成随机AES会话密钥(16字节)。
用RSA公钥加密会话密钥(确保只有私钥持有者能解密)。
用AES密钥加密文件内容(高效对称加密)。
将加密后的数据写入原文件(覆盖原内容)。
- 代码逐行剖析
(1)导入加密库
- from Crypto.Cipher import AES, PKCS1_OAEP
- from Crypto.PublicKey import RSA
- from Crypto.Random import get_random_bytes
复制代码 AES:对称加密算法,用于加密文件内容。
PKCS1_OAEP:RSA加密添补方案(安全模式)。
RSA:非对称加密,用于加密会话密钥。
get_random_bytes:生成随机密钥。
(2)加密函数 Encrypt(filename)
- def Encrypt(filename):
- data = ''
- with open(filename, 'rb') as f:
- data = f.read() # 读取原始文件内容
复制代码 以二进制模式(‘rb’)读取文件内容到 data 变量。
(3)生成并加密会话密钥
- with open(filename, 'wb') as out_file:
- recipient_key = RSA.import_key(open('python-file-encryption\zmy_rsa.pub').read())
- session_key = get_random_bytes(16) # 生成16字节AES密钥
- cipher_rsa = PKCS1_OAEP.new(recipient_key)
- out_file.write(cipher_rsa.encrypt(session_key)) # 写入加密后的会话密钥
- recipient_key:从文件加载RSA公钥(zmy_rsa.pub)。
- session_key:随机生成的16字节AES密钥(每次加密不同)。
- cipher_rsa.encrypt(session_key):用RSA公钥加密会话密钥,写入文件。
复制代码 (4)加密文件内容(AES)
- cipher_aes = AES.new(session_key, AES.MODE_EAX)
- ciphertext, tag = cipher_aes.encrypt_and_digest(data) # 加密并生成认证标签
- out_file.write(cipher_aes.nonce) # 写入随机nonce(初始化向量)
- out_file.write(tag) # 写入认证标签(防篡改)
- out_file.write(ciphertext) # 写入加密后的文件内容
复制代码 AES.MODE_EAX:一种安全的AES加密模式(支持认证)。
nonce:随机数,确保相同明文每次加密结果差别。
tag:完整性校验标签,防止密文被篡改。
ciphertext:加密后的文件内容。
(5)调用加密函数
对指定路径的文件进行加密。
3. 加密后的文件布局
最终文件内容按顺序包含:
RSA加密的AES会话密钥(长度取决于RSA密钥大小,如2048位→256字节)。
AES的nonce(16字节)。
认证标签tag(16字节)。
加密后的文件内容(长度与原文件相同)。
4. 为什么混合使用RSA和AES?
RSA:加密短数据(如会话密钥),但速率慢,不适合大文件。
AES:加密大文件高效,但需要安全传递密钥。
结合优势:用RSA传递AES密钥,用AES加密文件。
5. 安全性注意事项
公钥必须可信:若攻击者更换公钥,可解密会话密钥。
nonce和tag不可篡改:否则解密会失败。
覆盖原文件:加密后原文件丢失,需提前备份。
密钥管理:私钥(zmy_rsa.priv)必须严格保密,否则全部文件可被解密。
6. 怎样解密?
需另一段代码用RSA私钥解密会话密钥,再用AES解密文件内容。解密流程:
读取加密文件的前256字节(RSA加密的AES密钥)。
用RSA私钥解密出AES会话密钥。
读取后续的nonce、tag和密文。
用AES解密并验证完整性。
总结
这段代码是一个典型的混合加密系统,结合了RSA的安全性和AES的高效性,适用于文件加密场景。实际应用中需注意密钥管理和错误处理(如文件不存在、密钥无效等)。
4.文件解密
- from Crypto.PublicKey import RSA
- from Crypto.Cipher import AES, PKCS1_OAEP
- def Descrypt(filename):
- code = 'nooneknows'
- with open(filename, 'rb') as fobj:
- # 导入私钥
- private_key = RSA.import_key(open('python-file-encryption\zmy_rsa').read(), passphrase=code)
- # 会话密钥, 随机数,消息认证码,机密的数据
- enc_session_key, nonce, tag, ciphertext = [fobj.read(x)
- for x in (private_key.size_in_bytes(),
- 16, 16, -1)]
- cipher_rsa = PKCS1_OAEP.new(private_key)
- session_key = cipher_rsa.decrypt(enc_session_key)
- cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
- # 解密
- data = cipher_aes.decrypt_and_verify(ciphertext, tag)
- with open(filename, 'wb') as wobj:
- wobj.write(data)
-
- Descrypt("filename")
复制代码 这段代码是一个使用 Python 的 pycryptodome 库实现的文件解密程序,重要功能是解密一个使用 RSA 和 AES 加密的文件。以下是代码的具体解释:
1. 导入必要的模块
- from Crypto.PublicKey import RSA
- from Crypto.Cipher import AES, PKCS1_OAEP
复制代码
- Crypto.PublicKey.RSA:用于处理 RSA 加密和解密。
- Crypto.Cipher.AES:用于处理 AES 加密和解密。
- Crypto.Cipher.PKCS1_OAEP:用于处理 RSA 的 OAEP 添补模式,这是一种安全的 RSA 加密方式。
2. 界说解密函数
- 界说了一个名为 Descrypt 的函数,接受一个参数 filename,表现要解密的文件路径。
3. 设置私钥的密码
- 界说了一个字符串变量 code,值为 'nooneknows',这是用于解密私钥的密码。
4. 打开并读取文件
- with open(filename, 'rb') as fobj:
复制代码
- 使用 with 语句以二进制读取模式 ('rb') 打开文件,文件路径由 filename 参数指定。
- fobj 是文件对象,用于后续读取文件内容。
5. 导入私钥
- private_key = RSA.import_key(open('python-file-encryption\zmy_rsa').read(), passphrase=code)
复制代码
- 打开路径为 'python-file-encryption\zmy_rsa' 的文件,读取其内容。
- 使用 RSA.import_key 方法导入私钥,并传入 passphrase=code,即私钥的密码 'nooneknows'。
6. 读取加密数据
- enc_session_key, nonce, tag, ciphertext = [fobj.read(x)
- for x in (private_key.size_in_bytes(),
- 16, 16, -1)]
复制代码
- 从文件中依次读取以下数据:
- enc_session_key:加密的会话密钥,长度为私钥的字节大小(private_key.size_in_bytes())。
- nonce:随机数,长度为 16 字节。
- tag:消息认证码,长度为 16 字节。
- ciphertext:加密的数据,读取剩余的全部内容(-1 表现读取到文件末端)。
7. 解密会话密钥
- cipher_rsa = PKCS1_OAEP.new(private_key)
- session_key = cipher_rsa.decrypt(enc_session_key)
复制代码
- 使用 PKCS1_OAEP.new 创建一个 RSA 解密器,传入私钥 private_key。
- 使用 cipher_rsa.decrypt 方法解密 enc_session_key,得到会话密钥 session_key。
8. 创建 AES 解密器
- cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
复制代码
- 使用 AES.new 创建一个 AES 解密器,传入会话密钥 session_key、模式 AES.MODE_EAX 和随机数 nonce。
9. 解密数据
- data = cipher_aes.decrypt_and_verify(ciphertext, tag)
复制代码
- 使用 cipher_aes.decrypt_and_verify 方法解密 ciphertext,并验证消息认证码 tag。
- 假如验证通过,返回解密后的数据 data。
10. 将解密后的数据写回文件
- with open(filename, 'wb') as wobj:
- wobj.write(data)
复制代码
- 使用 with 语句以二进制写入模式 ('wb') 打开文件,文件路径由 filename 参数指定。
- 将解密后的数据 data 写入文件。
11. 调用解密函数
- 调用 Descrypt 函数,传入文件路径 "D:/a_yan0/lesuo/2130090224 张天奇.docx",对该文件进行解密。
这段代码实现了一个文件解密程序,重要步骤包罗:
- 导入必要的模块。
- 界说解密函数,设置私钥密码。
- 打开并读取加密文件。
- 导入私钥,读取加密数据。
- 解密会话密钥。
- 创建 AES 解密器,解密数据。
- 将解密后的数据写回文件。
- 调用解密函数,对指定文件进行解密。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |