使用Python加密/解密文件

4
有没有一种使用Python加密/解密文件的方法(类似Axcrypt)?

Ruby标签是不是误加的?我可以帮你修复,但我做不到... - Ashley Grenon
您是不是想使用 python 标签而不是 ruby,或者标题和问题描述有误? - Thomas
有 Ruby 标签吗?抱歉,我没有注意到。 - Gary
4个回答

1


0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接