Machine Learning

Using Chesar Cipher Epython

Is Roman ruler known for his best leadership strategies. It is named after him, Chesar Cipher is a pleasing cryptographic way that Julius Caesar employed sending secret signals and messages to his military personnel.

Caesar Cipher is basic in its performance. It works by changing all the book characters that should be encrypted with a number of fixed areas, called key. The person receiving a key and uses the key to change the message, thus providing a simple and intelligent way to hold officers.

Understanding the project

In this article, we will learn how to use Chesar Cipher in Python. This is a friendly friendly job where we will use conditional statements, pulps and functions that you charge and determine user installation data.

Here's how the system will work: The system asks the user that the message is included or issued. The user selects or encrypting or deterring. The program asks the key when the message will be crucified. When a user provides the key, the system will change the message by turning each message of the message according to the key, either in accordance with characters, or back if it is drawn.

Let us first explain the project steps with the help of FlowChart.

Step 1: Explanation of characters lists

First of all, we will use the datetype of the Pythonye list to create a list of alphabetic characters. The Python list is in a row in a particular order and have several built-in activities. The system will use this list as a reference to change the characters that are forward or back in accordance with the selection code or constipation. Let's explain a list, alphabet.

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
            'v', 'w', 'x', 'y', 'z']

Step 2: Ask user input

The next step is to take the user's input. We will request a user:

  1. That they want to include in or to decide the message encode_or_decode
  2. Specific message secret_message who want to enter the code or examine, and
  3. Number of change key The message will be included or released.
encode_or_decode = input("Type 'encode' to encrypt, type 'decode' to decrypt:n").lower()
secret_message = input("Type your message here:n").lower()
key = int(input("Type the key:n"))

Be sure to convert key in int Datetype, otherwise you might get into trouble in turning this book with the key number as Python will look for input as a string as part of the rope not in the incer. And remember to change user's input secret_message including encode_or_decode Measuring the parallels in the list of lists and conditional statements that will be forward.

Step 3: Explaining activities and using modulo operators

The next step is to describe both encode including decode Tasks to be called later. This page encode The work will be called when the user chooses 'encode' in encode_or_decode Quick installation, and decode The employee will be called when user types are included in the encode_or_decode Quick installation.

Both of these activities are directly straight into the code. It will take secret_messageDump all the letters, and to all the letters in the alphabet from the front, it will replace their indicators, add key in it, then give shifted_position. We will use this shifted_position as a reflection of characters and put it in output_text which will be a combined or limited message.

But we have one problem here! What if by changing letters, the direction of the index is from the letter list? Well the solution for this uses the exciting Python operator called 'stem'. The Modolo is the operator that offers the remainder after being separated and pictured by '%' in Python. So 8% 5 will equal 3, as the remainder after 8 is separated at 5.

We will use this operator to our Encode and Description. Therefore, processing a list of characters There are 26 items, and if the user is giving the message to “Vampire” and key to the '32 characters. The 32 converts, this publication will be 32% 26 = 6th position. We will therefore use the same understanding of the examination of our code and the events.

Here's the Encode Work:

def encode(message, keynumber, operation):
    output_text = ""
    for letter in message:
        if letter in alphabet:
            shifted_position = alphabet.index(letter) + keynumber
            shifted_position = shifted_position % len(alphabet)
            output_text = output_text + alphabet[shifted_position]
        else:
            output_text += letter       
    print("Here is the encoded text : ", output_text)

So if I want to rewrite “I have a pet cat” with the keyword '6', I will receive the following message sent:

Missed message (Photo by the writer)
Photos by Bogdan Farca in UnderChes

By decode work, because we postpone you, we have to convert letters with key In a convertible order, that is, back. So if I want to decide the next message: 'O Ngib vkz Ig', I have to repeat the main number by1, to instead of adding the key, it outputs keyno shifted_position will be available by submitting the direction of the direction back.

Let us explain the work of finding:

def decode(message, keynumber, operation):
    keynumber = keynumber * -1
    output_text = ""
    for letter in message:
        if letter in alphabet:
            shifted_position = alphabet.index(letter) + keynumber
            shifted_position = shifted_position % len(alphabet)
            output_text = output_text + alphabet[shifted_position]
        else:
            output_text += letter       
    print("Here is the decoded text : ", output_text)

Here is our organized message:

A edited message (Photo by the writer)

Step 4: Femal works

When our jobs are described, we will call them when needed. If the user wants to text confidential, we will call encode work, and if they want to stop the message, we will call the decode work. Let us use this in our code using conditional statements when, Elif, and more:

if encode_or_decode == 'encode':
    encode(message=secret_message, keynumber=key, operation=encode_or_decode)
elif encode_or_decode == 'decode':
    decode(message=secret_message, keynumber=key, operation=encode_or_decode)
else:
    print("Error")

Step 5: Continuing of the program

The final step for this program to ask the user that they want to continue to develop an encrypting plan and to postpone or end. We will use this in variations continue_program That will be true at first and will always be when the user wants to continue the program. If the user wants the end, flexibility will change it, and the system will end. We will include this situation in while LOOP will run as long as you remain a variable True.

continue_program = True

while continue_program:

    encode_or_decode = input("Type 'encode' to encrypt, type 'decode' to decrypt:n").lower()
    secret_message = input("Type your message here:n").lower()
    key = int(input("Type the key:n"))

    if encode_or_decode == 'encode':
        encode(message=secret_message, keynumber=key, operation=encode_or_decode)
    elif encode_or_decode == 'decode':
        decode(message=secret_message, keynumber=key, operation=encode_or_decode)
    else:
        print("Error")
        
    restart = input("Type 'yes' if you want to continue with the program.nOtherwise, type 'no'.n").lower()
    if restart == "no":
        continue_program = False

Store

By the installation of while The LOOP above, use the use of Cesar Cipher in Python. This project has examined conditional statements if, elifbesides else, for including while Lias, and to explain the costs worth. In addition, we introduce the module operator on the program that is different.

You can find the perfect source code here.

If you have any questions or you want to share a different method, feel free to comment on this article. I will look forward to it. Codes? 🙂

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button