写点什么

赶紧给你的文件加个密吧!

作者:Jackpop
  • 2022 年 4 月 04 日
  • 本文字数:1937 字

    阅读完需:约 6 分钟

hello,大家好,我是 Jackpop。今天跟大家聊一下隐私保护的话题。


使用电脑久了,日积月累,都会沉淀下来一些隐私信息,内容包含但不限于文档、音频、视频等形式。存储在云盘上吧....都知道的,公有云盘安全性差,说不定哪天也就跑路了。存在电脑上吧....又担心泄露隐私。今天,就来手把手教大家开发一款专有的内容加密工具!生成密钥首先,正如我们之前所知道的,非对称加密有两个秘钥,而且是相互不同的。一个是公钥,用于加密信息,另一个是私钥,用于解密信息。'''pip install pycryptodome'''


from Crypto.PublicKey import RSA


key = RSA.generate(2048)privateKey = key.export_key()publicKey = key.publickey().export_key()


with open('private.pem', 'wb') as f:f.write(privateKey)


with open('public.pem', 'wb') as f:f.write(publicKey)


print('Private key saved to private.pem')print('Public key saved to public.pem')print('Done')上面代码比较简单,首先导入 Crypto 工具包中的 RSA 模块,用于生成秘钥。然后你会得到两个文件:private.pem 和 public.pem。保存公钥后面会用来加密数据,保存私钥用于解密数据。加密函数首先,给出加密函数的代码:


def encrypt(dataFile, publicKeyFile):'''use EAX mode to allow detection of unauthorized modifications'''# read data from filewith open(dataFile, 'rb') as f:data = f.read()


# convert data to bytesdata = bytes(data)
# read public key from filewith open(publicKeyFile, 'rb') as f: publicKey = f.read()
# create public key objectkey = RSA.import_key(publicKey)sessionKey = os.urandom(16)
# encrypt the session key with the public keycipher = PKCS1_OAEP.new(key)encryptedSessionKey = cipher.encrypt(sessionKey)
# encrypt the data with the session keycipher = AES.new(sessionKey, AES.MODE_EAX)ciphertext, tag = cipher.encrypt_and_digest(data)[]
# save the encrypted data to file[ fileName, fileExtension ] = dataFile.split('.')encryptedFile = fileName + '_encrypted.' + fileExtensionwith open(encryptedFile, 'wb') as f: [ f.write(x) for x in (encryptedSessionKey, cipher.nonce, tag, ciphertext) ]print('Encrypted file saved to ' + encryptedFile)
复制代码


接下来,简单的解释一下这个函数。


这个函数有两个输入参数,文件的路径信息和公钥的文件路径。当这个函数运行时,程序将打开消息文件并将数据转换为字节数。同时打开公钥,用一个随机值创建一个会话密钥,并将其附加到加密的消息文件中。这意味着,当有人试图修改该文件时,那么该文件就无法解密。在我们有了会话密钥后,然后用公钥对该会话密钥进行加密。最后,程序将用会话密钥加密数据,并保存加密的会话密钥、nonce、标签和密码文本。解密函数同样,先给出解密函数的代码:def decrypt(dataFile, privateKeyFile):'''use EAX mode to allow detection of unauthorized modifications'''


# read private key from filewith open(privateKeyFile, 'rb') as f:    privateKey = f.read()    # create private key object    key = RSA.import_key(privateKey)
# read data from filewith open(dataFile, 'rb') as f: # read the session key encryptedSessionKey, nonce, tag, ciphertext = [ f.read(x) for x in (key.size_in_bytes(), 16, 16, -1) ]
# decrypt the session keycipher = PKCS1_OAEP.new(key)sessionKey = cipher.decrypt(encryptedSessionKey)
# decrypt the data with the session keycipher = AES.new(sessionKey, AES.MODE_EAX, nonce)data = cipher.decrypt_and_verify(ciphertext, tag)
# save the decrypted data to file[ fileName, fileExtension ] = dataFile.split('.')decryptedFile = fileName + '_decrypted.' + fileExtensionwith open(decryptedFile, 'wb') as f: f.write(data)
print('Decrypted file saved to ' + decryptedFile)
复制代码


解密函数和加密函数的概念是一样的,只不过解密函数的运行过程和加密是相反的。本文介绍的文件加密和解密过程是非对称加密中比较基础的,希望通过本文能够让你了解一些勒索软件是如何加密你的数据,以及 SSL 如何加密通信。如果感兴趣,可以自己深入研究一下,开发一款功能更加高级的加密工具,给你的小姐姐加个密吧!

最后,给大家推荐一个非常不错,可以用于练习算法和编程技巧的平台,涵盖 Java、算法、Python、SQL 等不同领域和方向,题库丰富,完全免费:

https://www.nowcoder.com/exam/oj?tab=%E7%AE%97%E6%B3%95%E7%AF%87&topicId=295&fromPut=pc_zh_liudong0110_suanfa

发布于: 刚刚阅读数: 2
用户头像

Jackpop

关注

还未添加个人签名 2020.09.16 加入

公众号:平凡而诗意,微信:code_7steps,全网粉丝超20万,技术进阶、优质资源、实用工具,欢迎关注!

评论

发布
暂无评论
赶紧给你的文件加个密吧!_Jackpop_InfoQ写作平台