Skip to content

Cesar Cipher implemented in python #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions allalgorithms/string/cesarCipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# String Algorithms
# Contributed by: JordaoA
#Cesar Cipher implemented in python

class cipher():
#Constructor method
def __init__(self,posCipher,jump):
self.letters = ["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"] #Alphabet in UPPERCASE
self.posCipher = posCipher #encrypted word
self.jump = jump #jump between the letters

#Method responsible for ignore the case of all letters of the word
def ignoreCase(self):
newPosCipher = ""

for i in range(len(self.posCipher)):
newPosCipher += self.posCipher[i].upper()

self.posCipher = newPosCipher #transforming the old word

#Method responsible for search index of each letter of the word in the alphabet
def returnIndex(self,element, listOfLetters):
index = 0 #letter index

for i in range(len(listOfLetters)):
if element == listOfLetters[i]:
index = i
break

return index

#Method responsible for deciphering the word given by the user according to the letter jump
def cipherC(self):

self.ignoreCase()
preCipher = "" #decipher word

for j in range(len(self.posCipher)):
index = self.returnIndex(self.posCipher[j], self.letters)

if (index - self.jump) > 25:
preCipher += self.letters[(index - self.jump) % 26]

else:
preCipher += self.letters[(index - self.jump)]

return preCipher