我正在使用Google App Engine和PyCrypto進行一些加密。我得到的錯誤如下,僅在我的本地開發服務器上運行Linux Mint Maya(13)時出現。我將相同的代碼部署到GAE雲,並且運行時沒有錯誤。Google App Engine + PyCrypto =/dev/urandom無法訪問
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的權限:
[email protected] ~ $ dpkg -L udev | xargs grep urandom
/lib/udev/rules.d/50-udev-default.rules:KERNEL=="null|zero|full|random|urandom", MODE="0666"
[email protected] ~ $ 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用戶身份運行開發服務器,但是我得到了同樣的錯誤。出於某種原因,這隻發生在開發服務器上,而不是部署到谷歌的雲中。關於下一步嘗試的任何想法?
謝謝!
你是否嘗試打開一個Python提示符並執行'從Crypto import Random'? – voithos
@voithos,是我在python終端完成了這個工作,沒有錯誤: 'from Crypto import Random' 'from Crypto.Cipher import AES' 'Random.new()。read(AES.block_size)' '* \ xd85G \ x10 + \ xce \ xd2 \ x98 \ x85 \ xcd + \ xf5 \ xf5 [<'' – zzz