mirror of
https://bitbucket.org/Mattrixwv/pypherstream.git
synced 2025-12-06 18:43:58 -05:00
108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
#PypherStream/Caesar.py
|
|
#Matthew Ellison
|
|
# Created: 03-30-19
|
|
#Modified: 03-30-19
|
|
#This file contains the Caesar class for the PypherStream Project.
|
|
#This class takes input and uses a Caesar Cipher to encode or decode a message.
|
|
#This is a Python port of the CipherStream program found at https://bitbucket.org/Mattrixwv/CipherStream
|
|
|
|
|
|
class Caesar:
|
|
__version = "1.0" #The current version number for the library
|
|
def __init__(self):
|
|
self.__inputString = "" #The string that needs encoded/decoded
|
|
self.__outputString = "" #The encoded/decoded string
|
|
self.__shift = 0 #The amount that you need to shift each letter
|
|
#Sets shift and makes sure it is within the propper bounds
|
|
def __setShift(self, shiftAmount: int):
|
|
self.__shift = shiftAmount % 26
|
|
#Sets the input string
|
|
def __setInputString(self, inputString: str):
|
|
self.__inputString = inputString
|
|
#Encodes the inputString and stores the result in outputString
|
|
def __encode(self) -> str:
|
|
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
|
|
def __decode(self) -> str:
|
|
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
|
|
def getInputString(self) -> str:
|
|
return self.__inputString
|
|
#Returns the shift value
|
|
def getShift(self) -> int:
|
|
return self.__shift
|
|
#Returns the outputString
|
|
def getOutputString(self) -> str:
|
|
return self.__outputString
|
|
#Sets the shift and inputString and encodes the message
|
|
def encode(self, shiftAmount: int, inputStr: str) -> str:
|
|
self.reset()
|
|
self.__setShift(shiftAmount)
|
|
self.__setInputString(inputStr)
|
|
return self.__encode()
|
|
#Sets the shift and inputString and decodes the message
|
|
def decode(self, shiftAmount: int, inputStr: str) -> str:
|
|
self.reset()
|
|
self.__setShift(shiftAmount)
|
|
self.__setInputString(inputStr)
|
|
return self.__decode()
|
|
|
|
#Makes sure all of the variables are empty
|
|
def reset(self):
|
|
self.__inputString = self.__outputString = ""
|
|
self.__shift = 0
|
|
|
|
#Returns the version of the library
|
|
@classmethod
|
|
def getVersion(cls) -> str:
|
|
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
|
|
if __name__ == "__main__":
|
|
NotImplemented
|