我正在使用Google App Engine和PyCrypto进行一些加密。我得到的错误,如下所示,仅在我的本地开发服务器上运行,运行Linux Mint Maya(13)。我将相同的代码部署到GAE云中,并且运行时没有错误。
ERROR 2012-06-29 16:04:20,717 webapp2.py:1553] [Errno 13] file not accessible: '/dev/urandom'
Traceback (most recent call last):
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/eric/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/eric/workspace/commentbox/src/controller/api.py", line 55, in get
self.response.out.write(encrypt(json.dumps(to_json)))
File "/home/eric/workspace/commentbox/src/controller/api.py", line 27, in encrypt
iv = Random.new().read(AES.block_size)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/__init__.py", line 33, in new
return _UserFriendlyRNG.new(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 206, in new
return RNGFile(_get_singleton())
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 200, in _get_singleton
_singleton = _LockingUserFriendlyRNG()
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 144, in __init__
_UserFriendlyRNG.__init__(self)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 86, in __init__
self._ec = _EntropyCollector(self._fa)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 53, in __init__
self._osrng = OSRNG.new()
File "/usr/lib/python2.7/dist-packages/Crypto/Random/OSRNG/posix.py", line 60, in new
return DevURandomRNG(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Random/OSRNG/posix.py", line 42, in __init__
f = open(self.name, "rb", 0)
File "/home/eric/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 592, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/dev/urandom'
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] Exception
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] AttributeError
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] :
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] "'DevURandomRNG' object has no attribute 'closed'"
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] in
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] <bound method DevURandomRNG.__del__ of <Crypto.Random.OSRNG.posix.DevURandomRNG object at 0x52707d0>>
ERROR 2012-06-29 16:04:20,721 webapp2.py:1549] ignored
抛出错误的python代码是此块中的第二行:
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(plaintext)
看到此错误后,I realized it might be a permissions error。然后我快速检查了/ dev / urandom上的权限:
eric@eric-Latitude-E5400 ~ $ dpkg -L udev | xargs grep urandom
/lib/udev/rules.d/50-udev-default.rules:KERNEL=="null|zero|full|random|urandom", MODE="0666"
eric@eric-Latitude-E5400 ~ $ ls -lart /dev/*random
crw-rw-rw- 1 root root 1, 9 Jun 29 10:53 /dev/urandom
crw-rw-rw- 1 root root 1, 8 Jun 29 10:53 /dev/random
所以看起来我的权限很好。我也试过以root身份运行开发服务器,但是我得到了同样的错误。出于某种原因,这只发生在开发服务器上,而不是在部署到谷歌的云时。关于下一步该尝试的任何想法?
谢谢!
答案 0 :(得分:2)
您收到的错误是因为GAE限制了文件访问权限,/dev/urandom被屏蔽了。
请注意,导入PyCrypto时不会出现错误,当您执行AES.new(key, AES.MODE_CBC, iv)
您可以通过修改Crypto/Random/OSRNG/__init__.py
并移动线
if hasattr(os, 'urandom'):
from Crypto.Random.OSRNG.fallback import new
到顶部,或者在脚本开头将os.name修改为不同于posix或nt的东西。我建议第一个选择。
ps:我假设你使用的是python 2.5和pycrypto 2.2,因为你的Traceback。下次请详细说明。