Possible fix for non alphanumeric characters, needs testing.

This commit is contained in:
Fletcher Boyd 2013-09-04 20:13:45 +08:00
parent 397842f518
commit d6bdf3ab38

View file

@ -2,11 +2,10 @@
Plugin which (de)cyphers a string Plugin which (de)cyphers a string
Doesn't cypher non-alphanumeric strings yet. Doesn't cypher non-alphanumeric strings yet.
by instanceoftom by instanceoftom
All character cyphering added - TheNoodle
""" """
from util import hook from util import hook
chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ "
len_chars = len(chars)
@hook.command @hook.command
@ -20,19 +19,15 @@ def cypher(inp):
out = "" out = ""
passwd_index = 0 passwd_index = 0
for character in inp: for character in inp:
try: chr_index = ord(character)
chr_index = chars.index(character) passwd_chr_index = ord(passwd[passwd_index])
passwd_chr_index = chars.index(passwd[passwd_index])
out_chr_index = (chr_index + passwd_chr_index) % len_chars out_chr_index = (chr_index + passwd_chr_index) % 255
out_chr = chars[out_chr_index] out_chr = chr[out_chr_index]
out += out_chr out += out_chr
passwd_index = (passwd_index + 1) % len_passwd passwd_index = (passwd_index + 1) % len_passwd
except ValueError:
out += character
continue
return out return out
@ -46,11 +41,7 @@ def decypher(inp):
passwd_index = 0 passwd_index = 0
for character in inp: for character in inp:
try:
chr_index = chars.index(character)
passwd_index = (passwd_index + 1) % len_passwd passwd_index = (passwd_index + 1) % len_passwd
except ValueError:
continue
passwd_index -= 1 passwd_index -= 1
reversed_message = inp[::-1] reversed_message = inp[::-1]
@ -58,10 +49,10 @@ def decypher(inp):
out = "" out = ""
for character in reversed_message: for character in reversed_message:
try: try:
chr_index = chars.index(character) chr_index = ord(character)
passwd_chr_index = chars.index(passwd[passwd_index]) passwd_chr_index = ord(passwd[passwd_index])
out_chr_index = (chr_index - passwd_chr_index) % len_chars out_chr_index = (chr_index - passwd_chr_index) % 255
out_chr = chars[out_chr_index] out_chr = chars[out_chr_index]
out += out_chr out += out_chr