Permalink
Browse files

Decryption script added. Using pickle for serialization of matrix

  • Loading branch information...
1 parent f141d02 commit 93d06f0496d7891df528f043289f7220c715b334 @RachelJMorris committed Mar 3, 2013
Showing with 59 additions and 9 deletions.
  1. +56 −0 Cryptography/decrypter.py
  2. +3 −9 Cryptography/encrypter.py
View
@@ -0,0 +1,56 @@
+import numpy
+from numpy import linalg
+import pickle
+
+continueProgram = "N"
+
+while ( continueProgram.upper() == "N" ):
+ messageFilename = raw_input( "Please enter filename of encrypted message: " )
+ encryptedMatrix = pickle.load( open( messageFilename, "rb" ) ) # pickle is awesome!
+
+ print( "\nRECEIVED ENCRYPTED MESSAGE:" )
+ print( encryptedMatrix )
+
+ print( "\nCIPER MATRIX INFORMATION" )
+ rows = raw_input( "Please enter the amount of ROWS for the Ciper Matrix: " )
+ cols = raw_input( "Please enter the amount of COLUMNS for the Ciper Matrix: " )
+
+ cipherMatrix = numpy.zeros( shape=(int(rows), int(cols)) )
+ for r in range( 0, int(rows) ):
+ print( "" )
+ for c in range( 0, int(cols) ):
+ cell = raw_input( "Please enter value at (" + str(r) + ", " + str(c) + "): " )
+ cipherMatrix[r, c] = int(cell)
+
+ print( "\nVERIFY INFO" )
+ print( "Ciper Matrix dimensions: " + rows + " x " + cols )
+ print( "Matrix: " )
+ print( cipherMatrix )
+
+ continueProgram = raw_input( "Continue? [Y/N] " )
+
+# To decrypt the message, we now take the Encrypted Matrix
+# and multiply it by the inverse of the Cipher Matrix
+
+print( "\nCIPHER MATRIX INVERSE:" )
+cipherInverse = linalg.inv( cipherMatrix )
+print( cipherInverse )
+
+print( "\nDECRYPT MESSAGE" )
+messageMatrix = numpy.dot( cipherInverse, encryptedMatrix )
+
+print( "Number code matrix: " )
+print( messageMatrix )
+print( "Convert back to text: " )
+
+message = ""
+for c in range( 0, messageMatrix.shape[1] ):
+ for r in range( 0, messageMatrix.shape[0] ):
+ if ( int(messageMatrix[r, c]) == 27 ):
+ #space character
+ message += " "
+ else:
+ message += chr( int(messageMatrix[r, c]) + 64 )
+
+print( "\nDECODED MESSAGE:" )
+print( message )
View
@@ -2,7 +2,7 @@
# Math 300 essay on Cryptography
import numpy
-import sys
+import pickle
continueProgram = "N"
@@ -73,11 +73,5 @@
encryptedMatrix = numpy.dot( cipherMatrix, messageMatrix )
print( encryptedMatrix )
-# Output encrypted message to text file
-encryptedMessageFile = open( "encrypted-message.txt", "w" )
-
-for r in range( 0, int( rows ) ):
- for c in range( 0, int( cols ) ):
- encryptedMessageFile.write( "\nrow " + str(r) + " col " + str(c) + " val " + str(int(encryptedMatrix[r, c])) )
-
-
+# Output encrypted message to text file via pickle serialization
+pickle.dump( encryptedMatrix, open( "encrypted-message.txt", "wb" ) )

0 comments on commit 93d06f0

Please sign in to comment.