我试图用Python 2.7.3 在Ubuntu 10.04(Lucid Lynx)上安装pycrypto2.6 .
我遇到以下错误:
running build
running build_py
running build_ext
running build_configure
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/pratibha/Desktop/pycrypto-2.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
Traceback (most recent call last):
File "setup.py", line 456, in <module>
core.setup(**kw)
File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run() …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的EncryptedCharField,我希望在连接UI时基本上显示为CharField,但在数据库中存储/检索之前,它会加密/解密它.
该自定义字段的文件说要:
__metaclass__ = models.SubfieldBase因此,您认为这很容易 - 只需要解密该值,然后再加密它.
松散地基于django片段,该字段的文档如下:
class EncryptedCharField(models.CharField):
"""Just like a char field, but encrypts the value before it enters the database, and decrypts it when it
retrieves it"""
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
super(EncryptedCharField, self).__init__(*args, **kwargs)
cipher_type = kwargs.pop('cipher', 'AES')
self.encryptor = Encryptor(cipher_type)
def get_prep_value(self, value):
return encrypt_if_not_encrypted(value, self.encryptor)
def to_python(self, value):
return decrypt_if_not_decrypted(value, self.encryptor)
def encrypt_if_not_encrypted(value, encryptor):
if isinstance(value, EncryptedString):
return value
else:
encrypted = encryptor.encrypt(value) …Run Code Online (Sandbox Code Playgroud) 我已经将pycrypto(版本2.3)安装到/usr/local/lib/python2.6/dist-packages/Crypto/,我可以在那里看到Random包.
但是当我尝试导入Crypto.Random时,它让我大吃一惊
from Crypto.Random import *
ImportError: No module named Random
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会发生这种情况?谢谢.
import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))
Run Code Online (Sandbox Code Playgroud)
结果:
/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']
Run Code Online (Sandbox Code Playgroud) 我打算在项目中使用PyCrypto,我想知道PyCrypto是否安全可靠,可以使用.如何根据各种加密算法(如RSA和AES)确保PyCrypto正确加密数据?
我试图弄清楚为什么我的Python客户端和Ruby服务器对如何加密数据存在分歧.我在Ruby代码和我的代码中看到的唯一区别是它们没有指定初始化向量,因此它回退到所有\ x0的默认值
当我尝试在没有iv的情况下实例化PyCrypto时它会给我一个错误.这是一个例子:
from Crypto.Cipher import AES
test = "Very, very confidential data"
key = b'Thirty Two Byte key, made Beef y'
gryp = AES.new(key, AES.MODE_CBC)
Run Code Online (Sandbox Code Playgroud)
(这个例子基本上是来自PyCrypto文档的示例代码而没有指定IV)文档说w/r/t IV"它是可选的,当它不存在时,它将被赋予全零的默认值." 但是我得到错误"ValueError:IV必须是16个字节长".
所以我可以指定IV,这不是问题,但我想弄清楚,如果它认为它不能使用默认值,如果我使用库的方式有其他错误.
我试图从Python 2.7.10 64位Windows版本的源代码编译pycrypto-2.6.1并面临以下错误.
Processing pycrypto-2.6.1.tar.gz
Writing c:\users\sivasuba\appdata\local\temp\easy_install-ecznz_\pycrypto-2.6.1\setup.cfg
Running pycrypto-2.6.1\setup.py -q bdist_egg --dist-dir c:\users\sivasuba\appdata\local\temp\easy_install-ecznz_\pycrypto-2.6.1\egg-dist-tmp-us3gka
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
winrand.c
LINK : fatal error LNK1104: cannot open file 'python27.lib'
error: Setup script exited with error: command 'C:\\Users\\sivasuba\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\link.exe' failed with exit status 1104
Run Code Online (Sandbox Code Playgroud)
谷歌搜索没有多大帮助.任何有关这方面的帮助将不胜感激.
PS我不是在寻找预先编译的二进制文件.要求是从源代码构建它.
我正在尝试在ubuntu上安装pycrypto,但它会抛出错误
hom@PC71:~/Desktop/pycrypto-2.3$ sudo python setup.py build
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.6 -c src/MD2.c -o build/temp.linux-i686-2.6/src/MD2.o
src/MD2.c:31: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
我已经安装了python-dev工具.
我试图了解PyCrypto如何在项目中使用,但我还没有完全理解初始化向量(IV)的重要性.我发现在解码字符串时我可以使用错误的IV,除了前16个字节(块大小)之外我似乎仍然收到消息.我只是错误地使用它或不理解某些东西?
这是一个示例代码,用于演示:
import Crypto
import Crypto.Random
from Crypto.Cipher import AES
def pad_data(data):
if len(data) % 16 == 0:
return data
databytes = bytearray(data)
padding_required = 15 - (len(databytes) % 16)
databytes.extend(b'\x80')
databytes.extend(b'\x00' * padding_required)
return bytes(databytes)
def unpad_data(data):
if not data:
return data
data = data.rstrip(b'\x00')
if data[-1] == 128: # b'\x80'[0]:
return data[:-1]
else:
return data
def generate_aes_key():
rnd = Crypto.Random.OSRNG.posix.new().read(AES.block_size)
return rnd
def encrypt(key, iv, data):
aes = AES.new(key, AES.MODE_CBC, iv)
data = pad_data(data)
return aes.encrypt(data)
def decrypt(key, …Run Code Online (Sandbox Code Playgroud) 我正在创建执行不同任务的各种流程.其中一个,只有其中一个,有一个安全模块,可以创建PyCrypto对象.所以我的程序启动,创建各种进程,处理消息的进程使用安全模块解密,我得到以下错误:
firstSymKeybin = self.cipher.decrypt(encFirstSymKeybin, '')
File "/usr/local/lib/python2.7/dist-packages/Crypto/Cipher/PKCS1_v1_5.py", line 206, in decrypt
m = self._key.decrypt(ct)
File "/usr/local/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 174, in decrypt
return pubkey.pubkey.decrypt(self, ciphertext)
File "/usr/local/lib/python2.7/dist-packages/Crypto/PublicKey/pubkey.py", line 93, in decrypt
plaintext=self._decrypt(ciphertext)
File "/usr/local/lib/python2.7/dist-packages/Crypto/PublicKey/RSA.py", line 235, in _decrypt
r = getRandomRange(1, self.key.n-1, randfunc=self._randfunc)
File "/usr/local/lib/python2.7/dist-packages/Crypto/Util/number.py", line 123, in getRandomRange
value = getRandomInteger(bits, randfunc)
File "/usr/local/lib/python2.7/dist-packages/Crypto/Util/number.py", line 104, in getRandomInteger
S = randfunc(N>>3)
File "/usr/local/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 187, in read
return self._singleton.read(bytes)
File "/usr/local/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 163, in read
return _UserFriendlyRNG.read(self, bytes)
File "/usr/local/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line …Run Code Online (Sandbox Code Playgroud) 我用pycrypto生成了一个公钥和私钥,然后使用export key将它们保存到一个文件中:
from Crypto.PublicKey import RSA
bits=2048
new_key = RSA.generate(bits, e=65537)
prv = open('keymac.pem','w')
prv.write(new_key.exportKey('PEM'))
prv.close()
pub = open('pubmac.pem', 'w')
pub.write(new_key.publickey().exportKey('PEM'))
pub.close()
Run Code Online (Sandbox Code Playgroud)
我使用公钥加密文件(参见http://insiderattack.blogspot.com/2014/07/encrypted-file-transfer-utility-in.html#comment-form)
当我读取文件解密时,我得到"密文不正确".
我在Deepal Jayasekara示例的解密代码周围添加了一个try-except块:
try:
encryptedonetimekey = filetodecrypt.read(512)
privatekey = open("keymac.pem", 'r').read()
rsaofprivatekey = RSA.importKey(privatekey)
pkcs1ofprivatekey = PKCS1_OAEP.new(rsaofprivatekey)
aesonetimekey = pkcs1ofprivatekey.decrypt(encryptedonetimekey)
except Exception as decrypprivkeyerr:
print "Decryption of the one time key using the private key failed!!"
print "Key error == %s" %decrypprivkeyerr
raise Exception("Decryption using Private key failed error = %s" %decrypprivkeyerr)
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?我应该以不同方式保存私钥吗?我没有正确读取私钥吗?
pycrypto ×10
python ×9
django ×2
python-2.7 ×2
aes ×1
encryption ×1
gcc ×1
python-3.x ×1
ubuntu ×1