常见编码解码脚本

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

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

附上脚本:

  1 #-*-coding:utf-8-*-
  2 #author:hell0_w
  3 #本人博客:http://hell0w.cnblogs.com/
  4 
  5 import base64
  6 import bubblepy
  7 import urllib
  8 import quopri
  9 import cgi
 10 import HTMLParser
 11 
 12 #加密函数:
 13 def base16_encode(content):
 14     return base64.b16encode(content)
 15 def base32_encode(content):
 16     return base64.b32encode(content)
 17 def base64_encode(content):
 18     return base64.b64encode(content)
 19 def BubbleBabble_encode(content):
 20     return bubblepy.BubbleBabble().encode(content)
 21 def url_encode(content):
 22     return urllib.quote(content)
 23 def dec_binary(content):
 24     list = []
 25     for i in content.split(' '):
 26         list.append(bin(int(i)).replace('0b',''))
 27     return list
 28 def str_binary(content):
 29     list = []
 30     for i in content:
 31         list.append(ord(i))
 32     list1 = []
 33     for j in list:
 34         list1.append(bin(int(j)).replace('0b',''))
 35     return list1
 36 def quoted_printable_encode(content):
 37     return quopri.encodestring(content)
 38 def HtmlParser_encode(content):
 39     return cgi.escape(content)
 40 
 41 
 42 #解密函数:
 43 def base16_decode(content):
 44     return base64.b16decode(content)
 45 def base32_decode(content):
 46     return base64.b32decode(content)
 47 def base64_decode(content):
 48     return base64.b64decode(content)
 49 def BubbleBabble_decode(content):
 50     return bubblepy.BubbleBabble().decode(content)
 51 def url_decode(content):
 52     return urllib.unquote(content)
 53 def binary_dec(content):
 54     list = []
 55     for i in content.split(' '):
 56         list.append(int(i,2))
 57     return list
 58 def binary_str(content):
 59     list = []
 60     for i in content.split(' '):
 61         list.append(int(i,2))
 62     list1 =[]
 63     for j in list:
 64         list1.append(chr(j))
 65     return ''.join(list1)
 66 def quoted_printable_decode(content):
 67     return quopri.decodestring(content)
 68 def HtmlParser_decode(content):
 69     return HTMLParser.HTMLParser().unescape(content)
 70 
 71 
 72 def encode():
 73     print "[1]:base16编码"
 74     print "[2]:base32编码"
 75     print "[3]:base64编码"
 76     print "[4]:BubbleBabble编码"
 77     print "[5]:url编码"
 78     print "[6]:十进制转二进制"
 79     print "[7]:字符串转二进制"
 80     print "[8]:quoted-printable编码"
 81     print "[9]:HTML实体编码"
 82     operation = input("请选择:")
 83     strs = raw_input("请输入需要加密的字符串:")
 84     if operation == 1:
 85         try:
 86             print "[+]加密的结果为:%s " % base16_encode(strs)
 87         except Exception,e:
 88             print e
 89 
 90     elif operation == 2:
 91         try:
 92             print "[+]加密的结果为:%s " % base32_encode(strs)
 93         except Exception,e:
 94             print e
 95 
 96     elif operation == 3:
 97         try:
 98             print "[+]加密的结果为:%s " % base64_encode(strs)
 99         except Exception,e:
100             print e
101 
102     elif operation == 4:
103         try:
104             print "[+]加密的结果为:%s " % BubbleBabble_encode(strs)
105         except Exception,e:
106             print e
107     
108     elif operation == 5:
109         try:
110             print "[+]加密的结果为:%s " % url_encode(strs)
111         except Exception,e:
112             print e
113         
114     elif operation == 6:
115         try:
116             print "[+]加密的结果为:%s " % dec_binary(strs)
117         except Exception,e:
118             print e
119 
120     elif operation == 7:
121         try:
122             print "[+]加密的结果为:%s " % str_binary(strs)
123         except Exception,e:
124             print e
125     
126     elif operation == 8:
127         try:
128             print "[+]加密的结果为:%s " % quoted_printable_encode(strs)
129         except Exception,e:
130             print e
131     
132     elif operation == 9:
133         try:
134             print "[+]加密的结果为:%s " % HtmlParser_encode(strs)
135         except Exception,e:
136             print e
137     else:
138         print "error!"
139         encode()
140     
141 
142 def decode():
143     print "[1]:base16解码"
144     print "[2]:base32解码"
145     print "[3]:base64解码"
146     print "[4]:BubbleBabble解码"
147     print "[5]:url解码"
148     print "[6]:二进制转十进制"
149     print "[7]:二进制转字符串"
150     print "[8]:quoted-printable解码"
151     print "[9]:HTML实体解码"
152     operation = input("请选择:")
153     strs = raw_input("请输入需要解密的字符串:")
154     if operation == 1:
155         try:
156             print "[+]解密的结果为:%s " % base16_decode(strs)
157         except Exception,e:
158             print "Error!Not base16 encoding!"
159 
160     elif operation == 2:
161         try:
162             print "[+]解密的结果为:%s " % base32_decode(strs)
163         except Exception,e:
164             print "Error!Not base32 encoding!"
165 
166     elif operation == 3:
167         try:
168             print "[+]解密的结果为:%s " % base64_decode(strs)
169         except Exception,e:
170             print "Error!Not base64 encoding!"
171 
172     elif operation == 4:
173         try:
174             print "[+]解密的结果为:%s " % BubbleBabble_decode(strs)
175         except Exception,e:
176             print "Error!Not BubbleBabble encoding!"
177     
178     elif operation == 5:
179         try:
180             print "[+]解密的结果为:%s " % url_decode(strs)
181         except Exception,e:
182             print "Error!Not url encoding!"
183         
184     elif operation == 6:
185         try:
186             print "[+]解密的结果为:%s " % binary_dec(strs)
187         except Exception,e:
188             print "Error!Not binary encoding!"
189 
190     elif operation == 7:
191         try:
192             print "[+]解密的结果为:%s " % binary_str(strs)
193         except Exception,e:
194             print "Error!Not binary encoding!"
195     
196     elif operation == 8:
197         try:
198             print "[+]解密的结果为:%s " % quoted_printable_decode(strs)
199         except Exception,e:
200             print "Error!Not quoted-printable encoding!"
201     
202     elif operation == 9:
203         try:
204             print "[+]解密的结果为:%s " % HtmlParser_decode(strs)
205         except Exception,e:
206             print "Error!Not HtmlParser encoding!"
207     else:
208         print "error!"
209         decode()
210 
211 
212 def main():
213     print "[1]:加密"
214     print "[2]:解密"
215     operation = input("请选择:")
216     if operation == 1:
217         encode()
218     elif operation == 2:
219         decode()
220     else:
221         print "error!"
222         main()
223     
224 if __name__ == '__main__':
225     main()

运行示例:

加密:

解密:

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

posted @ 2017-09-12 21:03  hell0_w  阅读(1833)  评论(0编辑  收藏  举报