Encryption/Decryption(Caesar Cipher) of message using Python in Raspberry Pi

Encryption/Decryption(Caesar Cipher) of message using Python in Raspberry Pi

2nd November 2019 0 By Tanna TechBiz
  • Encryption is the process by which a readable message is converted to an unreadable form to prevent unauthorized parties from reading it. 
  • Decryption is the process of converting an encrypted message back to its original (readable) format. The original message is called the plaintext message. The encrypted message is called the ciphertext message.
  • Here in this tutorial what we are going to do is, we simply use the Caesar Cipher method of encryption and decryption for our data.
  • The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution in which each letter in the plaintext is shifted a certain number of the alphabet. The method is named after Julius Caesar, who apparently used it to communicate with his generals.
  • Here there is a key
    In cryptography, a key is a variable value that is applied using an algorithm to a string or block of unencrypted text to produce encrypted text, or to decrypt encrypted text. The length of the key is a factor in considering how difficult it will be to decrypt the text in a given message.
  • So here we just write a simple algorithm in python which will do above things on raspberry pi.


How to do..

  • Boot up your raspberry pi with Raspbian OS.
  • Open Python IDLE or you can make a .py file from terminal.
    open terminal and write
$ sudo nano main.py
  • After that write below code and run you script
def encryptText(input_string,key):
    output = ""
    for letter in input_string:
        #Ascii Uppercase 65-90 
        #non-characters will not allowed
        if (ord("A") > ascii_value) or (ord("Z") < ascii_value):
            #Apply encryption key
            key_variable = ascii_value+key
            #To ensure that we just use A-Z in data
            if not((ord("A")) < key_variable < ord("Z")):
        #Add the encoded letter to the output string

    return output
#Test function
def main():
    print ("Please enter text to scramble:")
     #Get user input
        user_input = input()
        scrambled_output = encryptText(user_input,20)
        print ("output: " + scrambled_output)
        print ("To unscramble, press enter again")
        unscrambled_output = encryptText(scrambled_output,-20)
        print ("output: " + unscrambled_output)
    except UnicodeDecodeError:
        print ("Sorry: Only ASCII Characters are supported")
  • After running this code if you enter the data like Hi How are you it will give output like BC CBIQ QULY YSIO that is your encrypted data.
  • If you want to decrypt the data you can simply write encrypted data and you will get your message back.


  • Here in this code we make or you can say we defines two functions encrypttext() and main().
  • Here as we run the code the main function will take input from user and will store in user_input till user press enter.
    after that we call another function that is encrytext()
    The encryptText() function performs a simple form of encryption by taking the letters in the message and substituting each letter with another in the alphabet (determined by counting on from the number of letters specified by the encryption key). In this way, the letter A will become C when the encryption key is 2.
  • The ord() function will give the ascii value of a letter here we convert input text in upper case letter by using upper() function for simplicity.
  • As we have ASCII range of A to Z is 65 to 90 so we need to take care of that our encrypted character’s value must be below 91 so for that here we use % operator with ASCII value of Z to ensure that our encrypted character’s value is less than 91.
  • So, if X is 24 and we add 20, we get 44. The value of 44%26 (where 26 is the total number of letters) is 11. Counting on 11 from A, we reach K.
  • You can try this simple experiment. Currently, with this basic form of encryption, you would supply the method and the key to anyone you wish to read your message.