Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions playfair.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@

def matrix(key):
matrix=[]

for e in key.upper():
# we ignore the letter "J" and convert all occurances of "J" in the key to "I"
if e == "J":
e = "I"

if e not in matrix:
matrix.append(e)
alphabet="ABCDEFGHIKLMNOPQRSTUVWXYZ"

for e in alphabet:
if e not in matrix:
matrix.append(e)
matrix.append(e)

#initialize a new list. Is there any elegant way to do that?
matrix_group=[]
for e in range(5):
Expand All @@ -29,21 +33,23 @@ def message_to_digraphs(message_original):
for e in message_original:
message.append(e)

#Delet space
#Delete space
for unused in range(len(message)):
if " " in message:
message.remove(" ")

# Use "X" as a filler letter
#If both letters are the same, add an "X" after the first letter.
i=0
for e in range(len(message)/2):
if message[i]==message[i+1]:
message.insert(i+1,'X')
i=i+2

#If it is odd digit, add an "X" at the end
# Use "Z" as a padding letters
#If it is odd digit, add an "Z" at the end
if len(message)%2==1:
message.append("X")
message.append("Z")
#Grouping
i=0
new=[]
Expand All @@ -62,20 +68,22 @@ def find_position(key_matrix,letter):

return x,y

def encrypt(message):
def encrypt(message, key):
message=message_to_digraphs(message)
key_matrix=matrix(key)
cipher=[]
for e in message:
p1,q1=find_position(key_matrix,e[0])
p2,q2=find_position(key_matrix,e[1])

# There was an error here as the lowercase letters were checked in the key_matrix containing uppercase letters
p1,q1=find_position(key_matrix,e[0].upper())
p2,q2=find_position(key_matrix,e[1].upper())
if p1==p2:
if q1==4:
q1=-1
if q2==4:
q2=-1
cipher.append(key_matrix[p1][q1+1])
cipher.append(key_matrix[p1][q2+1])
cipher.append(key_matrix[p1][q2+1])
elif q1==q2:
if p1==4:
p1=-1;
Expand All @@ -97,20 +105,20 @@ def cipher_to_digraphs(cipher):
return new


def decrypt(cipher):
def decrypt(cipher):
cipher=cipher_to_digraphs(cipher)
key_matrix=matrix(key)
plaintext=[]
for e in cipher:
p1,q1=find_position(key_matrix,e[0])
p2,q2=find_position(key_matrix,e[1])
p1,q1=find_position(key_matrix,e[0].upper())
p2,q2=find_position(key_matrix,e[1].upper())
if p1==p2:
if q1==4:
q1=-1
if q2==4:
q2=-1
plaintext.append(key_matrix[p1][q1-1])
plaintext.append(key_matrix[p1][q2-1])
plaintext.append(key_matrix[p1][q2-1])
elif q1==q2:
if p1==4:
p1=-1;
Expand All @@ -125,7 +133,7 @@ def decrypt(cipher):
for unused in range(len(plaintext)):
if "X" in plaintext:
plaintext.remove("X")

output=""
for e in plaintext:
output+=e
Expand All @@ -141,17 +149,21 @@ def decrypt(cipher):


print "Playfair Cipher"
order=input("Choose :\n1,Encrypting \n2,Decrypting\n")
order=input("Choose :\n1.Encryption \n2.Decryption\n")
if order==1:
key=raw_input("Please input the key : ")
message=raw_input("Please input the message : ")
print "Encrypting: \n"+"Message: "+message
print "Break the message into digraphs: "
print message_to_digraphs(message)

# print the matrix in a bettter way
print "Matrix: "
print matrix(key)
print "Cipher: "
print encrypt(message)
for element in matrix(key):
print element

print "Cipher: "
print encrypt(message, key)
elif order==2:
key=raw_input("Please input the key : ")
cipher=raw_input("Please input the cipher text: ")
Expand All @@ -161,5 +173,3 @@ def decrypt(cipher):
print decrypt(cipher)
else:
print "Error"