我使用reStructuredText来记录我的代码,以便通过epydoc获得不错的离线HTML页面.
结果很棒.唯一的缺点是当我使用Python交互式shell时,help()函数不解析文档字符串中的reST元数据,而是显示整个事物.
有没有办法让help()对文档字符串进行一些最小的解析?
我不希望渲染斜体字体或超链接,但至少进行一些最小的清理以提高可读性.
我正在使用PyCrypto在CBC模式下使用AES加密二进制文件(Python 3.2.3 64位和PyCrypto 2.6).使用以下代码:http://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/
但遇到以下错误:ValueError:IV必须长度为16个字节.
这是代码:
def encryptFile(key, in_filename, out_filename=None, chunksize=64*1024):
""" Encrypts a file using AES (CBC mode) with the
given key.
key:
The encryption key - a string that must be
either 16, 24 or 32 bytes long. Longer keys
are more secure.
in_file:
Input file
out_file:
If None, a StringIO will be returned.
chunksize:
Sets the size of the chunk which the function
uses to read and encrypt the file. Larger chunk
sizes can …Run Code Online (Sandbox Code Playgroud) 使用https://raw.github.com/usefulfor/usefulfor/master/security/JBoss.java上的代码,我做了以下工作:
bash-3.2$ java -cp . JBoss -e testpython
-27038292d345798947e2852756afcf0a
bash-3.2$ java -cp . JBoss -d -27038292d345798947e2852756afcf0a
testpython
Run Code Online (Sandbox Code Playgroud)
但是,我不能为我的生活,弄清楚如何使用python中的pycrypto解密字符串'27038292d345798947e2852756afcf0a'.我的理解是Java代码使用的是Blowfish,而"jaas就是这种方式"作为密码的关键.但我无法理解如何在python中执行此操作.以下结果导致大多数不可打印的垃圾:
import Crypto
from Crypto.Cipher import Blowfish
from base64 import b64encode, b64decode
bs = Blowfish.block_size
key = 'jaas is the way'
plaintext = b'27038292d345798947e2852756afcf0a'
iv = '\0' * 8
c1 = Blowfish.new(key, Blowfish.MODE_ECB)
c2 = Blowfish.new(key, Blowfish.MODE_CBC, iv)
c3 = Blowfish.new(key, Blowfish.MODE_CFB, iv)
c4 = Blowfish.new(key, Blowfish.MODE_OFB, iv)
msg1 = c1.decrypt(plaintext)
msg2 = c2.decrypt(plaintext)
msg3 = c3.decrypt(plaintext)
msg4 = c4.decrypt(plaintext)
print …Run Code Online (Sandbox Code Playgroud) 我最近遇到了以下代码示例,用于使用带有SHA-256 HMAC的AES-256 CBC加密文件进行身份验证和验证:
aes_key, hmac_key = self.keys
# create a PKCS#7 pad to get us to `len(data) % 16 == 0`
pad_length = 16 - len(data) % 16
data = data + (pad_length * chr(pad_length))
# get IV
iv = os.urandom(16)
# create cipher
cipher = AES.new(aes_key, AES.MODE_CBC, iv)
data = iv + cipher.encrypt(data)
sig = hmac.new(hmac_key, data, hashlib.sha256).digest()
# return the encrypted data (iv, followed by encrypted data, followed by hmac sig):
return data + sig
Run Code Online (Sandbox Code Playgroud)
因为在我的情况下,我加密的不仅仅是一个字符串,而是一个相当大的文件,我修改了代码来执行以下操作: …
我正在尝试编写一个需要大量加密强度伪随机字节的应用程序.
RC4密码对此非常理想; 它的重量轻,易于理解.所以,我得到了规范并用Python编写了一个RC4算法.
它完全按预期工作,但是,它像糖蜜一样慢.在我的Core i7 2.2GHz上,我只能从算法中获得大约1MB /秒的速度.
很明显,Python的解释性质并不是最适合这类任务的.我的问题是我不熟悉C编码 - 我用C做的最好的是一些Hello World的东西和一些文件读写的实验.无论哪种方式,我对C使用Python-C API肯定不够好.
我知道.NET/C#,我在Windows上用C#编写了相同的算法,并且我能够轻松地超过60MB /秒.所以.NET的CLR更加优化.但是,Python应用程序的目标平台是Unix/Linux.
理想情况下,我不想通过大量的中间层来获得优化的RC4密码到Python应用程序中.
由于RC4依赖于状态,理想情况下我会用类来做(这就是我用Python实现的方式.)所以,这里是我想要做的一小部分:
rc4 = RC4Encrypter()
rc4.seed(myKey) # seed the RC4 algorithm with bytes from string myKey
rc4.getRC4Bytes(1048576) # get the next 1MB of RC4 cryptostream bytes as a binary string
rc4.encryptWithRC4(myString) # encrypt myString's bytes with RC4 bytes using xor and return
Run Code Online (Sandbox Code Playgroud)
有什么建议?我很想学习C,但对于这个简单的项目来说,这是一个很大的学习曲线.
python ×5
encryption ×3
pycrypto ×3
aes ×1
epydoc ×1
hmac ×1
java ×1
javax.crypto ×1
pydoc ×1
python-3.x ×1
rc4-cipher ×1