PyCrypto in Google App Engine development server “

2020-06-20 04:56发布

问题:

I have a function which encrypts a string with AES using PyCrypto. When I call that function in my unit tests, everything works fine. On the production environment, it works fine as well. However, when the function is called on the GAE development server, an error is thrown: "ImportError: cannot import name blockalgo". I tested it on Windows 7 (64 bit) and Mac OS 10.5. Both resulted in the same error. I'm using Google App Engine with Python 2.7. What could be the problem?

app.yaml

application: xxx
version: 6
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: django
  version: "1.2"
- name: webapp2
  version: "2.3"
- name: jinja2
  version: "2.6"
- name: pycrypto
  version: "2.3"
- name: PIL
  version: "1.1.7"

builtins:
- appstats: on
- remote_api: on

inbound_services:
- mail
- warmup

Encryption function:

def encrypt(plaintext):
    from Crypto.Cipher import AES
    import hashlib

    password = 'xxx'
    key = hashlib.sha256(password).digest()

    mode = AES.MODE_ECB
    encryptor = AES.new(key, mode)

    BLOCK_SIZE = 16
    PADDING = '{'
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
    EncodeAES = lambda c, s: b58encode(c.encrypt(pad(s)))

    encrypted = EncodeAES(encryptor, plaintext)

    if len(encrypted) < 22:
        for i in range (len(encrypted), 22):
            encrypted += "_"
    return encrypted

回答1:

Make sure the version of PyCrypto that is installed on your local system is the same as the version specified in app.yaml. Think twice before you upgrade a package to the newest version.