mirror of
https://bitbucket.org/Mattrixwv/pypherstream.git
synced 2025-12-06 18:43:58 -05:00
Implemented the Caesar class allowing Caesar
ciphers to be used
This commit is contained in:
80
Caesar.py
80
Caesar.py
@@ -15,38 +15,92 @@ class Caesar:
|
|||||||
self.__shift = 0 #The amount that you need to shift each letter
|
self.__shift = 0 #The amount that you need to shift each letter
|
||||||
#Sets shift and makes sure it is within the propper bounds
|
#Sets shift and makes sure it is within the propper bounds
|
||||||
def __setShift(self, shiftAmount: int):
|
def __setShift(self, shiftAmount: int):
|
||||||
NotImplemented
|
self.__shift = shiftAmount % 26
|
||||||
#Sets the input string
|
#Sets the input string
|
||||||
def __setInputString(self, inputString: str):
|
def __setInputString(self, inputString: str):
|
||||||
NotImplemented
|
self.__inputString = inputString
|
||||||
#Encodes the inputString and stores the result in outputString
|
#Encodes the inputString and stores the result in outputString
|
||||||
def __encode(self) -> str:
|
def __encode(self) -> str:
|
||||||
NotImplemented
|
temp = 0 #A temperary holder for the current working character
|
||||||
|
for cnt in range(0, len(self.__inputString)):
|
||||||
|
temp = ord(self.__inputString[cnt])
|
||||||
|
#If it is an upper case letter shift it and wrap if necessary
|
||||||
|
if(chr(temp).isupper()):
|
||||||
|
temp += self.__shift
|
||||||
|
#Wrap around if the letter is now out of bounds
|
||||||
|
if(temp < ord('A')):
|
||||||
|
temp += 26
|
||||||
|
elif(temp > ord('Z')):
|
||||||
|
temp -= 26
|
||||||
|
#If it is a lower case letter shift it and wrap if necessary
|
||||||
|
elif(chr(temp).islower()):
|
||||||
|
temp += self.__shift
|
||||||
|
#Wrap around if the letter is now out of bounds
|
||||||
|
if(temp < ord('a')):
|
||||||
|
temp += 26
|
||||||
|
elif(temp > ord('z')):
|
||||||
|
temp -= 26
|
||||||
|
|
||||||
|
#If it is whitespace, number, or punctuation just let it pass through
|
||||||
|
#Add it to the output string
|
||||||
|
self.__outputString += chr(temp)
|
||||||
|
return self.__outputString
|
||||||
|
|
||||||
#Decodes the inputString and stores the result in outputString
|
#Decodes the inputString and stores the result in outputString
|
||||||
def __decode(self) -> str:
|
def __decode(self) -> str:
|
||||||
NotImplemented
|
temp = 0
|
||||||
|
for cnt in range(0, len(self.__inputString)):
|
||||||
|
temp = ord(self.__inputString[cnt])
|
||||||
|
#If it is an upper case letter shift it and wrap if necessary
|
||||||
|
if(chr(temp).isupper()):
|
||||||
|
temp -= self.__shift
|
||||||
|
if(temp < ord('A')):
|
||||||
|
temp += 26
|
||||||
|
elif(temp > ord('Z')):
|
||||||
|
temp -= 26
|
||||||
|
#If it is a lower case letter shift it and wrap if necessary
|
||||||
|
elif(chr(temp).islower()):
|
||||||
|
temp -= self.__shift
|
||||||
|
if(temp < ord('a')):
|
||||||
|
temp += 26
|
||||||
|
elif(temp > ord('z')):
|
||||||
|
temp -= 26
|
||||||
|
#If it is whitespace, number, or punctuation just let it pass through
|
||||||
|
#Add it to the output string
|
||||||
|
self.__outputString += chr(temp)
|
||||||
|
return self.__outputString
|
||||||
|
|
||||||
#Returns the inputString
|
#Returns the inputString
|
||||||
def getInputString(self) -> str:
|
def getInputString(self) -> str:
|
||||||
NotImplemented
|
return self.__inputString
|
||||||
#Returns the shift value
|
#Returns the shift value
|
||||||
def getShift(self) -> int:
|
def getShift(self) -> int:
|
||||||
NotImplemented
|
return self.__shift
|
||||||
#Returns the outputString
|
#Returns the outputString
|
||||||
def getOutputString(self) -> str:
|
def getOutputString(self) -> str:
|
||||||
NotImplemented
|
return self.__outputString
|
||||||
#Sets the shift and inputString and encodes the message
|
#Sets the shift and inputString and encodes the message
|
||||||
def encode(self, shiftAmount: int, input: str) -> str:
|
def encode(self, shiftAmount: int, inputStr: str) -> str:
|
||||||
NotImplemented
|
self.reset()
|
||||||
|
self.__setShift(shiftAmount)
|
||||||
|
self.__setInputString(inputStr)
|
||||||
|
return self.__encode()
|
||||||
#Sets the shift and inputString and decodes the message
|
#Sets the shift and inputString and decodes the message
|
||||||
def decode(self, shiftAmount: int, input: str) -> str:
|
def decode(self, shiftAmount: int, inputStr: str) -> str:
|
||||||
NotImplemented
|
self.reset()
|
||||||
|
self.__setShift(shiftAmount)
|
||||||
|
self.__setInputString(inputStr)
|
||||||
|
return self.__decode()
|
||||||
|
|
||||||
#Makes sure all of the variables are empty
|
#Makes sure all of the variables are empty
|
||||||
def reset(self):
|
def reset(self):
|
||||||
NotImplemented
|
self.__inputString = self.__outputString = ""
|
||||||
|
self.__shift = 0
|
||||||
|
|
||||||
#Returns the version of the library
|
#Returns the version of the library
|
||||||
@classmethod
|
@classmethod
|
||||||
def getVersion(cls) -> str:
|
def getVersion(cls) -> str:
|
||||||
NotImplemented
|
return cls.__version
|
||||||
|
|
||||||
#This will run the appropriate commands if this script is called stand alone instead of with the rest of the PypherStream program
|
#This will run the appropriate commands if this script is called stand alone instead of with the rest of the PypherStream program
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user