Cryptographic
Algorithms
Types of Algorithm
• Secret Key (Symmetric Key) Algorithm
• Public Key (Asymmetric Key) Algorithm
Secret Key Cryptography
• Using a single key for encryption/decryption.
• The plaintext and the cipher text having the same
size
DES (Data Encryption Standard)
3DES (Triple DES)
IDEA (International Data Encryption Algorithm)
AES (Advanced Encryption Standard)
Public Key Cryptography
• Both the parties have two keys: Public Key,
Private Key
• Eg: RSA Algorithm
Alice Bob
encrypt mA using eB encrypt mA using dB
Classical Encryption Techniques
• Substitution cipher
Replacing each element of the plaintext with
another element.
• Transposition (or permutation) cipher
Rearranging the order of the elements of the
plaintext.
Substitution Ciphers
Caeser Cipher:
• Earliest known substitution cipher
• Invented by Julius Caesar
• Each letter is replaced by the letter three
positions further down the alphabet.
• Example: ohio state RKLR VWDWH
Caeser Cipher
• Mathematically, map letters to numbers:
a, b, c, ..., x, y, z
0, 1, 2, ..., 23, 24, 25
• Then the general Caesar cipher is:
c = EK(p) = (p + k) mod 26
p = DK(c) = (c – k) mod 26
Monoalphabetic Cipher
• Shuffle the letters and map each plaintext letter to a
different random ciphertext letter:
Plain letters: abcdefghijklmnopqrstuvwxyz
Cipher letters: DKVQFIBJWPESCXHTMYAUOLRGZN
Plaintext: ifwewishtoreplaceletters
Ciphertext: WIRFRWAJUHYFTSDVFSFUUFYA
• Now we have a total of 26! = 4 x 1026 keys.
• With so many keys, it is secure against brute-force
attacks.
• Playfair Cipher
• Polyalphabetic Substitution Cipher
Vigenere Cipher
Caeser Cipher Implementation
• https://repl.it/
#taking the input from user
plaintext = input("Enter a text to encrypt: ")
key = input("Enter the value of key: ")
# encrypting the plaintext
def encrypt(plaintext, key):
ciphertext = ""
for i in range(len(plaintext)):
char = plaintext[i]
if (char.isalpha()):
if (char.isupper()):
ciphertext += chr((ord(char)-65 + int(key))%26 + 65)
else:
ciphertext += chr((ord(char)-97 + int(key))%26 + 97)
else:
ciphertext += char
return ciphertext
# decrypting the cipher Text
def decrypt(ciphertext, key):
plain = ""
for i in range(len(ciphertext)):
char = ciphertext[i]
if (char.isalpha()):
if (char.isupper()):
plain += chr((ord(char)-65 - int(key))%26 + 65)
else:
plain += chr((ord(char)-97 - int(key))%26 + 97)
else:
plain += char
return plain
print("Plain Text : ", plaintext)
print("Key for shift : ",str(key))
cipher = encrypt(plaintext,key)
print("Cipher Text: ",cipher)
print("Decrypted Text: ", decrypt(cipher, key))
Brute Force Attack
Try to decrypt this:
GUVF VF ZL FRPERG ZRFFNTR
cipher = input("Enter cipher text to decrypt")
for i in range(1, 26):
plaintext = ""
for j in range(len(cipher)):
char = cipher[j]
if (char.isalpha()):
if (char.isupper()):
plaintext += chr((ord(char)-65 - i)%26 + 65)
else:
plaintext += chr((ord(char)-97 - i)%26 + 97)
else:
plaintext += char
print(" For Key = “, str(i), "Plaintext = “, plaintext)