Python 101- Caesar Cipher - simple encryption and decryption methods

in #python4 years ago

maxwellnelsontaiuG8CPKAQunsplash.jpgSOURCE

A very basic look at Caesar Cipher


In cryptography, Caesar Cipher is a simple and widely known encryption and decryption method. In this following article, I will give a brief overview of my current python script for encrypt and decrypt methods with text in the alphabet.


HIVEISALIVE.PNG

USER MANUAL

NAME

         Caesar Cipher 

 Can perform basic Caesar encryption/decryption with no restricted length in the message a fully automated way for communicating over any social networks and working on confidential projects with brief messages for easy convert from encryption/decryption methods.

SYNOPSIS

                Caesar.py [arg1][arg2][arg3]

DESCRIPTION

       Caesar requests the user to provide the following three arguments before proceeding.
1)  a code whether either encryption or decryption is performed
2)  A text they want to encrypt/decrypt
3)  The number of shifts for encryption/decryption. 
If the user forgot to enter one or more arguments, Caesar displays an error message and stops. If each parameter is not valid, Caesar issues an error message and stops.

Then Caesar encrypts or decrypts the given text provided as the argument 2 based on the code provided as the argument 1.
 
When encryption is selected, Caesar first converts all the letters from lowercase to uppercase as the preparation for performing the Caesar encryption. Caesar converts a full stop to the letter “X”. All other letters including space are ignored. The converted text is encrypted or decrypted by using the number of shifts. If there are no valid letters in the text for encryption or decryption, Caesar issues an error message and stops.
 
When the encryption or decryption finishes, Caesar will output and display the encrypted or decrypted text on the screen.

EXAMPLE

the user receives the following result.
          
>>> %Run Caesarjk.py
Welcome, What would you like encrypting/decrypting today?

>>> %Run Caesarjk.py e "Billy Goat" 7

Encrypted text:  IPSSFNVHA

>>> %Run Caesarjk.py d "IPSSFNVHA" 7

Decrypted text:  BILLYGOAT




     SEE ALSO
       sys, re   



BUGS

When the user gives certain lowercase letters and non-alphabetical letters such as numbers or special characters as text to decrypt. Will cause an error and return blank as the encryption function accepts only uppercase for the input

Not using quotation marks correctly and misconfigured from human error



PSEUDO-CODE

Pseudo-Code for This Function –

Define convert_to_Caesar() and get a string as an argument
Initialise variables for letter conversion and returning string
Using a ‘for’ loop to find a letter to convert
Set True to a flag for writing a converted letter into a returning string
If the letter is upper case
Pass the letter to the returning value without modification
Else if the letter is lower case
Convert the letter to ASCII code
Turning lower case to upper case by subtracting 32
Returning the letter from ASCII to character
Else if the letter is “.” (full stop)
Change to “X”
Else the letter is of other type
Set False to the flag to not write the letter into a string
End of the if condition

If the flag is True
Write a converted letter to the result string to pass to the caller program
End of the for loop

If the input does not contain any valid letters
Display a message “No valid letter is found.”
End of the if condition

Return the result Strings


import sys
import re
# The following three functions are to check the validity of each argument.
#
# Check the validity of the encryption/decryption code argument.
# Any arguments other than 'e' 'E' 'd' 'D' return an error message and python script will stop. 
def encryptCheck(encrypt):
    rightLetter = False
# Check the validity of encryption/decryption code. simple if condition
# as a for loop might add an unnecessary time only to check one letter argument
    if (encrypt != "e") and (encrypt != "d") and (encrypt != "E") and (encrypt != "D"):
        print("USER INPUT ERROR: Invalid encryption code: it should be in this range only e/E/d/D.")
        sys.exit(1)
    else:
        return(encrypt)

# If the user forgot to enter one of the arguments, the program displays an error message and stops. and can also show the following at startup
try:
    encrypt = sys.argv[1]
    text = sys.argv[2]
    shift = int(sys.argv[3])  
except IndexError:
    print("Welcome, What would you like encrypting/decrypting today?")
else:
# Before starting the encryption process, the program checks the validity of all arguments.
    encrypt = encryptCheck(encrypt)
    text = textCheck(text)
    shift = shiftCheck(shift)



Test cases

io.PNG

errors.PNG

Sort:  

Thanks for sharing your creative and inspirational post on HIVE!



This post got curated by our fellow curator @tibfox and you received a 100% upvote from our non-profit curation service!

Join the official DIYHub community on HIVE and show us more of your amazing work!

Thanks @diyhub I have subscribed and look forward to sharing more do it your self guides!