0% found this document useful (0 votes)
23 views

+ Ciphers.

Uploaded by

Ammar Mousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

+ Ciphers.

Uploaded by

Ammar Mousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Functions.

- The chr(<num>) function returns the character that represents the specified Unicode dec.

- The ord(<num>) function returns the number representing the unicode dec of a specified character.

Example :
x = ord("h")
y = chr("97")
print(x, y) # 104 , 97

English alphabet decimal value


uppercase Dec lowercase Dec
A 65 a 97
B 66 b 98
C 67 c 99
D 68 d 100
E 69 e 101
F 70 f 102
G 71 g 103
H 72 h 104
I 73 i 105
J 74 j 106
K 75 k 107
L 76 l 108
M 77 m 109
N 78 n 110
O 79 o 111
P 80 p 112
Q 81 q 113
R 82 r 114
S 83 s 115
T 84 t 116
U 85 u 117
V 86 v 118
W 87 w 119
X 88 x 120
Y 89 y 121
Z 90 z 122
1- Ceaser
The Algorithm.

# input:
String of letters ‘plaintext 𝑃(𝑥) or ciphertext 𝐶(𝑥)’
An Integer denoting the required shift ‘the key 𝐾’.

# procedure:
step 1) Give each character a number: [𝑎 − 𝑧] ← [0 − 25].
step 2) Convert each letter in the string by its value number
step 3) Traverse the text one character ‘𝑥’ at a time
step 4) shift each character based on
- For encrypt 𝐶(𝑥) = (𝑥 + 𝑠) 𝑚𝑜𝑑 26
- For decrypt 𝑃(𝑥) = (𝑥 − 𝑠) 𝑚𝑜𝑑 26
step 5) return the new shifted text

The Code.
alph=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
T=str(input( "Enter the string: "))
S=int(input( "enter number of shifts: "))
result=""

for i in T :
x=alph.index(i.lower())
ceasar = (x + S) % 26 #for decrypt ( x <-> S )

if i.isupper():
result+=alph[ceasar].upper()
else:
result+=alph[ceasar]

print(result)

Another Code.
text=str(input( "Enter the string: "))
shift= int(input( "enter number of shifts: " ))
result = ""

for i in text:
if (i.isupper()): # For uppercase
result += chr((ord(i)+ shift -65 ) % 26 + 65) #for decrypt ( ord(i) <-> Shifs )

else: # For lowercase


result += chr((ord(i)+ shift -97 ) % 26 + 97) #for decrypt ( ord(i) <-> Shifs )

print(result)
2- Vigenère
The Algorithm.

# input:
The text ‘plaintext 𝑃(𝑥) or ciphertext 𝐶(𝑥)’
The key as string of letters

# procedure:
step 1) give each letter a number: [𝑎 − 𝑧] ← [0 − 25].
step 2) Convert each letter in the string by its value number
step 3) Test if len(key) > len(text):
repeat the letters of the key until len(key) == len(plain text).
step 4) Traverse the text one character ′𝑥’ at a time
step 5) shift each character on the text by the value of corresponding letter in the key
• For encrypt 𝐶(𝑥) = (𝑥 + 𝑘) 𝑚𝑜𝑑 26 • For decrypt 𝑃(𝑥) = (𝑥 − 𝑘) 𝑚𝑜𝑑 26
step 6) return the new shifted text

The Code.
string=input( "Enter the string: ")
key=input( "enter the key: " )
result = ''
if len(string) > len(key): # generating the key
for i in range(len(string) - len(key)):
key+=key[i % len(key)] # <%> for if the text is so much bigger than key.
for i in range(len(string)):
if string[i].isupper(): # for uppercase
result += chr( ( (ord(string[i])-97) + (ord(key[i])-97) ) % 26 +97).upper()
else: # for lowercase
result += chr( ( (ord(string[i])-97) + (ord(key[i])-97) ) % 26 +97 )
print (result)
# for decrypt

Another Code.
alph=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
string=input( "Enter the string: ")
key=input( "enter the key: " ).lower() #<.lower()> because i need key in lower case
result=''

if len(string) > len(key): # generating the key


for i in range(len(string) - len(key)):
key+=key[i % len(key)] # <%> for if the text is so much bigger than key.

for i in range(len(string)):
x=alph.index(string[i].lower())
k=alph.index(key[i])
vegener = (x + k) % 26

if string[i].isupper(): # for uppercase


result+=alph[vegener].upper()
else:
result+=alph[vegener] # for lowercase
print(result)
3- Monoalphabetic
The Algorithm.

The Code.
alph=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
text=str(input( "Enter the string: "))
key=input('Enter the key:\n' )
result=''

while len(key) != 26: #make sure that the key is 26 letter


key=input('invalid key, Eenter it again:\n' )

for i in text: #for decrypt


l=alph.index(i) #l=key.index(i)
result+= key[l] #result+=alph[l]
print('\nletters: ', alph)
print('the key: ', list(key))
print(result)
4- Affine
The Algorithm.

# input:
String of letters ‘plaintext 𝑃(𝑥) or ciphertext 𝐶(𝑥)’
Two integer a,b represnt ‘the key 𝐾’.

# procedure:
step 1) give each character a number: [𝑎 − 𝑧] ← [0 − 25].
step 2) Convert each letter in the string by its value number
step 3) Traverse the text one character ‘𝑥’ at a time
step 4) convert each character on the text based on
- For encrypt 𝐶(𝑥) = (𝑎𝑥 + 𝑏) 𝑚𝑜𝑑 26
- For decrypt 𝑃(𝑥) = 𝑎−1 (𝑥 − 𝑏) 𝑚𝑜𝑑 26
step 5) return the text

The Code.
alph=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
text=input( "Enter the text: ")
print( "enter the key: " )
a , b=int(input("a= ")) , int(input('b= '))
result=''

y=0 #modulo inverse of a -> (y=a^-1)


for x in range(1, 26):
if (((a%26) * (x%26)) % 26 == 1):
y=x

for i in text:
x=alph.index(i.lower())
affine = (a*x + b) % 26 #for decrypt <affine = y*(x - b) % 26>

if i.isupper():
result+=alph[affine].upper()
else:
result+=alph[affine]
print(result)
5- Rail fences
The Algorithm.

The Code.

def railfence():
text=input( "Enter the text: ") ; key=2 #key is number of lines ‘rail’
def encrypt():
list1,list2=[],[]
for i in range(0,len(text),key):
try:
j=i+1
list1.append(text[i])
list2.append(text[j])
except:
pass
encrypt="".join(list1+list2)
print(encrypt)
def decrypt():
list3,list4=[],[]
decrypt=''
for i in range(0 , int((len(text)+1)/2)):
list3.append(text[i])

for i in range(int((len(text)+1)/2) ,len(text)):


list4.append(text[i])

for i in range(0,len(list3)) :
try:
decrypt+=list3[i]+list4[i]
except:
decrypt+=list3[i]
print(decrypt)

decrypt() #call the function that you wanna use

railfence()
Another code with 3 rails
def railfence():
text=input( "Enter the text: ") ; key=3

def encrypt():
list1,list2,list3=[],[],[]
for i in range(0,len(text),key):
try:
j=i+1 ; k=i+2
list1.append(text[i])
list2.append(text[j])
list3.append(text[k])
except:
pass

encrypt="".join(list1+list2+list3)
print(encrypt)

def decrypt():
list4,list5,list6=[],[],[]
decrypt=''

for i in range(0 , int((len(text)+1)/3)):


list4.append(text[i])

for i in range(int((len(text)+1)/3) ,int((2*(len(text)+1))/3)):


list5.append(text[i])

for i in range(int((2*(len(text)+1))/3) ,len(text)):


list6.append(text[i])

for i in range(0,len(list4)) :
try:
decrypt+=list4[i]+list5[i]+list6[i]
except:
try:
decrypt+=list4[i]+list5[i]
except:
decrypt+=list4[i]

print(decrypt)

decrypt()

railfence()
6- Hill
The Algorithm.

# input:
String of letters represent ‘plaintext 𝑃(𝑥) or ciphertext 𝐶(𝑥)’
Another string of letters represent ‘the key 𝐾’.

# procedure:
step 1) give each character a number: [𝑎 − 𝑧] ← [0 − 25].
step 2) Convert each letter in the string by its value number
step 3) Generate the key as a matrix 𝑛 × 𝑛
step 4) devide litter of the text into matrices 1 × 𝑛
- for encypt: multiply key matrix by one text matrix at time
- for decrypt: multiply the invers of the key matrix by one text matrix at time
step 5) return the obtained matrices as string of letters

The Code.
text=input("enter the text: ") #"gfg"
key = input("enter the key: ") #'hillmagic'
m=int(len(key)**(0.5))

def getMatrix(x):
l=[]
for i in x:
l.append(ord(i))

y=[]
for i in range(0,len(l),m):
y.append(list(l[i:i+m]))

while len(y[-1]) < m:


y[-1].append(32)
return(y)
Mtext=getMatrix(text)
Mkey=getMatrix(key)
Mresult=[]

for i in range(len(Mtext)):
for j in range(len(Mkey)):
result=0
for k in range(len(Mkey[0])):
result+=(Mkey[j][k]-97)*(Mtext[i][k]-97)

Mresult.append(chr((result%26)+97))
print(''.join(Mresult)) #swk
7- permutation
The Algorithm.
Step 1: Start
Step 2: Read cipher, plaintext
Step 3: Re-arrange plaintext in the order shown in cipher
Step 4: ciphertext :=re-arranged plaintext
Step 5: Print ciphertext
Step 6: End

The Code.
def decrypt(cipher, ciphertext):
return encrypt(inverse_key(cipher), ciphertext)

def encrypt(cipher, plaintext):


plaintext = "".join(plaintext.split(" ")).upper()
ciphertext = ""
for pad in range(0, len(plaintext)%len(cipher)*-1%len(cipher)):
plaintext += "X"
for offset in range(0, len(plaintext), len(cipher)):
for element in [a-1 for a in cipher]:
ciphertext += plaintext[offset+element]
ciphertext += " "
return ciphertext[:-1]

def inverse_key(cipher):
inverse = []
for position in range(min(cipher),max(cipher)+1,1):
inverse.append(cipher.index(position)+1)
return inverse

cipher = [2,4,1,5,3]
plaintext = "LOREM IPSUM DOLOR SITAM ETCON SECTE TUERA DIPIS CINGE LITXX"
ciphertext = encrypt(cipher, plaintext)
print(ciphertext)

cipher=[2,4,1,5,3]
ciphertext="OELMR PUIMS OODRL IASMT TOENC ETSEC URTAE IIDSP IGCEN IXLXT"
plaintext=decrypt(cipher,ciphertext)
print(plaintext)
8- OTP
The Algorithm.

The Code.
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
K 10
L 11
M 12
N 13
O 14
P 15
Q 16
R 17
S 18
T 19
U 20
V 21
W 22
X 23
Y 24
Z 25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy