Sending Encrypted strings using socket in Python

前端 未结 1 360
借酒劲吻你
借酒劲吻你 2021-01-01 00:32

I made a simple server program that is able to receive data from 4 different clients at a time. Now I want to send some data with AES-128 Encryption but that should be decod

1条回答
  •  自闭症患者
    2021-01-01 01:25

    Use Python's Crypto module which supports AES. You need a symmetric key (same key used to encrypt and decrypt). The same key can be generated in both server and client if the same passphrase and the initialization vector(IV) are used.

    Summary: 1. Same key to be used to encrypt and decrypt 2. Use Crypto.Cipher.AES

    AES has methods to generate key, encrypt and decrypt data. Following links have the actual code. pycrypto stackoverflow

    Client - Call this method to encrypt your data and send the encrypted data

    from Crypto.Cipher import AES
    
    def do_encrypt(message):
        obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
        ciphertext = obj.encrypt(message)
        return ciphertext
    

    Server - Receive data and call this method to decrypt the data

    from Crypto.Cipher import AES
    
    def do_decrypt(ciphertext):
        obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
        message = obj2.decrypt(ciphertext)
        return message
    

    This is a sample code, make sure you choose a strong passphrase and IV.

    0 讨论(0)
提交回复
热议问题