Thursday, March 3, 2016

Sending RSA encrypted message - From iOS device to Python socket server (Raspberry Pi Part)

Secure Data Transmission: iOS device (Swift) -> Raspberry Pi (Python)

Secure communication between a mobile device client and a server is important. This tutorial selects a Raspberry Pi as the server and uses an iPhone simulator as the client. The communication procedure is as below:

1. Server generates an RSA private / public key pair and sends the public key to iPhone
2. iPhone encrypts a message using the public key provided by server and sends the encrypted message to server
3. Server decrypts the encrypted message from iPhone using the private key

This tutorial is derived from the basic communication between an iPhone and a Python socket server built on a Raspberry Pi without encryption:

Connect an iPhone to a Simple Python Socket Server (Raspberry Pi Part) (iOS Part)

For an overview on client-server communications, see this:

Communication between iOS device (Client) and Raspberry Pi (Server)

Raspberry Pi Part

Raspberry Pi 2 Model B is used. The Python version used is 2.7.3. Check the Python version with the python -V command.

1. Install Python-Crypto.

sudo apt-get install python-crypto




2. Use sudo nano server.py command to edit a python file as below:


import socket
from Crypto.PublicKey import RSA
from Crypto import Random

#Generate private key and public key
random_generator = Random.new().read
private_key = RSA.generate(1024, random_generator)
public_key = private_key.publickey()

#Declartion
mysocket = socket.socket()
host = socket.gethostbyname(socket.getfqdn())
port = xxxx

encrypt_str = "encrypted_message="

if host == "127.0.1.1":
    import commands
    host = commands.getoutput("hostname -I")
print "host = " + host

#Prevent socket.error: [Errno 98] Address already in use
mysocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mysocket.bind((host, port))
mysocket.listen(5)
c, addr = mysocket.accept()

while True:

    #Wait until data is received.
    data = c.recv(1024)
    data = data.replace("\r\n", '') #remove new line character

    if data == "Client: OK":
        c.send("public_key=" + public_key.exportKey() + "\n")
        print "public key sent"

    elif encrypt_str in data:

        #remove encrypt_str
        data = data.replace(encrypt_str, '')

        #decrypt
        decrypted = private_key.decrypt(data)

        #remove padding
        if len(decrypted) > 0 and decrypted[0] == '\x02':
            pos = decrypted.find('\x00')
            if pos > 0:
                c.send("Server: OK")
                message = decrypted[pos+1:]
                print message

    elif data == "Quit": break

#Server to stop
c.send("Server stopped\n")
print "Server stopped"
c.close()


iOS Part

Write an iOS app in Swift. See the client version of this post:

Sending RSA encrypted message - From iOS device to Python socket server (iOS Part)

Result

Run both the socket server on Raspberry Pi and iOS simulator on Mac. Press the four app buttons from top to bottom. iPhone sends "Secret message from iPhone!!" to Raspberry Pi:



Reference:

How to decrypt an RSA encrypted file in Python

Go back to Communication between iOS device (Client) and Raspberry Pi (Server)

1 comment:

  1. Casino of the Oz Hotel & Racetrack - Mapyro
    Find Casino of the Oz Hotel & Racetrack information for 충청북도 출장샵 you. The casino is 전라남도 출장샵 located 제주도 출장안마 in Tuscany, New York and can be 동해 출장샵 contacted 세종특별자치 출장안마 at (212) 547-0363 or at (212)

    ReplyDelete