使用python加密/解密文件

时间:2010-08-12 19:07:22

标签: python encryption

有没有办法使用python加密/解密文件(比如Axcrypt)?

4 个答案:

答案 0 :(得分:1)

this SO Q&A如何讨论使用PGP进行加密/解密?

答案 1 :(得分:1)

答案 2 :(得分:0)

在python docs中找到可用于加密的模块:http://docs.python.org/library/crypto.html

答案 3 :(得分:0)

您可以尝试此方法进行加密和解密。

#!/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