|
| 1 | +class CeaserCipher: |
| 2 | + """Class for doing encryption and decryption of ceaser cipher criptography""" |
| 3 | + |
| 4 | + def __init__(self, shift): |
| 5 | + """Construct Ceaser Cipher using the given integer shight for rotation""" |
| 6 | + """shift parameter holds the value for the shifting of the character in the list for the |
| 7 | + encryption""" |
| 8 | + #temp array for encryption |
| 9 | + encoder=[None] * 26 #creates a list containing 26 indices with value None |
| 10 | + |
| 11 | + #temp array for decryption |
| 12 | + decoder=[None] * 26 #creates a list containing 26 indices with value None |
| 13 | + |
| 14 | + for k in range(26): |
| 15 | + encoder[k]= chr((k+shift) % 26 + ord('A')) #reamainder of index k + value of shift + Unicode integer value of letter A |
| 16 | + # example: 0+3 % 26 + 65 = 3+65 =68 -> which is letter D so we shift letter A 3 places to the left going to the end of the list |
| 17 | + |
| 18 | + decoder[k]=chr((k-shift) % 26 + ord('A')) #remainder index k - shift value + the UNICODE value of A (65) |
| 19 | + # example: 3-6 % 26 + 65= -3 +65 = 62 |
| 20 | + |
| 21 | + self._forward=''.join(encoder) # creates a string object from the list encoder |
| 22 | + self._backward=''.join(decoder) # creates a string object from the list decoder |
| 23 | + |
| 24 | + def encrypt(self, message): |
| 25 | + """Function that takes the String message object and applies the function forward to shift the elements of the encoder list""" |
| 26 | + """Returns the string representation of the encrypted message""" |
| 27 | + return self._transform(message, self._forward) |
| 28 | + |
| 29 | + def decrypt(self, secret): |
| 30 | + """Returns the String of the secret which is the encrypted message""" |
| 31 | + return self._transform(secret, self._backward) |
| 32 | + |
| 33 | + def _transform(self, original, code): |
| 34 | + """Function to perform transformation based on given code string""" |
| 35 | + msg=list(original) #msg becomes a list of the string object orginal containing on each index a character from string object original |
| 36 | + |
| 37 | + """for k in length of the list message that takes original message and encrypts and takes secret message and decrypts""" |
| 38 | + for k in range(len(msg)): |
| 39 | + if msg[k].isupper(): # if letter at msg[k] is an Upper case letter |
| 40 | + j= ord(msg[k]) - ord('A') # object j will hold the value of the UNICODE integer of the character at msg[k] minus UNICODE of A which is 65 |
| 41 | + msg[k]= code[j] # We then assign to msg[k] the character that is at position j in the list code which is based on the encoder or decoder list from previous functions |
| 42 | + #giving us the list containing the secret message or the original message |
| 43 | + return ''.join(msg) #returns the list msg in a string object |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + cipher=CeaserCipher(3) |
| 47 | + message= "DIOGO" |
| 48 | + coded=cipher.encrypt(message) |
| 49 | + print('Secret: ', coded) |
| 50 | + |
| 51 | + answer= cipher.decrypt(coded) |
| 52 | + print('Message: ', answer) |
0 commit comments