Created
November 7, 2011 19:17
-
-
Save mjcarroll/1345860 to your computer and use it in GitHub Desktop.
Additive encryption toy script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.mlab as mlab | |
SHARED_KEY = "Michael".lower() | |
PLAIN_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus accumsan neque id diam scelerisque et suscipit magna accumsan. Aenean suscipit lorem a sem semper non tincidunt nibh rhoncus. Morbi mattis sollicitudin dapibus. Aliquam erat volutpat. Proin leo ligula, tempor tincidunt faucibus eu, tempor in magna. Fusce ut odio leo, in volutpat lectus. Morbi in aliquet mi. Nam vitae neque vitae felis ultricies volutpat id ut dui. Morbi bibendum aliquam hendrerit. Proin vel libero sed nisl euismod cursus. Aliquam sagittis aliquam eleifend.".lower() | |
def encrypt(text, key): | |
key_numeric = [ord(x) for x in key] | |
text_numeric = [ord(x) for x in text] | |
text_length = len(text_numeric) | |
key_length = len(key_numeric) | |
key_long = np.tile(key_numeric,(1,np.ceil(text_length/float(key_length) + 1))).squeeze() | |
key_long = key_long[0:text_length] | |
return [chr(x) for x in key_long + text_numeric] | |
def decrypt(text, key): | |
key_numeric = [ord(x) for x in key] | |
text_numeric = [ord(x) for x in text] | |
text_length = len(text_numeric) | |
key_length = len(key_numeric) | |
key_long = np.tile(key_numeric,(1,np.ceil(text_length/float(key_length) + 1))).squeeze() | |
key_long = key_long[0:text_length] | |
return [chr(x) for x in text_numeric - key_long] | |
if __name__=='__main__': | |
fig = plt.figure() | |
fftfig = plt.figure() | |
fftax = fftfig.add_subplot(211) | |
ax = fig.add_subplot(211, xlim = (0,255)) | |
cyphertext = encrypt(PLAIN_TEXT, SHARED_KEY) | |
print "".join(cyphertext) | |
text_numeric = [ord(x) for x in PLAIN_TEXT] | |
ax.hist(text_numeric, 256, normed=0, facecolor='green', alpha=0.75) | |
fftax.plot(np.abs(np.fft.fft(text_numeric))) | |
cypher = encrypt(PLAIN_TEXT, SHARED_KEY) | |
cypher_numeric = [ord(x) for x in cypher] | |
ax = fig.add_subplot(212, xlim=(0,255)) | |
fftax = fftfig.add_subplot(212) | |
ax.hist(cypher_numeric, 256, normed=0, facecolor='green', alpha=0.75) | |
fftax.plot(np.abs(np.fft.fft(cypher_numeric))) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment