From 8c60a923baf9e8433722c7fc6c057f12d62a6be5 Mon Sep 17 00:00:00 2001 From: Fletcher Boyd Date: Thu, 5 Sep 2013 08:42:40 +0800 Subject: [PATCH] Rewrite --- plugins/cypher.py | 75 ++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 49 deletions(-) diff --git a/plugins/cypher.py b/plugins/cypher.py index 1c4d7d6..1faad4c 100755 --- a/plugins/cypher.py +++ b/plugins/cypher.py @@ -1,65 +1,42 @@ -""" -Plugin which (de)cyphers a string -Doesn't cypher non-alphanumeric strings yet. -by instanceoftom -All character cyphering added - TheNoodle -""" - +import base64 from util import hook +def encode(key, clear): + enc = [] + for i in range(len(clear)): + key_c = key[i % len(key)] + enc_c = chr((ord(clear[i]) + ord(key_c)) % 256) + enc.append(enc_c) + print "[debug]" + return base64.urlsafe_b64encode("".join(enc)) + +def decode(key, enc): + dec = [] + print " [debug] " + print "key: "+key + print "string: "+enc + enc = base64.urlsafe_b64decode(enc.encode('ascii','ignore')) + print "de64: "+enc + for i in range(len(enc)): + key_c = key[i % len(key)] + dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256) + dec.append(dec_c) + return "".join(dec) + + @hook.command def cypher(inp): """cypher -- Cyphers with .""" passwd = inp.split(" ")[0] - len_passwd = len(passwd) inp = " ".join(inp.split(" ")[1:]) - - out = "" - passwd_index = 0 - for character in inp: - chr_index = ord(character) - passwd_chr_index = ord(passwd[passwd_index]) - - out_chr_index = (chr_index + passwd_chr_index) % 255 - out_chr = chr[out_chr_index] - - out += out_chr - - passwd_index = (passwd_index + 1) % len_passwd - return out + return encode(passwd,inp) @hook.command def decypher(inp): """decypher -- Decyphers with .""" - passwd = inp.split(" ")[0] - len_passwd = len(passwd) inp = " ".join(inp.split(" ")[1:]) - - passwd_index = 0 - for character in inp: - passwd_index = (passwd_index + 1) % len_passwd - - passwd_index -= 1 - reversed_message = inp[::-1] - - out = "" - for character in reversed_message: - try: - chr_index = ord(character) - passwd_chr_index = ord(passwd[passwd_index]) - - out_chr_index = (chr_index - passwd_chr_index) % 255 - out_chr = chars[out_chr_index] - - out += out_chr - - passwd_index = (passwd_index - 1) % len_passwd - except ValueError: - out += character - continue - - return out[::-1] + return decode(passwd,inp)