Permalink
Browse files

Created script in Python to encrypt a message using linear algebra

  • Loading branch information...
1 parent 5d21504 commit eb88a64a9e0e7ca68ac536200858fa6e66b9251c @RachelJMorris committed Mar 3, 2013
Showing with 80 additions and 0 deletions.
  1. +80 −0 Cryptography/encrypter.py
View
80 Cryptography/encrypter.py
@@ -0,0 +1,80 @@
+# Sample encrypter program as an example for my
+# Math 300 essay on Cryptography
+
+import numpy
+import sys
+
+continueProgram = "N"
+
+while ( continueProgram.upper() == "N" ):
+
+ message = raw_input( "Please enter message to encrypt: " )
+ message = message.upper()
+
+ 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( "Message to encrypt: \"" + message + "\"" )
+ print( "Ciper Matrix dimensions: " + rows + " x " + cols )
+ print( "Matrix: " )
+ print( cipherMatrix )
+
+ continueProgram = raw_input( "Continue? [Y/N] " )
+
+print( "\nGENERATE ENCRYPTION ARRAY" )
+# Represent each letter by a number between 1 - 26, with 27 being a space
+# The amount of ROWS in this matrix needs to match the amount of ROWS in the
+# Cipher matrix
+
+msgRows = int(cols)
+msgCols = message.__len__() / int(msgRows)
+if ( message.__len__() % int(msgRows) != 0 ):
+ msgCols += 1
+
+print( "Cipher Matrix is " + rows + " x " + cols )
+print( "Message Matrix is " + str(msgRows) + " x " + str(msgCols) )
+
+messageMatrix = numpy.zeros( shape=(int(msgRows), int(msgCols) ) )
+
+r = 0
+c = 0
+for letter in message:
+ messageMatrix[r, c] = ord( letter ) - 65 + 1 # Account for ascii, but also start at 1 instead of 0.
+ if ( letter == ' ' ): # account for spaces
+ messageMatrix[r, c] = 27
+
+ r += 1
+ if ( r == int(msgRows) ):
+ r = 0
+ c += 1
+
+# Fill remaining space with the "space" character (27)
+while ( r != msgRows and c != msgCols ):
+ messageMatrix[r, c] = 27
+
+ r += 1
+ if ( r == int(msgRows) ):
+ r = 0
+ c += 1
+
+print( "\nMESSAGE MATRIX:" )
+print( messageMatrix )
+
+print( "\nMULTIPLY CIPHER MATRIX BY MESSAGE MATRIX:" )
+encryptedMatrix = numpy.dot( cipherMatrix, messageMatrix )
+print( encryptedMatrix )
+
+# Output encrypted message to text file
+encryptedMessageFile = open( "encrypted-message.txt", "w" )
+
+encryptedMessageFile.write( encryptedMatrix )
+

0 comments on commit eb88a64

Please sign in to comment.