From b23f32ed3063901a150a2755aa59918e643cd5dc Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Wed, 6 Sep 2017 17:33:40 +0530 Subject: [PATCH 1/6] Fixed Bug where J could be present in the Key --- playfair.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/playfair.py b/playfair.py index e741662..b102bf2 100644 --- a/playfair.py +++ b/playfair.py @@ -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): @@ -75,7 +79,7 @@ def encrypt(message): 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; @@ -97,7 +101,7 @@ def cipher_to_digraphs(cipher): return new -def decrypt(cipher): +def decrypt(cipher): cipher=cipher_to_digraphs(cipher) key_matrix=matrix(key) plaintext=[] @@ -110,7 +114,7 @@ def decrypt(cipher): 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; @@ -125,7 +129,7 @@ def decrypt(cipher): for unused in range(len(plaintext)): if "X" in plaintext: plaintext.remove("X") - + output="" for e in plaintext: output+=e @@ -149,8 +153,8 @@ def decrypt(cipher): print "Break the message into digraphs: " print message_to_digraphs(message) print "Matrix: " - print matrix(key) - print "Cipher: " + print matrix(key) + print "Cipher: " print encrypt(message) elif order==2: key=raw_input("Please input the key : ") @@ -161,5 +165,3 @@ def decrypt(cipher): print decrypt(cipher) else: print "Error" - - From 081feefd0144477bb968ae366b481ac75179f5c8 Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Wed, 6 Sep 2017 17:40:18 +0530 Subject: [PATCH 2/6] Matrix now prints in proper row and column fashion --- playfair.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/playfair.py b/playfair.py index b102bf2..a438421 100644 --- a/playfair.py +++ b/playfair.py @@ -5,7 +5,7 @@ def matrix(key): # 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" @@ -145,15 +145,19 @@ 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) + for element in matrix(key): + print element + print "Cipher: " print encrypt(message) elif order==2: From 623590cbd387d73af46d38106de55f8e8fa35a93 Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Wed, 6 Sep 2017 17:45:29 +0530 Subject: [PATCH 3/6] Use X as filler and Z as padding for the digraphs --- playfair.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/playfair.py b/playfair.py index a438421..01d6eca 100644 --- a/playfair.py +++ b/playfair.py @@ -38,6 +38,7 @@ def message_to_digraphs(message_original): 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): @@ -45,9 +46,10 @@ def message_to_digraphs(message_original): message.insert(i+1,'X') i=i+2 - #If it is odd digit, add an "X" at the end + Use "Z" as a padding letter + #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=[] From 5236ee387ee87739556e04a67e7ef4c20a254425 Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Wed, 6 Sep 2017 17:48:00 +0530 Subject: [PATCH 4/6] added comment for the padding --- playfair.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playfair.py b/playfair.py index 01d6eca..4bbf7a9 100644 --- a/playfair.py +++ b/playfair.py @@ -46,7 +46,7 @@ def message_to_digraphs(message_original): message.insert(i+1,'X') i=i+2 - Use "Z" as a padding letter + # Use "Z" as a padding letters #If it is odd digit, add an "Z" at the end if len(message)%2==1: message.append("Z") From 8bc45187c8f326ffd9d735c7901290724af9b460 Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Thu, 7 Sep 2017 10:51:42 +0530 Subject: [PATCH 5/6] Fixed Major Bug of checking lowercase letters in matrix containing uppercase letters --- playfair.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/playfair.py b/playfair.py index 4bbf7a9..b77fd5a 100644 --- a/playfair.py +++ b/playfair.py @@ -33,7 +33,7 @@ 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(" ") @@ -68,13 +68,15 @@ 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 @@ -161,7 +163,7 @@ def decrypt(cipher): print element print "Cipher: " - print encrypt(message) + print encrypt(message, key) elif order==2: key=raw_input("Please input the key : ") cipher=raw_input("Please input the cipher text: ") From 237ad9d9242cb0677fe88299e276575d2170bcff Mon Sep 17 00:00:00 2001 From: AdiK-gh Date: Thu, 7 Sep 2017 10:56:43 +0530 Subject: [PATCH 6/6] =?UTF-8?q?Fixed=C2=A0Major=C2=A0Bug=C2=A0of=C2=A0chec?= =?UTF-8?q?king=C2=A0lowercase=C2=A0letters=C2=A0in=C2=A0matrix=C2=A0conta?= =?UTF-8?q?ining=C2=A0uppercase=C2=A0letters=20in=20decrypt=20function=20a?= =?UTF-8?q?s=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playfair.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playfair.py b/playfair.py index b77fd5a..5a326fe 100644 --- a/playfair.py +++ b/playfair.py @@ -110,8 +110,8 @@ def decrypt(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