标签归档:pycrypto

使用PyCrypto AES 256加密和解密

问题:使用PyCrypto AES 256加密和解密

我正在尝试使用PyCrypto构建两个接受两个参数的函数:消息和密钥,然后对消息进行加密/解密。

我在网络上找到了几个链接可以帮助我,但每个链接都有缺陷:

在codekoala上,此代码使用os.urandom,PyCrypto不建议这样做。

此外,不能保证我提供给函数的键具有预期的确切长度。我该怎么做才能做到这一点?

另外,有几种模式,推荐哪种?我不知道该怎么用:/

最后,IV到底是什么?我可以提供不同的IV进行加密和解密,还是返回不同的结果?

编辑:删除了代码部分,因为它不安全。

I’m trying to build two functions using PyCrypto that accept two parameters: the message and the key, and then encrypt/decrypt the message.

I found several links on the web to help me out, but each one of them has flaws:

This one at codekoala uses os.urandom, which is discouraged by PyCrypto.

Moreover, the key I give to the function is not guaranteed to have the exact length expected. What can I do to make that happen ?

Also, there are several modes, which one is recommended? I don’t know what to use :/

Finally, what exactly is the IV? Can I provide a different IV for encrypting and decrypting, or will this return in a different result?

Edit: Removed the code part since it was not secure.


回答 0

这是我的实现,并通过一些修复为我工作,并用32字节和iv到16字节增强了密钥和秘密短语的对齐方式:

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES

class AESCipher(object):

    def __init__(self, key): 
        self.bs = AES.block_size
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, raw):
        raw = self._pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw.encode()))

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s)-1:])]

Here is my implementation and works for me with some fixes and enhances the alignment of the key and secret phrase with 32 bytes and iv to 16 bytes:

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES

class AESCipher(object):

    def __init__(self, key): 
        self.bs = AES.block_size
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, raw):
        raw = self._pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw.encode()))

    def decrypt(self, enc):
        enc = base64.b64decode(enc)
        iv = enc[:AES.block_size]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')

    def _pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

    @staticmethod
    def _unpad(s):
        return s[:-ord(s[len(s)-1:])]

回答 1

您可能需要以下两个功能:padunpad当输入的长度不是BLOCK_SIZE的倍数时,填充(执行加密时)和-取消填充(执行解密时)。

BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[:-ord(s[len(s)-1:])]

所以您要问密钥的长度?您可以使用密钥的md5sum而不是直接使用它。

而且,根据我使用PyCrypto的经验,在输入相同的情况下,IV用于混合加密输出,因此将IV选择为随机字符串,并将其用作加密输出的一部分,然后用它来解密消息。

这是我的实现,希望它将对您有用:

import base64
from Crypto.Cipher import AES
from Crypto import Random

class AESCipher:
    def __init__( self, key ):
        self.key = key

    def encrypt( self, raw ):
        raw = pad(raw)
        iv = Random.new().read( AES.block_size )
        cipher = AES.new( self.key, AES.MODE_CBC, iv )
        return base64.b64encode( iv + cipher.encrypt( raw ) ) 

    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc[16:] ))

You may need the following two functions: pad– to pad(when doing encryption) and unpad– to unpad (when doing decryption) when the length of input is not a multiple of BLOCK_SIZE.

BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[:-ord(s[len(s)-1:])]

So you’re asking the length of key? You can use the md5sum of the key rather than use it directly.

More, according to my little experience of using PyCrypto, the IV is used to mix up the output of a encryption when input is same, so the IV is chosen as a random string, and use it as part of the encryption output, and then use it to decrypt the message.

And here’s my implementation, hope it will be useful for you:

import base64
from Crypto.Cipher import AES
from Crypto import Random

class AESCipher:
    def __init__( self, key ):
        self.key = key

    def encrypt( self, raw ):
        raw = pad(raw)
        iv = Random.new().read( AES.block_size )
        cipher = AES.new( self.key, AES.MODE_CBC, iv )
        return base64.b64encode( iv + cipher.encrypt( raw ) ) 

    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc[16:] ))

回答 2

让我解决您有关“模式”的问题。AES256是一种分组密码。它以32字节的密钥和16字节的字符串(称为块)作为输入,并输出一个块。我们在操作模式下使用AES 进行加密。上面的解决方案建议使用CBC,这是一个示例。另一个称为CTR,使用起来更容易一些:

from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random

# AES supports multiple key sizes: 16 (AES128), 24 (AES192), or 32 (AES256).
key_bytes = 32

# Takes as input a 32-byte key and an arbitrary-length plaintext and returns a
# pair (iv, ciphtertext). "iv" stands for initialization vector.
def encrypt(key, plaintext):
    assert len(key) == key_bytes

    # Choose a random, 16-byte IV.
    iv = Random.new().read(AES.block_size)

    # Convert the IV to a Python integer.
    iv_int = int(binascii.hexlify(iv), 16) 

    # Create a new Counter object with IV = iv_int.
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)

    # Create AES-CTR cipher.
    aes = AES.new(key, AES.MODE_CTR, counter=ctr)

    # Encrypt and return IV and ciphertext.
    ciphertext = aes.encrypt(plaintext)
    return (iv, ciphertext)

# Takes as input a 32-byte key, a 16-byte IV, and a ciphertext, and outputs the
# corresponding plaintext.
def decrypt(key, iv, ciphertext):
    assert len(key) == key_bytes

    # Initialize counter for decryption. iv should be the same as the output of
    # encrypt().
    iv_int = int(iv.encode('hex'), 16) 
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)

    # Create AES-CTR cipher.
    aes = AES.new(key, AES.MODE_CTR, counter=ctr)

    # Decrypt and return the plaintext.
    plaintext = aes.decrypt(ciphertext)
    return plaintext

(iv, ciphertext) = encrypt(key, 'hella')
print decrypt(key, iv, ciphertext)

这通常称为AES-CTR。在将AES-CBC与PyCrypto结合使用时,我建议您谨慎使用。原因是它要求您指定填充方案,如其他给出的解决方案所示。通常,如果您对填充不太谨慎,则可以完全破坏加密的攻击

现在,必须注意,密钥必须是一个随机的32字节字符串;密码足够。通常,密钥是这样生成的:

# Nominal way to generate a fresh key. This calls the system's random number
# generator (RNG).
key1 = Random.new().read(key_bytes)

密钥也可以从密码派生

# It's also possible to derive a key from a password, but it's important that
# the password have high entropy, meaning difficult to predict.
password = "This is a rather weak password."

# For added # security, we add a "salt", which increases the entropy.
#
# In this example, we use the same RNG to produce the salt that we used to
# produce key1.
salt_bytes = 8 
salt = Random.new().read(salt_bytes)

# Stands for "Password-based key derivation function 2"
key2 = PBKDF2(password, salt, key_bytes)

上面的一些解决方案建议使用SHA256派生密钥,但这通常被认为是不良的加密做法。查阅Wikipedia,了解更多有关操作模式的信息。

Let me address your question about “modes.” AES256 is a kind of block cipher. It takes as input a 32-byte key and a 16-byte string, called the block and outputs a block. We use AES in a mode of operation in order to encrypt. The solutions above suggest using CBC, which is one example. Another is called CTR, and it’s somewhat easier to use:

from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random

# AES supports multiple key sizes: 16 (AES128), 24 (AES192), or 32 (AES256).
key_bytes = 32

# Takes as input a 32-byte key and an arbitrary-length plaintext and returns a
# pair (iv, ciphtertext). "iv" stands for initialization vector.
def encrypt(key, plaintext):
    assert len(key) == key_bytes

    # Choose a random, 16-byte IV.
    iv = Random.new().read(AES.block_size)

    # Convert the IV to a Python integer.
    iv_int = int(binascii.hexlify(iv), 16) 

    # Create a new Counter object with IV = iv_int.
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)

    # Create AES-CTR cipher.
    aes = AES.new(key, AES.MODE_CTR, counter=ctr)

    # Encrypt and return IV and ciphertext.
    ciphertext = aes.encrypt(plaintext)
    return (iv, ciphertext)

# Takes as input a 32-byte key, a 16-byte IV, and a ciphertext, and outputs the
# corresponding plaintext.
def decrypt(key, iv, ciphertext):
    assert len(key) == key_bytes

    # Initialize counter for decryption. iv should be the same as the output of
    # encrypt().
    iv_int = int(iv.encode('hex'), 16) 
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)

    # Create AES-CTR cipher.
    aes = AES.new(key, AES.MODE_CTR, counter=ctr)

    # Decrypt and return the plaintext.
    plaintext = aes.decrypt(ciphertext)
    return plaintext

(iv, ciphertext) = encrypt(key, 'hella')
print decrypt(key, iv, ciphertext)

This is often referred to as AES-CTR. I would advise caution in using AES-CBC with PyCrypto. The reason is that it requires you to specify the padding scheme, as exemplified by the other solutions given. In general, if you’re not very careful about the padding, there are attacks that completely break encryption!

Now, it’s important to note that the key must be a random, 32-byte string; a password does not suffice. Normally, the key is generated like so:

# Nominal way to generate a fresh key. This calls the system's random number
# generator (RNG).
key1 = Random.new().read(key_bytes)

A key may be derived from a password, too:

# It's also possible to derive a key from a password, but it's important that
# the password have high entropy, meaning difficult to predict.
password = "This is a rather weak password."

# For added # security, we add a "salt", which increases the entropy.
#
# In this example, we use the same RNG to produce the salt that we used to
# produce key1.
salt_bytes = 8 
salt = Random.new().read(salt_bytes)

# Stands for "Password-based key derivation function 2"
key2 = PBKDF2(password, salt, key_bytes)

Some solutions above suggest using SHA256 for deriving the key, but this is generally considered bad cryptographic practice. Check out wikipedia for more on modes of operation.


回答 3

对于想使用urlsafe_b64encode和urlsafe_b64decode的用户,以下是对我有用的版本(花了一些时间处理unicode问题之后)

BS = 16
key = hashlib.md5(settings.SECRET_KEY).hexdigest()[:BS]
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[:-ord(s[len(s)-1:])]

class AESCipher:
    def __init__(self, key):
        self.key = key

    def encrypt(self, raw):
        raw = pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.urlsafe_b64encode(iv + cipher.encrypt(raw)) 

    def decrypt(self, enc):
        enc = base64.urlsafe_b64decode(enc.encode('utf-8'))
        iv = enc[:BS]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return unpad(cipher.decrypt(enc[BS:]))

For someone who would like to use urlsafe_b64encode and urlsafe_b64decode, here are the version that’re working for me (after spending some time with the unicode issue)

BS = 16
key = hashlib.md5(settings.SECRET_KEY).hexdigest()[:BS]
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[:-ord(s[len(s)-1:])]

class AESCipher:
    def __init__(self, key):
        self.key = key

    def encrypt(self, raw):
        raw = pad(raw)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.urlsafe_b64encode(iv + cipher.encrypt(raw)) 

    def decrypt(self, enc):
        enc = base64.urlsafe_b64decode(enc.encode('utf-8'))
        iv = enc[:BS]
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return unpad(cipher.decrypt(enc[BS:]))

回答 4

您可以使用SHA-1或SHA-256之类的加密哈希函数(不是 Python的内置函数)从任意密码中获取密码短语hash。Python在其标准库中包括对两者的支持:

import hashlib

hashlib.sha1("this is my awesome password").digest() # => a 20 byte string
hashlib.sha256("another awesome password").digest() # => a 32 byte string

您可以仅使用[:16]或来截断加密哈希值,[:24]并且它将在指定长度内保持其安全性。

You can get a passphrase out of an arbitrary password by using a cryptographic hash function (NOT Python’s builtin hash) like SHA-1 or SHA-256. Python includes support for both in its standard library:

import hashlib

hashlib.sha1("this is my awesome password").digest() # => a 20 byte string
hashlib.sha256("another awesome password").digest() # => a 32 byte string

You can truncate a cryptographic hash value just by using [:16] or [:24] and it will retain its security up to the length you specify.


回答 5

感谢其他启发但对我不起作用的答案。

在花了数小时试图弄清楚它是如何工作之后,我想到了下面的实现,并带有最新的PyCryptodomex库(这是我如何在Windows上的virtualenv .. phew中成功设置它的代理)

。在实现时,请记住写下填充,编码,加密步骤(反之亦然)。您必须打包和拆包,并牢记顺序。

导入base64
导入hashlib
从Cryptodome.Cipher导入AES
从Cryptodome.Random导入get_random_bytes

__key__ = hashlib.sha256(b'16个字符的键').digest()

def加密(原始):
    BS = AES.block_size
    pad = lambda s:s +(BS-len%BS)* chr(BS-len%BS)

    原始= base64.b64encode(pad(raw).encode('utf8'))
    iv = get_random_bytes(AES.block_size)
    密码= AES.new(密钥= __密钥__,模式= AES.MODE_CFB,iv = iv)
    返回base64.b64encode(iv + cipher.encrypt(raw))

def解密(enc):
    unpad = lambda s:s [:-ord(s [-1:])]

    enc = base64.b64decode(enc)
    iv = enc [:AES.block_size]
    cipher = AES.new(__ key__,AES.MODE_CFB,iv)
    返回unpad(base64.b64decode(cipher.decrypt(enc [AES.block_size:]))。decode('utf8'))

Grateful for the other answers which inspired but didn’t work for me.

After spending hours trying to figure out how it works, I came up with the implementation below with the newest PyCryptodomex library (it is another story how I managed to set it up behind proxy, on Windows, in a virtualenv.. phew)

Working on your implementation, remember to write down padding, encoding, encrypting steps (and vice versa). You have to pack and unpack keeping in mind the order.

import base64
import hashlib
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes

__key__ = hashlib.sha256(b'16-character key').digest()

def encrypt(raw):
    BS = AES.block_size
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

    raw = base64.b64encode(pad(raw).encode('utf8'))
    iv = get_random_bytes(AES.block_size)
    cipher = AES.new(key= __key__, mode= AES.MODE_CFB,iv= iv)
    return base64.b64encode(iv + cipher.encrypt(raw))

def decrypt(enc):
    unpad = lambda s: s[:-ord(s[-1:])]

    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(__key__, AES.MODE_CFB, iv)
    return unpad(base64.b64decode(cipher.decrypt(enc[AES.block_size:])).decode('utf8'))

回答 6

为了他人的利益,这是我结合@Cyril和@Marcus的答案所获得的解密实现。假定此消息是通过HTTP请求传入的,该消息带有quoted和base64编码。

import base64
import urllib2
from Crypto.Cipher import AES


def decrypt(quotedEncodedEncrypted):
    key = 'SecretKey'

    encodedEncrypted = urllib2.unquote(quotedEncodedEncrypted)

    cipher = AES.new(key)
    decrypted = cipher.decrypt(base64.b64decode(encodedEncrypted))[:16]

    for i in range(1, len(base64.b64decode(encodedEncrypted))/16):
        cipher = AES.new(key, AES.MODE_CBC, base64.b64decode(encodedEncrypted)[(i-1)*16:i*16])
        decrypted += cipher.decrypt(base64.b64decode(encodedEncrypted)[i*16:])[:16]

    return decrypted.strip()

For the benefit of others, here is my decryption implementation which I got to by combining the answers of @Cyril and @Marcus. This assumes that this coming in via HTTP Request with the encryptedText quoted and base64 encoded.

import base64
import urllib2
from Crypto.Cipher import AES


def decrypt(quotedEncodedEncrypted):
    key = 'SecretKey'

    encodedEncrypted = urllib2.unquote(quotedEncodedEncrypted)

    cipher = AES.new(key)
    decrypted = cipher.decrypt(base64.b64decode(encodedEncrypted))[:16]

    for i in range(1, len(base64.b64decode(encodedEncrypted))/16):
        cipher = AES.new(key, AES.MODE_CBC, base64.b64decode(encodedEncrypted)[(i-1)*16:i*16])
        decrypted += cipher.decrypt(base64.b64decode(encodedEncrypted)[i*16:])[:16]

    return decrypted.strip()

回答 7

对此的另一种看法(很大程度上来自上述解决方案),但

  • 使用null进行填充
  • 不使用lambda(从不成为粉丝)
  • 用python 2.7和3.6.5测试

    #!/usr/bin/python2.7
    # you'll have to adjust for your setup, e.g., #!/usr/bin/python3
    
    
    import base64, re
    from Crypto.Cipher import AES
    from Crypto import Random
    from django.conf import settings
    
    class AESCipher:
        """
          Usage:
          aes = AESCipher( settings.SECRET_KEY[:16], 32)
          encryp_msg = aes.encrypt( 'ppppppppppppppppppppppppppppppppppppppppppppppppppppppp' )
          msg = aes.decrypt( encryp_msg )
          print("'{}'".format(msg))
        """
        def __init__(self, key, blk_sz):
            self.key = key
            self.blk_sz = blk_sz
    
        def encrypt( self, raw ):
            if raw is None or len(raw) == 0:
                raise NameError("No value given to encrypt")
            raw = raw + '\0' * (self.blk_sz - len(raw) % self.blk_sz)
            raw = raw.encode('utf-8')
            iv = Random.new().read( AES.block_size )
            cipher = AES.new( self.key.encode('utf-8'), AES.MODE_CBC, iv )
            return base64.b64encode( iv + cipher.encrypt( raw ) ).decode('utf-8')
    
        def decrypt( self, enc ):
            if enc is None or len(enc) == 0:
                raise NameError("No value given to decrypt")
            enc = base64.b64decode(enc)
            iv = enc[:16]
            cipher = AES.new(self.key.encode('utf-8'), AES.MODE_CBC, iv )
            return re.sub(b'\x00*$', b'', cipher.decrypt( enc[16:])).decode('utf-8')

Another take on this (heavily derived from solutions above) but

  • uses null for padding
  • does not use lambda (never been a fan)
  • tested with python 2.7 and 3.6.5

    #!/usr/bin/python2.7
    # you'll have to adjust for your setup, e.g., #!/usr/bin/python3
    
    
    import base64, re
    from Crypto.Cipher import AES
    from Crypto import Random
    from django.conf import settings
    
    class AESCipher:
        """
          Usage:
          aes = AESCipher( settings.SECRET_KEY[:16], 32)
          encryp_msg = aes.encrypt( 'ppppppppppppppppppppppppppppppppppppppppppppppppppppppp' )
          msg = aes.decrypt( encryp_msg )
          print("'{}'".format(msg))
        """
        def __init__(self, key, blk_sz):
            self.key = key
            self.blk_sz = blk_sz
    
        def encrypt( self, raw ):
            if raw is None or len(raw) == 0:
                raise NameError("No value given to encrypt")
            raw = raw + '\0' * (self.blk_sz - len(raw) % self.blk_sz)
            raw = raw.encode('utf-8')
            iv = Random.new().read( AES.block_size )
            cipher = AES.new( self.key.encode('utf-8'), AES.MODE_CBC, iv )
            return base64.b64encode( iv + cipher.encrypt( raw ) ).decode('utf-8')
    
        def decrypt( self, enc ):
            if enc is None or len(enc) == 0:
                raise NameError("No value given to decrypt")
            enc = base64.b64decode(enc)
            iv = enc[:16]
            cipher = AES.new(self.key.encode('utf-8'), AES.MODE_CBC, iv )
            return re.sub(b'\x00*$', b'', cipher.decrypt( enc[16:])).decode('utf-8')
    

回答 8

我都用了CryptoPyCryptodomex库,它是速度极快…

import base64
import hashlib
from Cryptodome.Cipher import AES as domeAES
from Cryptodome.Random import get_random_bytes
from Crypto import Random
from Crypto.Cipher import AES as cryptoAES

BLOCK_SIZE = AES.block_size

key = "my_secret_key".encode()
__key__ = hashlib.sha256(key).digest()
print(__key__)

def encrypt(raw):
    BS = cryptoAES.block_size
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
    raw = base64.b64encode(pad(raw).encode('utf8'))
    iv = get_random_bytes(cryptoAES.block_size)
    cipher = cryptoAES.new(key= __key__, mode= cryptoAES.MODE_CFB,iv= iv)
    a= base64.b64encode(iv + cipher.encrypt(raw))
    IV = Random.new().read(BLOCK_SIZE)
    aes = domeAES.new(__key__, domeAES.MODE_CFB, IV)
    b = base64.b64encode(IV + aes.encrypt(a))
    return b

def decrypt(enc):
    passphrase = __key__
    encrypted = base64.b64decode(enc)
    IV = encrypted[:BLOCK_SIZE]
    aes = domeAES.new(passphrase, domeAES.MODE_CFB, IV)
    enc = aes.decrypt(encrypted[BLOCK_SIZE:])
    unpad = lambda s: s[:-ord(s[-1:])]
    enc = base64.b64decode(enc)
    iv = enc[:cryptoAES.block_size]
    cipher = cryptoAES.new(__key__, cryptoAES.MODE_CFB, iv)
    b=  unpad(base64.b64decode(cipher.decrypt(enc[cryptoAES.block_size:])).decode('utf8'))
    return b

encrypted_data =encrypt("Hi Steven!!!!!")
print(encrypted_data)
print("=======")
decrypted_data = decrypt(encrypted_data)
print(decrypted_data)

I have used both Crypto and PyCryptodomex library and it is blazing fast…

import base64
import hashlib
from Cryptodome.Cipher import AES as domeAES
from Cryptodome.Random import get_random_bytes
from Crypto import Random
from Crypto.Cipher import AES as cryptoAES

BLOCK_SIZE = AES.block_size

key = "my_secret_key".encode()
__key__ = hashlib.sha256(key).digest()
print(__key__)

def encrypt(raw):
    BS = cryptoAES.block_size
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
    raw = base64.b64encode(pad(raw).encode('utf8'))
    iv = get_random_bytes(cryptoAES.block_size)
    cipher = cryptoAES.new(key= __key__, mode= cryptoAES.MODE_CFB,iv= iv)
    a= base64.b64encode(iv + cipher.encrypt(raw))
    IV = Random.new().read(BLOCK_SIZE)
    aes = domeAES.new(__key__, domeAES.MODE_CFB, IV)
    b = base64.b64encode(IV + aes.encrypt(a))
    return b

def decrypt(enc):
    passphrase = __key__
    encrypted = base64.b64decode(enc)
    IV = encrypted[:BLOCK_SIZE]
    aes = domeAES.new(passphrase, domeAES.MODE_CFB, IV)
    enc = aes.decrypt(encrypted[BLOCK_SIZE:])
    unpad = lambda s: s[:-ord(s[-1:])]
    enc = base64.b64decode(enc)
    iv = enc[:cryptoAES.block_size]
    cipher = cryptoAES.new(__key__, cryptoAES.MODE_CFB, iv)
    b=  unpad(base64.b64decode(cipher.decrypt(enc[cryptoAES.block_size:])).decode('utf8'))
    return b

encrypted_data =encrypt("Hi Steven!!!!!")
print(encrypted_data)
print("=======")
decrypted_data = decrypt(encrypted_data)
print(decrypted_data)

回答 9

还不晚,但是我认为这将非常有帮助。没有人提及像PKCS#7填充这样的使用方案。您可以使用它代替以前的函数进行填充(加密时)和取消填充(解密时)。i将在下面提供完整的源代码。

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
import pkcs7
class Encryption:

    def __init__(self):
        pass

    def Encrypt(self, PlainText, SecurePassword):
        pw_encode = SecurePassword.encode('utf-8')
        text_encode = PlainText.encode('utf-8')

        key = hashlib.sha256(pw_encode).digest()
        iv = Random.new().read(AES.block_size)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        pad_text = pkcs7.encode(text_encode)
        msg = iv + cipher.encrypt(pad_text)

        EncodeMsg = base64.b64encode(msg)
        return EncodeMsg

    def Decrypt(self, Encrypted, SecurePassword):
        decodbase64 = base64.b64decode(Encrypted.decode("utf-8"))
        pw_encode = SecurePassword.decode('utf-8')

        iv = decodbase64[:AES.block_size]
        key = hashlib.sha256(pw_encode).digest()

        cipher = AES.new(key, AES.MODE_CBC, iv)
        msg = cipher.decrypt(decodbase64[AES.block_size:])
        pad_text = pkcs7.decode(msg)

        decryptedString = pad_text.decode('utf-8')
        return decryptedString

import StringIO
import binascii


def decode(text, k=16):
    nl = len(text)
    val = int(binascii.hexlify(text[-1]), 16)
    if val > k:
        raise ValueError('Input is not padded or padding is corrupt')

    l = nl - val
    return text[:l]


def encode(text, k=16):
    l = len(text)
    output = StringIO.StringIO()
    val = k - (l % k)
    for _ in xrange(val):
        output.write('%02x' % val)
    return text + binascii.unhexlify(output.getvalue())

It’s little late but i think this will be very helpful. No one mention about use scheme like PKCS#7 padding. You can use it instead the previous functions to pad(when do encryption) and unpad(when do decryption).i will provide the full Source Code below.

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
import pkcs7
class Encryption:

    def __init__(self):
        pass

    def Encrypt(self, PlainText, SecurePassword):
        pw_encode = SecurePassword.encode('utf-8')
        text_encode = PlainText.encode('utf-8')

        key = hashlib.sha256(pw_encode).digest()
        iv = Random.new().read(AES.block_size)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        pad_text = pkcs7.encode(text_encode)
        msg = iv + cipher.encrypt(pad_text)

        EncodeMsg = base64.b64encode(msg)
        return EncodeMsg

    def Decrypt(self, Encrypted, SecurePassword):
        decodbase64 = base64.b64decode(Encrypted.decode("utf-8"))
        pw_encode = SecurePassword.decode('utf-8')

        iv = decodbase64[:AES.block_size]
        key = hashlib.sha256(pw_encode).digest()

        cipher = AES.new(key, AES.MODE_CBC, iv)
        msg = cipher.decrypt(decodbase64[AES.block_size:])
        pad_text = pkcs7.decode(msg)

        decryptedString = pad_text.decode('utf-8')
        return decryptedString

import StringIO
import binascii


def decode(text, k=16):
    nl = len(text)
    val = int(binascii.hexlify(text[-1]), 16)
    if val > k:
        raise ValueError('Input is not padded or padding is corrupt')

    l = nl - val
    return text[:l]


def encode(text, k=16):
    l = len(text)
    output = StringIO.StringIO()
    val = k - (l % k)
    for _ in xrange(val):
        output.write('%02x' % val)
    return text + binascii.unhexlify(output.getvalue())


回答 10

https://stackoverflow.com/a/21928790/11402877

兼容的utf-8编码

def _pad(self, s):
    s = s.encode()
    res = s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs).encode()
    return res

https://stackoverflow.com/a/21928790/11402877

compatible utf-8 encoding

def _pad(self, s):
    s = s.encode()
    res = s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs).encode()
    return res

回答 11

from Crypto import Random
from Crypto.Cipher import AES
import base64

BLOCK_SIZE=16
def trans(key):
     return md5.new(key).digest()

def encrypt(message, passphrase):
    passphrase = trans(passphrase)
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return base64.b64encode(IV + aes.encrypt(message))

def decrypt(encrypted, passphrase):
    passphrase = trans(passphrase)
    encrypted = base64.b64decode(encrypted)
    IV = encrypted[:BLOCK_SIZE]
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return aes.decrypt(encrypted[BLOCK_SIZE:])
from Crypto import Random
from Crypto.Cipher import AES
import base64

BLOCK_SIZE=16
def trans(key):
     return md5.new(key).digest()

def encrypt(message, passphrase):
    passphrase = trans(passphrase)
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return base64.b64encode(IV + aes.encrypt(message))

def decrypt(encrypted, passphrase):
    passphrase = trans(passphrase)
    encrypted = base64.b64decode(encrypted)
    IV = encrypted[:BLOCK_SIZE]
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return aes.decrypt(encrypted[BLOCK_SIZE:])

ImportError:没有名为Crypto.Cipher的模块

问题:ImportError:没有名为Crypto.Cipher的模块

当我尝试运行app.py(Python 3.3,PyCrypto 2.6)时,我的virtualenv不断返回上面列出的错误。我的进口货单是from Crypto.Cipher import AES。我在寻找重复项,您可能会说有重复项,但是我尝试了解决方案(尽管大多数都不是解决方案),但没有任何效果。

您可以在下面查看PyCrypto的文件格式:

When I try to run app.py (Python 3.3, PyCrypto 2.6) my virtualenv keeps returning the error listed above. My import statement is just from Crypto.Cipher import AES. I looked for duplicates and you might say that there are some, but I tried the solutions (although most are not even solutions) and nothing worked.

You can see what the files are like for PyCrypto below:


回答 0

我有同样的问题(尽管在Linux上)。解决方案非常简单-添加:

libraries:
- name: pycrypto
  version: "2.6"

到我的app.yaml文件。由于过去该方法正常工作,因此我认为这是一个新要求。

I had the same problem (though on Linux). The solution was quite simple – add:

libraries:
- name: pycrypto
  version: "2.6"

to my app.yaml file. Since this worked correctly in the past, I assume this is a new requirement.


回答 1

使用进行安装时,在Mac上出现相同的问题pip。然后pycrypto,我使用删除并重新安装了它easy_install,如下所示:

pip uninstall pycrypto
easy_install pycrypto

就像卢克(Luke)所说:如果您在运行这些命令时遇到问题,请确保以admin(sudo)身份运行它们

希望这可以帮助!

编辑:正如winklerr在上面正确指出的那样,pycrypto不再安全。改用pycryptodome,它是一个替代品

I had the same problem on my Mac when installing with pip. I then removed pycrypto and installed it again with easy_install, like this:

pip uninstall pycrypto
easy_install pycrypto

also as Luke commented: If you have trouble running these commands, be sure to run them as admin (sudo)

Hope this helps!

EDIT: As winklerr correctly notes above, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement


回答 2

我也在Mac上遇到了这个问题,它似乎与通过pip在pycrypto旁边安装了一个不幸的名字相似的“ crypto”模块(不确定该用于什么目的)有关。

该修复程序似乎正在通过pip删除crypto和pycrypto:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

并重新安装pycrypto:

sudo pip install pycrypto

现在,当我执行以下操作时,它可以按预期工作:

from Crypto.Cipher import AES

I ran into this on Mac as well, and it seems to be related to having an unfortunately similarly named “crypto” module (not sure what that is for) installed alongside of pycrypto via pip.

The fix seems to be removing both crypto and pycrypto with pip:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

and reinstalling pycrypto:

sudo pip install pycrypto

Now it works as expected when I do something like:

from Crypto.Cipher import AES

回答 3

在Mac上…如果遇到此问题,请尝试是否可以导入加密货币?

如果是这样..包名是问题CVS c。要解决此问题,只需将这些行添加到脚本的顶部。

import crypto
import sys
sys.modules['Crypto'] = crypto

您知道应该能够成功导入paramiko。

On the mac… if you run into this.. try to see if you can import crypto instead?

If so.. the package name is the issue C vs c. To get around this.. just add these lines to the top of your script.

import crypto
import sys
sys.modules['Crypto'] = crypto

You know should be able to import paramiko successfully.


回答 4

正在卸载cryptopycrypto在我身上工作。然后仅安装pycrypto

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto

Uninstalling crypto and pycrypto works on me. Then install only pycrypto:

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto

回答 5

我找到了解决方案。问题可能是区分大小写(在Windows上)。

只需更改文件夹的名称:

  • C:\Python27\Lib\site-packages\crypto
  • 至: C:\Python27\Lib\site-packages\Crypto

这是在安装pycrypto之后命名文件夹的方式:

我将其更改为:

现在,以下代码可以正常工作:

I found the solution. Issue is probably in case sensitivity (on Windows).

Just change the name of the folder:

  • C:\Python27\Lib\site-packages\crypto
  • to: C:\Python27\Lib\site-packages\Crypto

This is how folder was named after installation of pycrypto:

I’ve changed it to:

And now the following code works fine:


回答 6

警告:请勿使用 pycrypto

正如你可以在阅读此页,的用法pycrypto不是安全了:

Pycrypto容易受block_templace.c中ALGnew函数中基于堆的缓冲区溢出的影响。它允许远程攻击者在python应用程序中执行任意代码。它被分配了CVE-2013-7459号。

自2014年6月20日以来,Pycrypto尚未发布对该漏洞的任何修复程序,也没有对该项目进行任何提交。

解决方案:使用Python3和pycryptodome

TL; DR: pip3 install pycryptodome

确保先卸载其他版本cryptopycrypto

设置新的虚拟环境

要安装虚拟环境并设置所有内容,请使用以下命令:

# install python3 and pip3
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip

# install virtualenv
pip3 install virtualenv

# install and create a virtual environment in your target folder
mkdir target_folder
cd target_folder
python3 -m virtualenv .

# now activate your venv and install pycryptodome
source bin/activate
pip3 install pycryptodome

# check if everything worked: 
# start the interactive python console and import the Crypto module
# when there is no import error then it worked
python
>>> from Crypto.Cipher import AES
>>> exit()

# don't forget to deactivate your venv again
deactivate

有关更多信息,请参见pycryptodome.org。

WARNING: Don’t use pycrypto anymore!

As you can read on this page, the usage of pycrypto is not safe anymore:

Pycrypto is vulnerable to a heap-based buffer overflow in the ALGnew function in block_templace.c. It allows remote attackers to execute arbitrary code in the python application. It was assigned the CVE-2013-7459 number.

Pycrypto didn’t release any fix to that vulnerability and no commit was made to the project since Jun 20, 2014.

Use Python3 and pycryptodome instead!

pip3 uninstall crypto 
pip3 uninstall pycrypto 
pip3 install pycryptodome

Make sure to uninstall other versions of crypto or pycrypto first because both packages install under the same folder Crypto where also pycryptodome will be installed.

Best practice: virtual environments

In order to avoid problems with pip packages in different versions or packages that install under the same folder (i.e. pycrypto and pycryptodome) you can make use of a so called virtual environment. There, the installed pip packages can be managed for every single project individually.

To install a virtual environment and setup everything, use the following commands:

# install python3 and pip3
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip

# install virtualenv
pip3 install virtualenv

# install and create a virtual environment in your target folder
mkdir target_folder
cd target_folder
python3 -m virtualenv .

# now activate your venv and install pycryptodome
source bin/activate
pip3 install pycryptodome

# check if everything worked: 
# start the interactive python console and import the Crypto module
# when there is no import error then it worked
python
>>> from Crypto.Cipher import AES
>>> exit()

# don't forget to deactivate your venv again
deactivate

For more information, see pycryptodome.org


回答 7

输入命令:

sudo pip install pycrypto

type command:

sudo pip install pycrypto

回答 8

如果您使用的是redhat,fedora,centos:

sudo yum install pycrypto

就我而言,我不会使用pip安装它

if you are using redhat,fedora, centos :

sudo yum install pycrypto

for my case I coouldnot install it using pip


回答 9

我遇到了同样的问题 'ImportError: No module named Crypto.Cipher',因为在OSX 10.8.5(Mountain Lion)上将GoogleAppEngineLauncher(版本> 1.8.X)与GAE Boilerplate一起使用。在具有python 2.7运行时的Google App Engine SDK中,建议使用pyCrypto 2.6。对我有用的解决方案是…

1)下载pycrypto2.6源将其解压缩到某处(~/Downloads/pycrypto26

例如,git clone https://github.com/dlitz/pycrypto.git

2)cdcd ~/Downloads/pycrypto26)然后

3)在上一个文件夹中执行以下终端命令,以便在GAE文件夹中手动安装pyCrypto 2.6。

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine

I’ve had the same problem 'ImportError: No module named Crypto.Cipher', since using GoogleAppEngineLauncher (version > 1.8.X) with GAE Boilerplate on OSX 10.8.5 (Mountain Lion). In Google App Engine SDK with python 2.7 runtime, pyCrypto 2.6 is the suggested version. The solution that worked for me was…

1) Download pycrypto2.6 source extract it somewhere(~/Downloads/pycrypto26)

e.g., git clone https://github.com/dlitz/pycrypto.git

2) cd (cd ~/Downloads/pycrypto26) then

3) Execute the following terminal command inside the previous folder in order to install pyCrypto 2.6 manually in GAE folder.

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine

回答 10

如果您是macOS,请将lib文件夹重命名lib/python3.7/site-packages/cryptolib/python3.7/site-packages/Crypto

If you an macos, rename lib folder lib/python3.7/site-packages/crypto to lib/python3.7/site-packages/Crypto


回答 11

尝试使用pip3

sudo pip3 install pycrypto

Try with pip3:

sudo pip3 install pycrypto

回答 12

加载通过pip安装的python模块可能是一个问题。参考此答案无法从site-packages目录加载通过pip安装的Python模块,并尝试类似

python -m pip install pycrypto

It could be a problem of loading python modules installed via pip. Refer to this answer Can’t load Python modules installed via pip from site-packages directory and try something like

python -m pip install pycrypto

回答 13

为我工作(Ubuntu 17.10)

删除venv并使用python v3.6重新创建

pip3 install PyJWT
sudo apt-get install build-essential libgmp3-dev python3-dev
pip3 install cryptography
pip3 install pycryptodome
pip3 install pycryptodomex

Pycrypto已过时,有问题,使用Pycryptodome

Worked for me (Ubuntu 17.10)

Removing venv and creating it again with python v3.6

pip3 install PyJWT
sudo apt-get install build-essential libgmp3-dev python3-dev
pip3 install cryptography
pip3 install pycryptodome
pip3 install pycryptodomex

Pycrypto is deprecated, had problems with it, used Pycryptodome


回答 14

我通过将首字母大写更改为大写来解决此问题。确保“从Crypto.Cipher导入AES”而不是“从crypto.Cipher导入AES”。

I solve this problem by change the first letter case to upper. Make sure ”from Crypto.Cipher import AES” not ”from crypto.Cipher import AES”.


回答 15

对于CentOS 7.4,我首先安装了pip,然后使用pip安装了pycrypto:

> sudo yum -y install python-pip 
> sudo python -m pip install pycrypto

For CentOS 7.4 I first installed pip and then pycrypto using pip:

> sudo yum -y install python-pip 
> sudo python -m pip install pycrypto

回答 16

迄今为止,from Crypto.Cipher import AES即使我安装/重新安装了pycrypto几次,导入时也遇到相同的问题。最终是因为pip默认为python3。

~ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

使用pip2安装pycrypto应该可以解决此问题。

To date, I’m having same issue when importing from Crypto.Cipher import AES even when I’ve installed/reinstalled pycrypto a few times. End up it’s because pip defaulted to python3.

~ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

installing pycrypto with pip2 should solve this issue.


回答 17

对于Windows 7:

我遇到了这个错误“模块错误Crypo.Cipher导入AES”

要在Windows中安装Pycrypto,

在命令提示符中尝试此操作,

设置路径= C:\ Python27 \ Scripts(即easy_install所在的路径)

然后执行以下命令

easy_install pycrypto

对于Ubuntu:

试试这个,

从“ https://pypi.python.org/pypi/pycrypto下载Pycrypto

然后使用终端将当前路径更改为下载路径:

例如:root @ xyz-virtual-machine:〜/ pycrypto-2.6.1#

然后使用终端执行以下命令:

python setup.py安装

对我有用。希望为所有人服务。

For Windows 7:

I got through this error “Module error Crypo.Cipher import AES”

To install Pycrypto in Windows,

Try this in Command Prompt,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)

Then execute the following,

easy_install pycrypto

For Ubuntu:

Try this,

Download Pycrypto from “https://pypi.python.org/pypi/pycrypto

Then change your current path to downloaded path using your terminal:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#

Then execute the following using the terminal:

python setup.py install

It’s worked for me. Hope works for all..


回答 18

可以通过安装C ++编译器(python27或python26)来解决此问题。从Microsoft https://www.microsoft.com/zh-cn/download/details.aspx?id=44266下载它,然后重新运行命令:pip install pycrypto当您杀死.NET 进程时运行gui Web访问easy_install.exe

This problem can be fixed by installing the C++ compiler (python27 or python26). Download it from Microsoft https://www.microsoft.com/en-us/download/details.aspx?id=44266 and re-run the command : pip install pycrypto to run the gui web access when you kill the process of easy_install.exe.


回答 19

也许您应该这样做:pycryptodome == 3.6.1将其添加到requirements.txt并安装,这应该消除错误报告。这个对我有用!

Maybe you should this: pycryptodome==3.6.1 add it to requirements.txt and install, which should eliminate the error report. it works for me!


回答 20

这对我有用

pip install pycryptodome==3.4.3

This worked for me

pip install pycryptodome==3.4.3

回答 21

嗯,这可能会出现奇怪,但安装后pycrypto还是pycryptodome,我们需要更新的目录名cryptoCryptolib/site-packages

参考

Well this might appear weird but after installing pycrypto or pycryptodome , we need to update the directory name crypto to Crypto in lib/site-packages

Reference


回答 22

我是3.7。在我尝试安装加密货币后,问题仍然存在。在我的情况下,pycrypto只是失败了。所以最后我的构建通过下面的包传递:pip install pycryptodome

I’m with 3.7. The issue remains after I try to install crypto. And pycrypto just fails in my case. So in the end my build passed via package below: pip install pycryptodome


如何在Windows上安装PyCrypto?

问题:如何在Windows上安装PyCrypto?

我已经阅读了所有其他Google来源和SO线程,但没有任何效果。

Python 2.7.3 32bit安装在上Windows 7 64bit。下载,解压缩然后尝试在以下位置安装PyCrypto结果"Unable to find vcvarsall.bat".

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是然后我得到了错误"RuntimeError: chmod error".

我该如何解决这个问题?我试过使用pip,它给出相同的结果。我找到了一个预构建的PyCrypto 2.3二进制文件并进行了安装,但是在系统上找不到该文件(无法正常工作)。

有任何想法吗?

I’ve read every other google source and SO thread, with nothing working.

Python 2.7.3 32bit installed on Windows 7 64bit. Download, extracting, and then trying to install PyCrypto results in "Unable to find vcvarsall.bat".

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error "RuntimeError: chmod error".

How in the world do I get around this? I’ve tried using pip, which gives the same result. I found a prebuilt PyCrypto 2.3 binary and installed that, but it’s nowhere to be found on the system (not working).

Any ideas?


回答 0

如果尚未安装与Python.org分发的Visual Studio二进制文件兼容的C / C ++开发环境,则应坚持仅安装纯Python软件包或可用于Windows二进制文件的软件包。

幸运的是,有适用于Windows的PyCrypto二进制文件:http ://www.voidspace.org.uk/python/modules.shtml#pycrypto

更新:
正如@Udi在下面的注释中建议的那样,以下命令也将安装pycrypto并可以在其中使用virtualenv

easy_install http://www.voidspace.org.uk/python/pycrypto-2.6.1/pycrypto-2.6.1.win32-py2.7.exe

注意,请从此列表中选择与您的设置相关的链接

如果您正在寻找适用于Python 3.5的版本,请参阅python 3.5上的PyCrypto

If you don’t already have a C/C++ development environment installed that is compatible with the Visual Studio binaries distributed by Python.org, then you should stick to installing only pure Python packages or packages for which a Windows binary is available.

Fortunately, there are PyCrypto binaries available for Windows: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

UPDATE:
As @Udi suggests in the comment below, the following command also installs pycrypto and can be used in virtualenv as well:

easy_install http://www.voidspace.org.uk/python/pycrypto-2.6.1/pycrypto-2.6.1.win32-py2.7.exe

Notice to choose the relevant link for your setup from this list

If you’re looking for builds for Python 3.5, see PyCrypto on python 3.5


回答 1

Microsoft最近最近发布了用于Python 2.7的独立的专用Microsoft Visual C ++编译器。如果您使用的是Python 2.7,只需安装该编译器和Setuptools 6.0或更高版本,大多数具有C扩展名的软件包现在都可以轻松编译。

Microsoft has recently recently released a standalone, dedicated Microsoft Visual C++ Compiler for Python 2.7. If you’re using Python 2.7, simply install that compiler and Setuptools 6.0 or later, and most packages with C extensions will now compile readily.


回答 2

经过数年之久,python终于同意了一种二进制发行版wheel,该发行版允许在Windows上安装甚至二进制扩展,而无需使用简单的编译器pip install packagename。有一种流行的软件包列表及其状态。Pycrypto还不存在,例如lxml,PySide和Scrapy。

2015年11月编辑pip uninstall pycryptopip install pycryptodome。这是pycrypto具有新功能的前叉,并且支持车轮。它取代了pycrypto,因此现有代码将继续起作用(请参阅https://pycryptodome.readthedocs.org/en/latest/src/examples.html

After years and years, python finally agreed for a binary disribution called wheel which allows to install even binary extensions on Windows without having a compiler with simple pip install packagename. There is a list of popular packages with their status. Pycrypto is not there yet, but lxml, PySide and Scrapy for example.

Edited Nov 2015: pip uninstall pycrypto & pip install pycryptodome. It is a pycrypto fork with new features and it supports wheel. It replaces pycrypto, so existing code will continue to work (see https://pycryptodome.readthedocs.org/en/latest/src/examples.html)


回答 3

对于VS2010:

SET VS90COMNTOOLS=%VS100COMNTOOLS%

对于VS2012:

SET VS90COMNTOOLS=%VS110COMNTOOLS%

然后调用:

pip install pyCrypto 

For VS2010:

SET VS90COMNTOOLS=%VS100COMNTOOLS%

For VS2012:

SET VS90COMNTOOLS=%VS110COMNTOOLS%

then Call:

pip install pyCrypto 

回答 4

一般来说

vcvarsall.bat是Visual C ++编译器的一部分,您需要安装要安装的内容。如果您的Python是使用Visual Studio工具链编译的,甚至不要尝试处理MingGW,反之亦然。甚至Microsoft工具链的版本也很重要。用VS 2008编译的Python不能与用VS 2010编译的扩展一起使用!

您必须使用与Python版本相同的编译器来编译PyCrypto。Google的“无法找到vcvarsall.bat”,因为这是问题的根源,这是在Windows上编译Python扩展的一个非常普遍的问题。

在使用此链接的任何系统上,要获得正确的信息,都需要大量的信息和大量的阅读。

注意使用Visual Studio 2010或不使用Visual Studio 2008

据我所知,以下仍然是正确的。这是在2010年6月的链接中发布的,该链接是指尝试针对python.org上可用的Python安装程序使用VS 2010 Express构建扩展。

如果这样做,请小心。python.org的Python 2.6和2.7是使用Visual Studio 2008编译器构建的。您将需要使用与Python相同的CRT(msvcr90.dll)进行链接。

Visual Studio 2010 Express链接到错误的CRT版本:msvcr100.dll。

如果执行此操作,则还必须使用Visual Studio 2010 Express重新构建Python。您不能将标准的Python二进制安装程序用于Windows。您也不能使用使用与Visual Studio 2010(Express)不同的编译器构建的任何C / C ++扩展。

意见:这是我放弃Windows从事OSX的所有重要开发工作的原因之一!

In general

vcvarsall.bat is part of the Visual C++ compiler, you need that to install what you are trying to install. Don’t even try to deal with MingGW if your Python was compiled with Visual Studio toolchain and vice versa. Even the version of the Microsoft tool chain is important. Python compiled with VS 2008 won’t work with extensions compiled with VS 2010!

You have to compile PyCrypto with the same compiler that the version of Python was compiled with. Google for “Unable to find vcvarsall.bat” because that is the root of your problem, it is a very common problem with compiling Python extensions on Windows.

There is a lot of information and a lot to read to get this right on whatever system you are on with this link.

Beware using Visual Studio 2010 or not using Visual Studio 2008

As far as I know the following is still true. This was posted in the link above in June, 2010 referring to trying to build extensions with VS 2010 Express against the Python installers available on python.org.

Be careful if you do this. Python 2.6 and 2.7 from python.org are built with Visual Studio 2008 compilers. You will need to link with the same CRT (msvcr90.dll) as Python.

Visual Studio 2010 Express links with the wrong CRT version: msvcr100.dll.

If you do this, you must also re-build Python with Visual Studio 2010 Express. You cannot use the standard Python binary installer for Windows. Nor can you use any C/C++ extensions built with a different compiler than Visual Studio 2010 (Express).

Opinion: This is one reason I abandoned Windows for all serious development work for OSX!


回答 5

PyCryptodomePyCrypto的几乎兼容的分支,带有pypi上的Windows轮子。

您可以使用以下简单的方法安装它:

pip install pycryptodome

该网站还包含使用Microsoft编译器从源进行构建的说明。

PyCryptodome is an almost-compatible fork of PyCrypto with Windows wheels available on pypi.

You can install it with a simple:

pip install pycryptodome

The website includes instructions to build it from sources with the Microsoft compilers too.


回答 6

我设法pycrypto通过使用MinGW32和进行编译MSYS。假定您已安装pipeasy_install安装。

这是我的做法:

1)安装MinGW32。为了便于说明,我们假设它已安装在中C:\MinGW。我建议使用安装程序时,选择. MSYS应该随其一起安装的C ++编译器MinGW

2)添加c:\mingw\bin,c:\mingw\mingw32\bin,C:\MinGW\msys\1.0, c:\mingw\msys\1.0\bin and c:\mingw\msys\1.0\sbin到您的中%PATH%。如果您不熟悉,这篇文章会很有帮助。

3)从搜索栏中运行msys,然后MSYS终端将打开。对于熟悉的人Cygwin,它的工作方式类似。

4)pip install pycrypto在此之后,应从MSYS终端内部正常运行。

I have managed to get pycrypto to compile by using MinGW32 and MSYS. This presumes that you have pip or easy_install installed.

Here’s how I did it:

1) Install MinGW32. For the sake of this explanation, let’s assume it’s installed in C:\MinGW. When using the installer, which I recommend, select the C++ compiler. MSYS should install with MinGW

2) Add c:\mingw\bin,c:\mingw\mingw32\bin,C:\MinGW\msys\1.0, c:\mingw\msys\1.0\bin and c:\mingw\msys\1.0\sbin to your %PATH%. If you aren’t familiar, this article is very helpful.

3) From the search bar, run msys and the MSYS terminal will open. For those familiar with Cygwin, it works in a similar fashion.

4) From within the MSYS terminal pip install pycrypto should run without error after this.


回答 7

对于Windows 7:

要在Windows中安装Pycrypto,

在命令提示符中尝试此操作,

设置路径= C:\ Python27 \ Scripts(即easy_install所在的路径)

然后执行以下命令

easy_install pycrypto

对于Ubuntu:

试试这个,

从“ https://pypi.python.org/pypi/pycrypto ” 下载Pycrypto

然后使用终端将当前路径更改为下载路径,并且用户应为root:

例如:root @ xyz-virtual-machine:〜/ pycrypto-2.6.1#

然后使用终端执行以下命令:

python setup.py安装

对我有用。希望为所有人服务。

For Windows 7:

To install Pycrypto in Windows,

Try this in Command Prompt,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)

Then execute the following,

easy_install pycrypto

For Ubuntu:

Try this,

Download Pycrypto from “https://pypi.python.org/pypi/pycrypto

Then change your current path to downloaded path using your terminal and user should be root:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#

Then execute the following using the terminal:

python setup.py install

It’s worked for me. Hope works for all..


回答 8

对于那些正在寻找python 3.4的人,我发现了一个可以正常使用的安装程序的git repo。这是x64的直接链接x32

For those of you looking for python 3.4 I found a git repo with an installer that just works. Here are the direct links for x64 and x32


回答 9

可以使用Windows 7 SDK工具包构建PyCrypto。Windows 7 SDK有两个版本。原始版本(针对.Net 3.5)包括VS 2008命令行编译器。可以同时安装32位和64位编译器。

第一步是编译mpir以提供快速算法。我已经记录了在gmpy库中使用的过程。可以在sdk_build中找到使用SDK编译器构建mpir的详细说明。

在DOS提示符下使用SDK编译器的关键步骤是:

1)根据需要运行vcvars32.bat或vcvars64.bat。

2)在提示符下,执行“ set MSSdk = 1”

3)在提示符下,执行“ set DISTUTILS_USE_SDK = 1”

假设C代码没有其他问题,这应该可以使“ python setup.py install”成功。但是我模糊地记得我不得不编辑几个PyCrypto文件来启用mpir并找到mpir库,但是目前我没有Windows系统。我将需要几天的时间来重新创建这些步骤。如果您到那时还没有报告成功,我将发布PyCrypto步骤。这些步骤将假定您能够编译mpir。

我希望这有帮助。

It’s possible to build PyCrypto using the Windows 7 SDK toolkits. There are two versions of the Windows 7 SDK. The original version (for .Net 3.5) includes the VS 2008 command-line compilers. Both 32- and 64-bit compilers can be installed.

The first step is to compile mpir to provide fast arithmetic. I’ve documented the process I use in the gmpy library. Detailed instructions for building mpir using the SDK compiler can be found at sdk_build

The key steps to use the SDK compilers from a DOS prompt are:

1) Run either vcvars32.bat or vcvars64.bat as appropriate.

2) At the prompt, execute “set MSSdk=1”

3) At the prompt, execute “set DISTUTILS_USE_SDK=1”

This should allow “python setup.py install” to succeed assuming there are no other issues with the C code. But I vaaguely remember that I had to edit a couple of PyCrypto files to enable mpir and to find the mpir libraries but I don’t have my Windows system up at the moment. It will be a couple of days before I’ll have time to recreate the steps. If you haven’t reported success by then, I’ll post the PyCrypto steps. The steps will assume you were able to compile mpir.

I hope this helps.


回答 10

尝试仅使用:

pip install pycryptodome

要么:

pip install pycryptodomex

来源:https : //pypi.python.org/pypi/pycryptodome

Try just using:

pip install pycryptodome

or:

pip install pycryptodomex

Source: https://pypi.python.org/pypi/pycryptodome


回答 11

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是,然后我得到错误“ RuntimeError:chmod错误”。

"RuntimeError: chmod error"发生此错误是因为安装脚本找不到chmod命令。

我该如何解决这个问题?

您只需要将MSYS二进制文件添加到PATH并重新运行安装脚本即可

(注意:MinGW随MSYS一起提供)

例如,如果我们在文件夹中 C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1>

C:\.....>set PATH=C:\MinGW\msys\1.0\bin;%PATH%
C:\.....>python setup.py install

可选:在重新运行脚本之前,您可能需要清理:

`C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1> python setup.py clean`

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error “RuntimeError: chmod error”.

This error "RuntimeError: chmod error" occurs because the install script didn’t find the chmod command.

How in the world do I get around this?

Solution

You only need to add the MSYS binaries to the PATH and re-run the install script.

(N.B: Note that MinGW comes with MSYS so )

Example

For example, if we are in folder C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1>

C:\.....>set PATH=C:\MinGW\msys\1.0\bin;%PATH%
C:\.....>python setup.py install

Optional: you might need to clean before you re-run the script:

`C:\<..>\pycrypto-2.6.1\dist\pycrypto-2.6.1> python setup.py clean`

回答 12

  1. 转到适用于Python 2.7的Microsoft Visual C ++编译器 ”,然后根据“系统要求”继续操作(这是我按照以下步骤进行的工作)。

  2. 安装setuptools(6.0或更高版本需要Python来自动检测setuptools的这个编译器包) 或者通过: pip install setuptools 下载“setuptools的自举安装”源来自于你的filestystem为“ez_python.py”,保存这个文件somwhere与安装:python ez_python.py

  3. 安装滚轮(建议使用滚轮来生产预构建的二进制软件包)。您可以使用以下方法安装它:pip install wheel

  4. 打开Windows提升的命令提示符cmd.exe(带有“以管理员身份运行”),为所有用户安装“适用于Python 2.7的Microsoft Visual C ++编译器”。您可以使用以下命令来执行此操作:msiexec / i C:\users\jozko\download\VCForPython27.msi ALLUSERS=1仅使用您自己的文件路径:msiexec /i <path to MSI> ALLUSERS=1

  5. 现在,您应该能够使用以下命令安装pycrypto: pip install pycrypto

  1. Go to Microsoft Visual C++ Compiler for Python 2.7 and continue based on “System Requirements” (this is what I did to put below steps together).

  2. Install setuptools (setuptools 6.0 or later is required for Python to automatically detect this compiler package) either by: pip install setuptools or download “Setuptools bootstrapping installer” source from, save this file somwhere on your filestystem as “ez_python.py” and install with: python ez_python.py

  3. Install wheel (wheel is recommended for producing pre-built binary packages). You can install it with: pip install wheel

  4. Open Windows elevated Command Prompt cmd.exe (with “Run as administrator”) to install “Microsoft Visual C++ Compiler for Python 2.7” for all users. You can use following command to do so: msiexec /i C:\users\jozko\download\VCForPython27.msi ALLUSERS=1 just use your own path to file: msiexec /i <path to MSI> ALLUSERS=1

  5. Now you should be able to install pycrypto with: pip install pycrypto


回答 13

如果您在Windows上并且正在努力安装Pycrypcto,请使用:pip install pycryptodome。它像奇迹一样运作,比起进行大量的配置和调整将使您的生活更加轻松。

If you are on Windows and struggling with installing Pycrypcto just use the: pip install pycryptodome. It works like a miracle and it will make your life much easier than trying to do a lot of configurations and tweaks.


回答 14

这可能不是最佳解决方案,但是您可以从MS下载并安装免费的Visual C ++ Express软件包。这将为您提供编译PyCrypto代码所需的C ++编译器。

This probably isn’t the optimal solution but you might download and install the free Visual C++ Express package from MS. This will give you the C++ compiler you need to compile the PyCrypto code.


回答 15

我的答案可能与此处提到的问题无关,但是我对Python 3.4遇到了同样的问题,其中Crypto.Cipher不是有效的导入。所以我尝试安装PyCrypto并遇到问题。

经过研究,我发现3.4应该使用pycryptodome

我使用pycharm安装pycryptodome,效果很好。

Crypto.Cipher导入AES

My answer might not be related to problem mention here, but I had same problem with Python 3.4 where Crypto.Cipher wasn’t a valid import. So I tried installing PyCrypto and went into problems.

After some research I found with 3.4 you should use pycryptodome.

I install pycryptodome using pycharm and I was good.

from Crypto.Cipher import AES


回答 16

因此,我安装了MinGW,并将其作为选择的编译器安装在安装线上。但是,然后我得到错误“ RuntimeError:chmod错误”。

您需要在MinGW下安装msys软件包

并在PATH env变量中添加以下条目。

  • C:\MinGW\bin
  • C:\MinGW\msys\1.0\bin [在这里您将找到chmod可执行文件]

然后从正常的Windows命令提示符下运行命令。

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error “RuntimeError: chmod error”.

You need to install msys package under MinGW

and add following entries in your PATH env variable.

  • C:\MinGW\bin
  • C:\MinGW\msys\1.0\bin [This is where you will find chmod executable]

Then run your command from normal windows command prompt.


回答 17

由于奇怪的法律原因,二进制文件无法正常发布。空隙空间通常是最好的第二个来源。但是由于相当长的一段时间,voidspace维护程序没有更新。使用[ https://www.dropbox.com/s/n6rckn0k6u4nqke/pycrypto-2.6.1.zip?dl=0]中的压缩文件

Due to weird legal reasons, binaries are not published the normal way. Voidspace is normally the best second source. But since quite some time, voidspace maintainer did not update. Use the zip from [https://www.dropbox.com/s/n6rckn0k6u4nqke/pycrypto-2.6.1.zip?dl=0]


回答 18

步骤1:从此处安装Visual C ++ 2010 Express 。

(请勿安装Microsoft Visual Studio 2010 Service Pack 1)

步骤2:从“控制面板\程序和功能”中删除所有Microsoft Visual C ++ 2010可再发行程序包。如果您不这样做,则安装将失败,并显示模糊的“安装过程中出现致命错误”错误。

步骤3:从此处安装用于Visual Studio 2010(v7.1)的Windows SDK的脱机版本。这是64位扩展所必需的。Windows内置了对Pismo等ISO的安装。

步骤4:您需要使用Pismo File Mount Audit Package安装ISO文件。从这里下载Pismo

步骤5:右键单击下载的ISO文件,然后选择Pismo挂载。之后,安装Setup \ SDKSetup.exe而不是setup.exe。

步骤6a:通过将目录更改为C:\ Program Files(x86)\ Microsoft Visual Studio版本\ VC \在C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ VC \ bin \ amd64中创建vcvars64.bat文件命令提示符。在命令提示符下键入命令: cd C:\Program Files (x86)\Microsoft Visual Studio version\VC\r

步骤6b:要为针对x86平台的64位命令行构建配置此“命令提示符”窗口,请在命令提示符下输入: vcvarsall x86单击此处以获取更多选项。

步骤7:在命令提示符下,输入以下命令安装PyCrypto: C:\Python3X>pip install -U your_wh_file

Step 1: Install Visual C++ 2010 Express from here.

(Do not install Microsoft Visual Studio 2010 Service Pack 1 )

Step 2: Remove all the Microsoft Visual C++ 2010 Redistributable packages from Control Panel\Programs and Features. If you don’t do those then the install is going to fail with an obscure “Fatal error during installation” error.

Step 3: Install offline version of Windows SDK for Visual Studio 2010 (v7.1) from here. This is required for 64bit extensions. Windows has builtin mounting for ISOs like Pismo.

Step 4: You need to install the ISO file with Pismo File Mount Audit Package. Download Pismo from here

Step 5: Right click the downloaded ISO file and choose mount with Pismo. Thereafter, install the Setup\SDKSetup.exe instead of setup.exe.

Step 6a: Create a vcvars64.bat file in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 by changing directory to C:\Program Files (x86)\Microsoft Visual Studio version\VC\ on the command prompt. Type command on the command prompt: cd C:\Program Files (x86)\Microsoft Visual Studio version\VC\r

Step 6b: To configure this Command Prompt window for 64-bit command-line builds that target x86 platforms, at the command prompt, enter: vcvarsall x86 Click here for more options.

Step 7: At the command prompt, install the PyCrypto by typing: C:\Python3X>pip install -U your_wh_file


回答 19

我有python的Pycharm。

  1. pycharm -> file -> setting -> project interpreter

  2. 点击 +

  3. 搜索"pycrypto"并安装软件包

注意:如果尚未安装“适用于Python 2.7的Microsoft Visual C ++编译器”,则它将提示安装,一旦安装完成,请尝试上述步骤,即可正常工作。

I had Pycharm for python.

  1. Go to pycharm -> file -> setting -> project interpreter

  2. Click on +

  3. Search for "pycrypto" and install the package

Note: If you don’t have “Microsoft Visual C++ Compiler for Python 2.7” installed then it will prompt for installation, once installation finished try the above steps it should work fine.