Skip to content

Instantly share code, notes, and snippets.

@brendan-w
Last active March 14, 2016 19:28
Show Gist options
  • Save brendan-w/49088a9735ade124fa0f to your computer and use it in GitHub Desktop.
Save brendan-w/49088a9735ade124fa0f to your computer and use it in GitHub Desktop.
Intentionally f****d up cryptography
#!/usr/bin/env python2
########################################################################
# #
# fcrypt: Intentionally f****d up cryptography #
# #
# Copyright 2016 Brendan Whitfield (me@brendan-w.com) #
# #
########################################################################
# #
# fcrypt.py #
# #
# This file is part of fcrypt #
# #
# fcrypt is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 2 of the License, or #
# (at your option) any later version. #
# #
# fcrypt is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with fcrypt. If not, see <http://www.gnu.org/licenses/>. #
# #
########################################################################
import os
import sys
import json
from base64 import b64encode as b64
key = "lol"
def make_random(n_chars):
output = ""
while len(output) < n_chars:
random_bytes = os.urandom(64)
output += b64(random_bytes).decode("utf-8")
return output[:n_chars]
def ensure_keyspace_file_exists(key):
# make the keyspace file if it doesn't already exist
if not os.path.isfile("%s.json" % key):
with open("%s.json" % key, "w") as f:
f.write("{}") # empty JSON file
def read_keyspace_file(key):
with open("%s.json" % key, "r") as f:
data = json.loads(f.read())
return data
def write_keyspace_file(key, data):
with open("%s.json" % key, "w") as f:
f.write(json.dumps(data))
def encrypt(msg):
ensure_keyspace_file_exists(key)
data = read_keyspace_file(key)
# check to see if this message was already encrypted
# (so we can consistently return the same ciphertext)
reverse_data = dict((v,k) for k,v in data.iteritems())
if msg in reverse_data:
# lookup the existing random "ciphertext"
cipher_text = reverse_data[msg]
else:
# generate new ciphertext
cipher_text = make_random(len(msg))
data[cipher_text] = msg
# write the updated keyspace back to our file
write_keyspace_file(key, data)
return cipher_text
def decrypt(cipher_text):
ensure_keyspace_file_exists(key)
data = read_keyspace_file(key)
# test if this ciphertext corresponds to some known plaintext
if cipher_text in data:
msg = data[cipher_text]
else:
# else, the user is trying to decrypt some bogus data
# throw some more random at them (and record it for consistency)
msg = make_random(len(cipher_text))
data[cipher_text] = msg
# write the updated keyspace back to our file
write_keyspace_file(key, data)
return msg
def main():
global key
running = True
while running:
print(">>> "),
line = sys.stdin.readline()[:-1]
if line.startswith("setkey "):
line = line[7:]
if len(line) == 0:
sys.stdout.write("Please enter a new key\n")
else:
key = line
sys.stdout.write("New key defined\n")
elif line == "quit":
running = False
elif line == "exit":
running = False
elif line.startswith("e "):
line = line[2:]
if len(line) == 0:
sys.stdout.write("Please enter a message to encrypt\n")
else:
sys.stdout.write(encrypt(line) + "\n")
elif line.startswith("d "):
line = line[2:]
if len(line) == 0:
sys.stdout.write("please enter a message to decrypt\n")
else:
sys.stdout.write(decrypt(line) + "\n")
else:
sys.stdout.write("'%s' is not a valid fcrypt command\n" % line)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment