赶紧给你的文件加个密吧

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 file
    with open(dataFile, 'rb') as f:
        data = f.read()
    
    # convert data to bytes
    data = bytes(data)

    # read public key from file
    with open(publicKeyFile, 'rb') as f:
        publicKey = f.read()
    
    # create public key object
    key = RSA.import_key(publicKey)
    sessionKey = os.urandom(16)

    # encrypt the session key with the public key
    cipher = PKCS1_OAEP.new(key)
    encryptedSessionKey = cipher.encrypt(sessionKey)

    # encrypt the data with the session key
    cipher = 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.' + fileExtension
    with 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 file
    with open(privateKeyFile, 'rb') as f:
        privateKey = f.read()
        # create private key object
        key = RSA.import_key(privateKey)

    # read data from file
    with 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 key
    cipher = PKCS1_OAEP.new(key)
    sessionKey = cipher.decrypt(encryptedSessionKey)

    # decrypt the data with the session key
    cipher = 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.' + fileExtension
    with open(decryptedFile, 'wb') as f:
        f.write(data)

    print('Decrypted file saved to ' + decryptedFile)

解密函数和加密函数的概念是一样的,只不过解密函数的运行过程和加密是相反的。

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

 hello,大家好,我是Jackpop,硕士毕业于哈尔滨工业大学,曾在华为、阿里等大厂工作,如果你对升学、就业、技术提升等有疑惑,不妨交个朋友:

https://mp.weixin.qq.com/s/fCHn8JpLQDH-M_QkVxwR1w

猜你喜欢

转载自blog.csdn.net/jakpopc/article/details/123961987