-1

I am new to python and I am trying to create a chat server in python using socket. In my server i want to encrypt and decrypt the message sended from the client to the sever.I am trying to create a key from the server then send it to the client but i got this error: "'function' object is not iterable" . I am following this guide : https://riptutorial.com/python/topic/8710/sockets-and-message-encryption-decryption-between-client-and-server. Here are the code that generate the error:

key_128 = os.urandom(16)
#encrypt CTR MODE session key
en = AES.new(key_128,AES.MODE_CTR,counter = lambda:key_128)
encrypto = en.encrypt(key_128)

and this is the error :

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    en = AES.new(key_128,AES.MODE_CTR,counter = lambda:key_128)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\AES.py", line 232, in new
    return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\__init__.py", line 79, in _create_cipher
    return modes[mode](factory, **kwargs)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\_mode_ctr.py", line 366, in _create_ctr_cipher
    _counter = dict(counter)
TypeError: 'function' object is not iterable

I tried to run this guide on python 2.7 and it run ok. But i run on python 3 it has error. Can some one explain why and help me fix this?

Update :

  • I am using pycryptodome instead of Pycrypto because i cant install Pycrypto in my pc.
  • I did some researches how to fix this. There is the link that said the error is caused by conflict between python-crypto in lib and local/lib ... But i dont think it is. Here is the link https://github.com/nccgroup/Winpayloads/issues/21
glibdud
  • 7,550
  • 4
  • 27
  • 37
son duong
  • 21
  • 6

1 Answers1

0

Per the documentation:

counter : (object) – Instance of Crypto.Util.Counter, which allows full customization of the counter block. This parameter is incompatible to both nonce and initial_value.

counter in this context is an anonymous function (a lambda), but not an instance of Crypto.Util.Counter. Given key_128 = os.urandom(16) - key_128 is a string.

alex
  • 6,818
  • 9
  • 52
  • 103
  • Thank you i understand a little bit more now. But why in python 2 this code can still run?If this code is incorrect, it shouldn't be able to run – son duong Sep 18 '19 at 03:50
  • @sonduong It should not be able to run in Python 2 either, unless the AES library has an entirely different build and internal API for Python 2. – alex Sep 18 '19 at 04:15