gpt4 book ai didi

python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

转载 作者:太空狗 更新时间:2023-10-29 18:06:39 26 4
gpt4 key购买 nike

我正在创建执行不同任务的各种流程。其中之一,也是唯一一个,有一个创建 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 122, in read
self._check_pid()
File "/usr/local/lib/python2.7/dist-packages/Crypto/Random/_UserFriendlyRNG.py", line 138, in _check_pid
raise AssertionError("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")
AssertionError: PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()

当不从进程中调用时,解密在交互上运行良好。

我的安全模块是这样的:

'''
Created on 25 Apr 2013

@author: max
'''

import base64, ast, binascii
from Crypto.Cipher import AES
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import br_consts

class SecurityMod(object):
'''
classdocs
'''

def __init__(self):
'''
Constructor
'''
super(SecurityMod,self).__init__()
self.privkey = RSA.importKey(open('./privkeyBR.pem', 'r').read())
self.cipher = PKCS1_v1_5.new(self.privkey)
self.ridToKeySalt = {}

#depending on the type of message, encryption format is different
def encrypt(self, msg, rqId, rid):
##code
return encMsg

#return string of object so it can be parse by JSON
def decrypt(self, encMsg, rqId, rid):

#code
return msgObjStr



def pad_data(self,data):
if len(data) == 0:
return data
if len(data) % 16 == 0:
padding_required = 15
else:
padding_required = 15 - (len(data) % 16)
data = '%s\x80' % data
data = '%s%s' % (data, '\x00' * padding_required)
return data


def unpad_data(self,data):
if not data:
return data
data = data.rstrip('\x00')
if data[-1] == '\x80':
return data[:-1]
else:
return data

最佳答案

您需要在 os.fork() 之后调用 Crypto.Random.atfork()

我只是将 __init__() 放在安全模块之前

关于python - 加密 : AssertionError ("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16981503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com