Commits

Cédric Bonhomme  committed e164c2d Draft

sync of hill and permutation.

  • Participants
  • Parent commits f9a160e

Comments (0)

Files changed (2)

 
 import utils
 
-def encrypt(message, matrix, encryption=True):
+def encrypt(message, matrix):
     """
     Hill encryption (decryption).
     """
         message = message + 'X'
     couple = [list(message[i*2:(i*2)+2]) for i in range(0, len(message)/2)]
     result = [i[:] for i in couple]
-    if not encryption:
-        # To decrypt, just need to inverse the matrix.
-        matrix = utils.inverse_matrix(matrix)
     for i, c in enumerate(couple):
         if c[0].isalpha() and c[1].isalpha():
+            """
+            result[i][0] = chr(((ord(c[0])-65) * matrix[0][0] + \
+                                    (ord(c[1])-65) * matrix[0][1]) % 26 + 65)
+            result[i][1] = chr(((ord(c[0])-65) * matrix[1][0] + \
+                                    (ord(c[1])-65) * matrix[1][1]) % 26 + 65)"""
             result[i][0] = chr(((ord(c[0])-65) * matrix[0][0] + \
                                     (ord(c[1])-65) * matrix[0][1]) % 26 + 65)
             result[i][1] = chr(((ord(c[0])-65) * matrix[1][0] + \
     """
     Hill decryption.
     """
-    return encrypt(cypher, matrix, False)
+    # To decrypt, just need to inverse the matrix.
+    return encrypt(cypher, utils.inverse_matrix(matrix))
 
 if __name__ == '__main__':
     # Point of entry in execution mode
-    print encrypt("Vivement les vacances !", [[11, 3], [8, 7]])
-    print decrypt(encrypt("Vivement les vacances !", [[11, 3], [8, 7]]), [[11, 3], [8, 7]])
+    print encrypt("CHIFFREMENT", [[9, 5], [4, 7]])
+    print decrypt(encrypt("CHIFFREMENT", [[9, 5], [4, 7]]), [[9, 5], [4, 7]])
+    print
+    print decrypt(encrypt("Vivement les vacances !", [[11, 3], [8, 7]]), [[11, 3], [8, 7]])
+

File permutation.py

 
 if __name__ == '__main__':
     # Point of entry in execution mode
-    #print decrypt(encrypt("BIENTOTLESVACANCES", [2,4,0,1,3]), [2,4,0,1,3])
-    print bruteforce(encrypt("BIENTOTLESVACANCES", [2,4,0,1,3]))
+    print encrypt("J'aime les vacances!", [2, 4, 0, 1, 3])
+    print decrypt(encrypt("J'aime les vacances!", [2, 4, 0, 1, 3]), [2, 4, 0, 1, 3])
+    #print bruteforce(encrypt("BIENTOTLESVACANCES", [2,4,0,1,3]))