在平时我们会遇到各种各样的编码,在这里,我总结了一些常见的编码,并不是很全

尝试着做了个编码解码的汇总,并且写了个脚本出来,由于python功底不是很强,所以可能会有不到之处,还望各位多多指正

附上脚本:

#-*-coding:utf-8-*-
#author:hell0_w
#本人博客:http://hell0w.cnblogs.com/

import base64
import bubblepy
import urllib
import quopri
import cgi
import HTMLParser

#加密函数:
def base16_encode(content):
    return base64.b16encode(content)
def base32_encode(content):
    return base64.b32encode(content)
def base64_encode(content):
    return base64.b64encode(content)
def BubbleBabble_encode(content):
    return bubblepy.BubbleBabble().encode(content)
def url_encode(content):
    return urllib.quote(content)
def dec_binary(content):
    list = []
    for i in content.split(\' \'):
        list.append(bin(int(i)).replace(\'0b\',\'\'))
    return list
def str_binary(content):
    list = []
    for i in content:
        list.append(ord(i))
    list1 = []
    for j in list:
        list1.append(bin(int(j)).replace(\'0b\',\'\'))
    return list1
def quoted_printable_encode(content):
    return quopri.encodestring(content)
def HtmlParser_encode(content):
    return cgi.escape(content)


#解密函数:
def base16_decode(content):
    return base64.b16decode(content)
def base32_decode(content):
    return base64.b32decode(content)
def base64_decode(content):
    return base64.b64decode(content)
def BubbleBabble_decode(content):
    return bubblepy.BubbleBabble().decode(content)
def url_decode(content):
    return urllib.unquote(content)
def binary_dec(content):
    list = []
    for i in content.split(\' \'):
        list.append(int(i,2))
    return list
def binary_str(content):
    list = []
    for i in content.split(\' \'):
        list.append(int(i,2))
    list1 =[]
    for j in list:
        list1.append(chr(j))
    return \'\'.join(list1)
def quoted_printable_decode(content):
    return quopri.decodestring(content)
def HtmlParser_decode(content):
    return HTMLParser.HTMLParser().unescape(content)


def encode():
    print "[1]:base16编码"
    print "[2]:base32编码"
    print "[3]:base64编码"
    print "[4]:BubbleBabble编码"
    print "[5]:url编码"
    print "[6]:十进制转二进制"
    print "[7]:字符串转二进制"
    print "[8]:quoted-printable编码"
    print "[9]:HTML实体编码"
    operation = input("请选择:")
    strs = raw_input("请输入需要加密的字符串:")
    if operation == 1:
        try:
            print "[+]加密的结果为:%s " % base16_encode(strs)
        except Exception,e:
            print e

    elif operation == 2:
        try:
            print "[+]加密的结果为:%s " % base32_encode(strs)
        except Exception,e:
            print e

    elif operation == 3:
        try:
            print "[+]加密的结果为:%s " % base64_encode(strs)
        except Exception,e:
            print e

    elif operation == 4:
        try:
            print "[+]加密的结果为:%s " % BubbleBabble_encode(strs)
        except Exception,e:
            print e
    
    elif operation == 5:
        try:
            print "[+]加密的结果为:%s " % url_encode(strs)
        except Exception,e:
            print e
        
    elif operation == 6:
        try:
            print "[+]加密的结果为:%s " % dec_binary(strs)
        except Exception,e:
            print e

    elif operation == 7:
        try:
            print "[+]加密的结果为:%s " % str_binary(strs)
        except Exception,e:
            print e
    
    elif operation == 8:
        try:
            print "[+]加密的结果为:%s " % quoted_printable_encode(strs)
        except Exception,e:
            print e
    
    elif operation == 9:
        try:
            print "[+]加密的结果为:%s " % HtmlParser_encode(strs)
        except Exception,e:
            print e
    else:
        print "error!"
        encode()
    

def decode():
    print "[1]:base16解码"
    print "[2]:base32解码"
    print "[3]:base64解码"
    print "[4]:BubbleBabble解码"
    print "[5]:url解码"
    print "[6]:二进制转十进制"
    print "[7]:二进制转字符串"
    print "[8]:quoted-printable解码"
    print "[9]:HTML实体解码"
    operation = input("请选择:")
    strs = raw_input("请输入需要解密的字符串:")
    if operation == 1:
        try:
            print "[+]解密的结果为:%s " % base16_decode(strs)
        except Exception,e:
            print "Error!Not base16 encoding!"

    elif operation == 2:
        try:
            print "[+]解密的结果为:%s " % base32_decode(strs)
        except Exception,e:
            print "Error!Not base32 encoding!"

    elif operation == 3:
        try:
            print "[+]解密的结果为:%s " % base64_decode(strs)
        except Exception,e:
            print "Error!Not base64 encoding!"

    elif operation == 4:
        try:
            print "[+]解密的结果为:%s " % BubbleBabble_decode(strs)
        except Exception,e:
            print "Error!Not BubbleBabble encoding!"
    
    elif operation == 5:
        try:
            print "[+]解密的结果为:%s " % url_decode(strs)
        except Exception,e:
            print "Error!Not url encoding!"
        
    elif operation == 6:
        try:
            print "[+]解密的结果为:%s " % binary_dec(strs)
        except Exception,e:
            print "Error!Not binary encoding!"

    elif operation == 7:
        try:
            print "[+]解密的结果为:%s " % binary_str(strs)
        except Exception,e:
            print "Error!Not binary encoding!"
    
    elif operation == 8:
        try:
            print "[+]解密的结果为:%s " % quoted_printable_decode(strs)
        except Exception,e:
            print "Error!Not quoted-printable encoding!"
    
    elif operation == 9:
        try:
            print "[+]解密的结果为:%s " % HtmlParser_decode(strs)
        except Exception,e:
            print "Error!Not HtmlParser encoding!"
    else:
        print "error!"
        decode()


def main():
    print "[1]:加密"
    print "[2]:解密"
    operation = input("请选择:")
    if operation == 1:
        encode()
    elif operation == 2:
        decode()
    else:
        print "error!"
        main()
    
if __name__ == \'__main__\':
    main()

  

运行示例:

加密:

解密:

 本文固定连接:http://www.cnblogs.com/hell0w/p/7512211.html  转载请注明出处,谢谢!

版权声明:本文为linuxsec原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/linuxsec/articles/7528252.html