Skip to content

Commit 7bb8661

Browse files
committed
Added Caesar Cipher Algorithm
1 parent aea691c commit 7bb8661

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import string
2+
3+
# Ceaser Cipher
4+
# plain text: A B C D.... (if key is 3) || ascii value: 65, 66, 67, 68
5+
# cipher text: D E F G (adding the key to the ascii value) || ascii value: 68, 69, 70, 71
6+
7+
8+
class CaeserCipher:
9+
def __init__(self, key=3):
10+
self.key = key%26 # whatever the key value it is append to the ascii value of the letter
11+
12+
# e is dictonary in which every key element store the item as ascii value of key element + key
13+
# for storing the lowercase items
14+
self.e = dict(zip(string.ascii_lowercase, string.ascii_lowercase[self.key:]+string.ascii_lowercase[:self.key])) # {'a': 'd', 'b': 'e'.......'z':'c'}
15+
#for storing the uppercase items
16+
self.e.update(dict(zip(string.ascii_uppercase, string.ascii_uppercase[self.key:]+string.ascii_uppercase[:self.key]))) # After updating the e {'a': 'd', 'b': 'e'.......'z': 'c', 'A': 'D', 'B':'E',.....'Z':'C'}
17+
18+
# same as above description
19+
self.d = dict(zip(string.ascii_lowercase[self.key:]+string.ascii_lowercase[:self.key],string.ascii_lowercase))
20+
self.d.update(dict(zip(string.ascii_uppercase[self.key:]+string.ascii_uppercase[:self.key], string.ascii_uppercase)))
21+
22+
# encryption function
23+
def encryption(self, plaintext):
24+
ans = [] # To store the letters of the encrypted message
25+
for letter in plaintext: # iterate every letter of the plain text
26+
if letter in self.e:
27+
ans.append(self.e[letter])
28+
else:
29+
letter
30+
return ''.join(ans)
31+
32+
# decryption function
33+
def decryption(self, ciphertext):
34+
ans = [] # To store the letters of the decrypted message
35+
for letter in ciphertext: # iterate every letter of the cipher text
36+
if letter in self.d:
37+
ans.append(self.d[letter])
38+
else:
39+
letter
40+
return ''.join(ans)
41+
42+
print("Input the Key: ")
43+
key = int(input(""))
44+
c = CaeserCipher(key) # Default key value is 3 but you can change value of key
45+
print("Enter the Message which you want to Encrypt: ")
46+
plain_text = input("") # plain text which we have to enter the message
47+
print("Original text: ", plain_text)
48+
encrypted_text = c.encryption(plain_text) # encrypted text which is return by the encryption function
49+
print("Encrypted text: ", encrypted_text)
50+
decrypted_text = c.decryption(encrypted_text) # decryption text which is return by the decryption function
51+
print("Decrypted text: ", decrypted_text)
24.2 KB
Loading
24.3 KB
Loading
24.8 KB
Loading

Ciphers/Caesar Cipher/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# Ceaser_cipher
3+
### Aim
4+
To Implement the Ceaser cipher
5+
6+
### Purpose
7+
- The Ceaser cipher Algorithm is simple to understand and implement.
8+
This is used to Encrypt the message and Decrypt the message.
9+
10+
### Description
11+
12+
- In cryptography, a Caesar cipher also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.
13+
It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.
14+
15+
### Workflow
16+
- The complete process depends on length of the alphabets used.
17+
- The letter of an alphabet of size m are first mapped to the integers in the range 0...m-1.
18+
- Each letter of a given text is replaced by a letter some fixed number of positions down the alphabet.
19+
- For example with a shift of 1, A would be replaced by B, B would become C, and so on. The method is apparently named after Julius Caesar, who apparently used it to communicate with his officials.
20+
- Thus to cipher a given text we need an integer value, known as shift which indicates the number of position each letter of the text has been moved down.
21+
- The encryption can be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a shift n can be described mathematically as.
22+
23+
### Compilation Steps
24+
25+
- Download the file Ceaser_cipher.py
26+
- Run the File Ceaser_cipher.py
27+
- Input any key
28+
- Input the Message
29+
- And finally we will get Encrypted message and Decrypted Message
30+
31+
# Outputs
32+
33+
### Output 1
34+
![Output_1](https://user-images.githubusercontent.com/78487398/135653838-f537c782-875b-4c1a-aef7-3dd7dc8c315b.png)
35+
36+
37+
### Output 2
38+
![Output_2](https://user-images.githubusercontent.com/78487398/135654075-6a373e48-ecb1-403f-8da5-520ed9eb31d9.png)
39+
40+
41+
### Output 3
42+
![Output_3](https://user-images.githubusercontent.com/78487398/135654147-e97beaed-c5d0-4d8c-8548-6ec2a6df17d1.png)
43+
44+
### Author
45+
[Manthan Nagpurkar](https://github.com/manthan0227)
46+

0 commit comments

Comments
 (0)