Skip to content

Commit 3d80ccb

Browse files
committed
Added Ceaser cipher
1 parent 8d15cda commit 3d80ccb

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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
11+
self.e = dict(zip(string.ascii_lowercase, string.ascii_lowercase[self.key:]+string.ascii_lowercase[:self.key]))
12+
self.e.update(dict(zip(string.ascii_uppercase, string.ascii_uppercase[self.key:]+string.ascii_uppercase[:self.key])))
13+
14+
self.d = dict(zip(string.ascii_lowercase[self.key:]+string.ascii_lowercase[:self.key],string.ascii_lowercase))
15+
self.d.update(dict(zip(string.ascii_uppercase[self.key:]+string.ascii_uppercase[:self.key], string.ascii_uppercase)))
16+
17+
def encryption(self, plaintext):
18+
ans = []
19+
for letter in plaintext:
20+
if letter in self.e:
21+
ans.append(self.e[letter])
22+
else:
23+
letter
24+
return ''.join(ans)
25+
26+
def decryption(self, ciphertext):
27+
ans = []
28+
for letter in ciphertext:
29+
if letter in self.d:
30+
ans.append(self.d[letter])
31+
else:
32+
letter
33+
return ''.join(ans)
34+
35+
print("Input the Key: ")
36+
key = int(input(""))
37+
c = CaeserCipher(key) # Default key value is 3 but you can change value of key
38+
print("Enter the Message which you want to Encrypt: ")
39+
plain_text = input("")
40+
print("Original text: ", plain_text)
41+
encrypted_text = c.encryption(plain_text)
42+
print("Encrypted text: ", encrypted_text)
43+
decrypted_text = c.decryption(encrypted_text)
44+
print("Decrypted text: ", decrypted_text)
24.2 KB
Loading
24.3 KB
Loading
24.8 KB
Loading

Ciphers/Caesar_cipher/README.md

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

0 commit comments

Comments
 (0)