+ Ciphers.
+ Ciphers.
- 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
# 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 )
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=''
for i in range(len(string)):
x=alph.index(string[i].lower())
k=alph.index(key[i])
vegener = (x + k) % 26
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=''
# 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=''
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(0,len(list3)) :
try:
decrypt+=list3[i]+list4[i]
except:
decrypt+=list3[i]
print(decrypt)
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,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]))
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 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