Frage

Unknown Error in pycrypto AES CBC encryption, The key is stripped to 16bytes but still it gives this error:

Source Help Article: http://support.ideascale.com/kb/ideascale-setup/single-sign-on-multipass-token-based Update Class module: class multipassGenerator(): def init(self): api_key = "61fd537c-8961-479f-a413-aa91a401c3f5-5e866a4c-09c7-4ba0-8da1-cd8e4e88e3ec" app_key = "25151"

    def generateMultipass(self,usrEmail,usrName):
        if usrEmail and usrName:
            # EXAMPLE: Multipass JSON Token
            message = {"email":usrEmail,"name":usrName,"expires":"2099-02-24T19:55:31.111-08:00"}
            block_size = 16
            mode = AES.MODE_CBC

            # Replace api_key and app_key with your Community's API Key and Site Key
            # The keys below are for this community: http://multipass.ideascale.com/
            # Do not share your keys - the keys below are for testing purposes only.
            # See: http://support.ideascale.com/kb/ideascale-setup/single-sign-on-multipass-token-based
            api_key = self.api_key
            app_key = self.app_key

            json = JSON.dumps(message, separators=(',',':'))

            salted = api_key+app_key
            saltedHash = hashlib.sha1(salted).digest()[:16]
            logging.info(message)
            logging.info(salted)
            #logging.info(saltedHash)
            logging.info(len(saltedHash))
            json_bytes = array.array('b', json[0 : len(json)]) 

            pad = block_size - len(json_bytes.tostring()) % block_size
            data = json_bytes.tostring() + pad * chr(pad)
            aes = AES.new(saltedHash, mode)
            encrypted_bytes = aes.encrypt(data)

            b64token = base64.b64encode(encrypted_bytes)
            b64token = re.sub(r'\s+' ,'' ,b64token)
            b64token = re.sub(r'\=+$','' ,b64token)
            b64token = re.sub(r'\+'  ,'-',b64token)
            b64token = re.sub(r'\/'  ,'_',b64token)
            token = urllib.quote(b64token)

            return token

We execute this:

from getUserMuiltipass import multipassGenerator
tokenGenerator = multipassGenerator()
tokenGenerator.api_key = u"61fd537c-8961-479f-a413-aa91a401c3f5-5e866a4c-09c7-4ba0-8da1-cd8e4e88e3ec"
tokenGenerator.app_key = u"25151"
email = 'sanvvvdeepkffffs@advaiddddya.com' # any input same issue 
print  email.split('@')[0]
ssoToken = tokenGenerator.generateMultipass(email,email.split('@')[0]) 
print ssoToken

this part of code is causing issue : AES.new(saltedHash, mode)

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\python\request_handler.py", line 156, in handle_interactive_request
    exec(compiled_code, self._command_globals)
  File "<string>", line 7, in <module>
  File "C:\appengine\ideasONSMS\getUserMuiltipass.py", line 59, in generateMultipass
    aes = AES.new(saltedHash, mode)
  File "C:\Python27\lib\site-packages\Crypto\Cipher\AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "C:\Python27\lib\site-packages\Crypto\Cipher\AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "C:\Python27\lib\site-packages\Crypto\Cipher\blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long
War es hilfreich?

Lösung

It is probably because AES.new fn needs 3 args for CBC mode:

AES.new(key, *args, **kwargs)

3-d arg is IV. Can be generated like this:

iv = Random.new().read(AES.block_size)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top