美文网首页
cryptography库

cryptography库

作者: lk_erzanml | 来源:发表于2022-12-22 11:33 被阅读0次

    from cryptography.fernet import Fernet

    (1)生成的秘钥,注意保存,第一次

    key = Fernet.generate_key()
    print(key)
    file = open('./key.data', 'wb')
    file.write(key)
    file.close()

    (2)读取秘钥

    fr = open("./key.data", 'rb')
    chenkey = fr.read()
    print(chenkey)

    (3)加密文件

    fpwd = Fernet(chenkey)
    file_orign = open("Win64OpenSSL-3_0_7.msi", 'rb')
    file_content = file_orign.read()
    print("chenread")
    encrypted_content = fpwd.encrypt(file_content)

    fsave = open("Win64OpenSSL-3_0_7.msi", 'wb')
    fsave.write(encrypted_content)
    print("加密 完成")

    (4)解密文件

    fpwd = Fernet(chenkey)
    file_jiemi_orign = open("Win64OpenSSL-3_0_7.msi", 'rb')
    file_jiemi_content = bytearray(fpwd.decrypt(file_jiemi_orign.read()))

    fsave_jiemi = open("Win64OpenSSL-3_0_7.msi", 'wb')
    fsave_jiemi.write(file_jiemi_content)
    print("解密 完成")

    相关文章

      网友评论

          本文标题:cryptography库

          本文链接:https://www.haomeiwen.com/subject/suyvqdtx.html