簡體   English   中英

使用python加密/解密文件

[英]Encrypting/Decrypting file with python

有沒有一種方法可以使用python來加密/解密文件(類似於Axcrypt)?

這樣的SO Q&A (如何使用PGP進行加密/解密)怎么樣?

在python文檔中轉到此處以獲取可用於加密的模塊: http : //docs.python.org/library/crypto.html

您可以嘗試進行加密和解密。

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import nacl.secret
import nacl.utils
import base64
from pyblake2 import blake2b
import getpass

print "### ENCRYPTION"
key = blake2b(digest_size=16)
key.update(getpass.getpass("PASSWORD:"))
key = key.hexdigest()

print "key: %s" % key
box = nacl.secret.SecretBox(key)

# This is our message to send, it must be a bytestring as SecretBox will
#   treat is as just a binary blob of data.
msg = b"whohooäööppöööo"
print "msg: %s" % msg
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
print "nonce: %s" % nacl.encoding.HexEncoder.encode(nonce)
encrypted = box.encrypt(msg, nonce, encoder=nacl.encoding.HexEncoder)
print "cipher: %s " % encrypted

print "### DECRYPTION"
key = blake2b(digest_size=16)
key.update(getpass.getpass("PASSWORD:"))
key = key.hexdigest()

nonce = None
print "nonce: %s" % nonce
print "key: %s" % key
box = nacl.secret.SecretBox(key)

msg = encrypted
print "msg: %s" % msg

plain = box.decrypt(ciphertext=msg,encoder=nacl.encoding.HexEncoder)
print "plain: %s" % plain

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM