# Rejcx/School

### Subversion checkout URL

You can clone with
or
.

# Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

# Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
• 3 commits
• 2 files changed
• 1 contributor
Commits on Mar 03, 2013
 Rejcx Created script in Python to encrypt a message using linear algebra eb88a64 Rejcx Had to update how encrypter saved f141d02 Rejcx Decryption script added. Using pickle for serialization of matrix 93d06f0
Showing with 133 additions and 0 deletions.
1. +56 −0 Cryptography/decrypter.py
2. +77 −0 Cryptography/encrypter.py
56 Cryptography/decrypter.py
 @@ -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 )
77 Cryptography/encrypter.py
 @@ -0,0 +1,77 @@ +# Sample encrypter program as an example for my +# Math 300 essay on Cryptography + +import numpy +import pickle + +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 via pickle serialization +pickle.dump( encryptedMatrix, open( "encrypted-message.txt", "wb" ) )

### No commit comments for this range

Something went wrong with that request. Please try again.