Sinisterly
[python] Simple Encoder(variants of hex and base64) - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: [python] Simple Encoder(variants of hex and base64) (/Thread-python-Simple-Encoder-variants-of-hex-and-base64)



[python] Simple Encoder(variants of hex and base64) - mls577 - 07-05-2013

The other day I stumbled upon hackbar, a very nifty little addon for firefox:https://addons.mozilla.org/en-us/firefox/addon/hackbar/

I saw the encoding tab, and thought to myself why not try to give this a try in python? so I did, and here's the result:
Code:
#!/usr/bin/env/python3.1 #encoder.py #mls577 #Shouts to haxme and #suidrewt #imports import sys import binascii import re sys_argv = [] #text list hex_list = [] #hex list def main(): #argument check if(len(sys.argv) < 2): usage() else: for word in sys.argv: sys_argv.append(word) #add text to list sys_argv.pop(0) #remove first entry from list(program name) string = " ".join(sys_argv) #convert list to one string bytes = to_bytes(string) #convert to bytes #hex hex_word = to_hex(bytes) hword_string = to_string(hex_word) #base64 base64_word = to_base64(bytes) #convert to base64 bword_string = to_string(base64_word) #convert back to usable string spaced_hex = re.findall("..", hword_string) #regex, add every two chars to a list #check to see if more than one hex character, this is for proper formatting if(len(hword_string) > 2): print("\nhex no spaces: " + hword_string) #hex no spaces print("\nhex with spaces: " + " ".join(spaced_hex)) #hex spaced print("\nhex seperated by : " + ":".join(spaced_hex)) #hex seperated by a colon print("\nhex seperated by % :" + "%" + "%".join(spaced_hex)) #hex seperated by percent sign print("\nunicode escaped: " + "\\x" + "\\x".join(spaced_hex)) #unicode escaped for a shellcode format print("\nbase64: " + bword_string) #base64 else: print("\nhex: " + hword_string) print("\nhex with % : " + "%" + hword_string) print("\nhex unicode escaped: " + "\\x" + hword_string) print("\nbase64: " + bword_string) def usage(): print("usage: ./encode.py <string> ") def to_bytes(string): return string.encode() #converts string to bytes def to_hex(bytes): return binascii.hexlify(bytes) #convert to hex def to_base64(bytes): return binascii.b2a_base64(bytes) #convert to base def to_string(string): string = string.decode() #convert back to string from bytes string = string.rstrip() #remove newline from string return string main()
I know I didn't write a decoder, but for my purposes I didn't need to, so I didn't.