python 加密解密 文件_python 文件加解密

该博客介绍了如何使用Python的pycryptodome库进行文件的加解密操作,包括RSA密钥对的生成、AES加密以及文件的重命名。通过创建RSA密钥对,用公钥加密文件,私钥解密,同时对文件名进行Base64编码处理以增加安全性。还提供了对整个文件夹内文件批量加解密的函数。
摘要由CSDN通过智能技术生成

#pip install pycryptodome == 3.9.9

from Crypto.PublicKey importRSAfrom Crypto.Random importget_random_bytesfrom Crypto.Cipher importAES, PKCS1_OAEPimportosimportbase64

code= '1ikwcwtfph'

#创建秘钥对

defCreateRSAKeys():

key= RSA.generate(2048)

encrypted_key= key.exportKey(passphrase=code, pkcs=8, protection="scryptAndAES128-CBC")#私钥

with open('my_private_rsa_key.bin', 'wb') as f:

f.write(encrypted_key)#公钥

with open('my_rsa_public.pem', 'wb') as f:

f.write(key.publickey().exportKey())#加密

defEncrypt(filename):

data= ''with open(filename,'rb') as f:

data=f.read()

with open(filename,'wb') as out_file:#收件人秘钥 - 公钥

recipient_key = RSA.import_key(open('my_rsa_public.pem').read())

session_key= get_random_bytes(16)#Encrypt the session key with the public RSA key

cipher_rsa =PKCS1_OAEP.new(recipient_key)

out_file.write(cipher_rsa.encrypt(session_key))#Encrypt the data with the AES session key

cipher_aes =AES.new(session_key, AES.MODE_EAX)

ciphertext, tag=cipher_aes.encrypt_and_digest(data)

out_file.write(cipher_aes.nonce)

out_file.write(tag)

out_file.write(ciphertext)#解密

defDescrypt(filename):

with open(filename,'rb') as fobj:

private_key= RSA.import_key(open('my_private_rsa_key.bin').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)#文件重命名

defRenameFile(dir, filename):

filename_bytes= filename.encode('utf-8')

filename_bytes_base64=base64.encodebytes(filename_bytes)

filename_bytes_base64= filename_bytes_base64[::-1][1:]

new_filename= filename_bytes_base64.decode('utf-8') + '.crypt1'

print(os.path.join(dir, filename))print(os.path.join(dir, new_filename))

os.rename(os.path.join(dir, filename), os.path.join(dir, new_filename))#解密并且恢复名字

defReserveFilename(dir, filename):

f=filename

filename= filename[::-1][7:][::-1]

filename_base64= filename[::-1] + '\n'filename_bytes_base64= filename_base64.encode('utf-8')

ori_filename= base64.decodebytes(filename_bytes_base64).decode('utf-8')print(os.path.join(dir, f))print(os.path.join(dir, ori_filename))

os.rename(os.path.join(dir, f), os.path.join(dir, ori_filename))#文件夹所有文件加密

defencryptFolder(rootDir):

list_dirs=os.walk(rootDir)for root, dirs, files inlist_dirs:for f infiles:

filename=os.path.join(root, f)

Encrypt(filename)

RenameFile(root, f)#文件夹所有文件解密

defdescryptFolder(rootDir):

list_dirs=os.walk(rootDir)for root, dirs, files inlist_dirs:for f infiles:

filename=os.path.join(root, f)

Descrypt(filename)

ReserveFilename(root, f)if __name__ == '__main__':

rootDir= "D://folder"

'''1.第一步执行创建秘钥函数

CreateRSAKeys()

2.第二步加密文件所有文件

encryptFolder(rootDir)

3.解密文件前,先注释第二部代码

#CreateRSAKeys()

#encryptFolder(rootDir)

descryptFolder(rootDir)'''CreateRSAKeys()#encryptFolder(rootDir)

#descryptFolder(rootDir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值