Friday, March 4, 2016

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

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

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

1. iPhone generates an RSA private / public key pair and sends the public key to server.
2. Server encrypts a message using the public key provided by iPhone and sends the encrypted message to iPhone
3. iPhone decrypts the encrypted message from server 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.Cipher import PKCS1_v1_5

#Declartion
mysocket = socket.socket()
host = socket.gethostbyname(socket.getfqdn())
port = 7777
publicKeyType_str = "public_key="
message = "Secret message from RPi server"

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 publicKeyType_str in data:

        #remove publicKey_str
        data = data.replace(publicKeyType_str, '')

        #convert string to key
        public_key = RSA.importKey(data)

        #add PKCS1 v1.5 padding
        public_key_padding = PKCS1_v1_5.new(public_key)
        #print(public_key.exportKey()) #Check this with Xcode's debug console.
        print("Client's public key received!")

        #encrypt
        encrypted = public_key_padding.encrypt(message)
        c.send(encrypted)
        print "Encrypted message sent!"

    elif data == "Quit": break

#Server to stop
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 Python socket server to iOS device (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. Raspberry Pi sends "Secret message from RPi server" to iPhone:


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

No comments:

Post a Comment