Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6ff87ab

Browse files
committed
Ceaser Cipher
1 parent 1361ca9 commit 6ff87ab

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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)

PythonDataStructsAndAlgo/Notes.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,19 @@
144144
for k from 1 to n-1 do
145145
Insert A[k] at its proper location within A[0], A[1],..., A[k]
146146

147-
### Simple cryptography
147+
### Using Characters as Array Indices
148+
149+
> Example A=0, B=1 etc.. So When writing a Ceaser Cipher Algorithm, we apply the rotation of r as a simple formula. Replace each letter i with the letter (i+r) mode 26 which returns the remainder after performing an integer division.
150+
151+
> In Python characters are represented in UNICODE by integer code points, and the code points for the uppercase letters are consecutive, thus for simplicity we restrict the encryption to used only uppercase letters.
152+
153+
> Function to retrive the UNICODE Integer value in python is *ord(c)* which takes a parameter character c and returns the integer value associated with it.
154+
155+
>Function to retrive a one character string from a UNICODE integer is *chr(j)* where j is an integer value that will return the one character string.
156+
157+
### Multi dimensional data sets
158+
159+
> 2-D arrays can be also called matrices.
160+
161+
> *A common representation for a 2-D list in Python is a list of lists*
162+
>> Example: data=[[22,18,709,5,33], [45,32,830,120,750], [4,880,45,66,61]] which is a matrix of 3 x 4 3 rows, and 4 columns

0 commit comments

Comments
 (0)