This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/cypher.py

40 lines
966 B
Python
Raw Normal View History

2013-09-05 02:42:40 +02:00
import base64
2014-02-14 04:36:57 +01:00
from util import hook
2012-02-29 06:47:11 +01:00
2013-09-05 02:42:40 +02:00
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)
return base64.urlsafe_b64encode("".join(enc))
2013-11-12 07:06:06 +01:00
2013-09-05 02:42:40 +02:00
def decode(key, enc):
dec = []
2013-11-12 07:06:06 +01:00
enc = base64.urlsafe_b64decode(enc.encode('ascii', 'ignore'))
2013-09-05 02:42:40 +02:00
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):
2013-09-04 12:30:04 +02:00
"""cypher <pass> <string> -- Cyphers <string> with <password>."""
2012-02-29 06:47:11 +01:00
passwd = inp.split(" ")[0]
inp = " ".join(inp.split(" ")[1:])
2013-11-12 07:06:06 +01:00
return encode(passwd, inp)
@hook.command
def decypher(inp):
2013-09-04 12:30:04 +02:00
"""decypher <pass> <string> -- Decyphers <string> with <password>."""
2012-02-29 06:47:11 +01:00
passwd = inp.split(" ")[0]
inp = " ".join(inp.split(" ")[1:])
2013-11-12 07:06:06 +01:00
return decode(passwd, inp)