Common encoding and decoding scripts

Source: Internet
Author: User

Common encoding and decoding scripts

We usually encounter various encodings. Here, I have summarized some common encodings, which are not very complete.

I tried to make a summary of encoding and decoding, and wrote a script. As python is not very good, it may not be possible. I hope you can correct me a lot.

Attached script:

1 #-*-coding: UTF-8-*-2 # author: hell0_w 3 # my blog: 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 # encryption function: 13 def base16_encode (content): 14 basereturn 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 Bub BleBabble_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.appe Nd (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 # decryption function: 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.b64decod E (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 encoding" 74 print "[2]: base32 encoding" 75 print "[3]: base64 encoding "76 print" [4]: BubbleBabble encoding "77 print" [5]: url encoding "78 print" [6]: Convert decimal to binary "79 print" [7]: String to binary "80 print" [8]: quoted-printable encoding "81 print" [9]: HTML Entity encoding "82 operation = input (" select :") 83 strs = raw_input ("Enter the string to be encrypted:") 84 if operation = 1: 85 try: 86 print "[+] The encrypted result is: % s "% base16_encode (strs) 87 failed t Exception, e: 88 print e 89 90 elif operation = 2: 91 try: 92 print" [+] The encrypted result is: % s "% base32_encode (strs) 93 failed t Exception, e: 94 print e 95 96 elif operation = 3: 97 try: 98 pr Int "[+] encrypted result: % s" % base64_encode (strs) 99 failed t Exception, e: 100 print e101 102 elif operation = try: 104 print "[+] The encrypted result is: % s" % BubbleBabble_encode (strs) 105 failed t Exception, e: 106 print e107 108 elif operation = try: 110 print "[+] The encrypted result is: % s" % url_encode (strs) 111 failed t Exception, e: 112 print e113 114 elif operation = try: 116 print "[+] encrypted result: % s" % dec_binary (strs) 117 exce Pt Exception, e: 118 print e119 120 elif operation = try: 122 print "[+] The encrypted result is: % s" % str_binary (strs) 123 precondition t Exception, e: 124 print e125 126 elif operation = try: 128 print "[+] The encrypted result is: % s" % quoted_printable_encode (strs) 129 failed t Exception, e: 130 print e131 132 elif operation = try: 134 print "[+] The encrypted result is: % s" % HtmlParser_encode (strs) 135 failed t Exception, e: 136 print ese else: 138 Print "error! "139 encode () 140 141 142 def decode (): 143 print" [1]: base16 decoding "144 print" [2]: base32 decoding "145 print" [3]: base64 decoding "146 print" [4]: BubbleBabble decoding "147 print" [5]: url Decoding "148 print" [6]: binary to decimal "149 print" [7]: Binary to string "150 print" [8]: quoted-printable decoding "151 print" [9]: HTML Entity decoding "152 operation = input (" select: ") 153 strs = raw_input (" Enter the string to be decrypted: ") 154 if operation = try: 156 print "[+] decryption result: % s" % base16_decode (Strs) 157 failed t Exception, e: 158 print "Error! Not base16 encoding! "159 160 elif operation = 2: 161 try: 162 print" [+] The decryption result is: % s "% base32_decode (strs) 163 failed t Exception, e: 164 print "Error! Not base32 encoding! "165 166 elif operation = 3: 167 try: 168 print" [+] The decryption result is: % s "% base64_decode (strs) 169 failed t Exception, e: 170 print "Error! Not base64 encoding! "171 172 elif operation = 4: 173 try: 174 print" [+] The decryption result is: % s "% BubbleBabble_decode (strs) 175 failed t Exception, e: 176 print "Error! Not BubbleBabble encoding! "177 178 elif operation = 5: 179 try: 180 print" [+] The decryption result is: % s "% url_decode (strs) 181 failed t Exception, e: 182 print "Error! Not url encoding! "183 184 elif operation = 6: 185 try: 186 print" [+] The decryption result is: % s "% binary_dec (strs) 187 failed t Exception, e: 188 print "Error! Not binary encoding! "189 190 elif operation = 7: 191 try: 192 print" [+] The decryption result is: % s "% binary_str (strs) 193 failed t Exception, e: 194 print "Error! Not binary encoding! "195 196 elif operation = 8: 197 try: 198 print" [+] The decryption result is: % s "% quoted_printable_decode (strs) 199 failed t Exception, e: 200 print "Error! Not quoted-printable encoding! "201 202 elif operation = try: 204 print" [+] The decryption result is: % s "% HtmlParser_decode (strs) 205 failed t Exception, e: 206 print" Error! Not HtmlParser encoding! "207 else: 208 print" error! "209 decode () 210 211 212 def main (): 213 print" [1]: Encrypted "214 print" [2]: decrypted "215 operation = input (" select: ") 216 if operation = encode () 218 elif operation = decode () 220 else: 221 print" error! "222 main () 223 224 if _ name _ = '_ main _': 225 main ()

Running example:

Encryption:

Decryption:

This article fixed connection: http://www.cnblogs.com/hell0w/p/7512211.html reprint please indicate the source, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.