A summary of how Python implements each of the binary transformations

Source: Internet
Author: User
Tags decimal to hex
This article mainly summarizes the python implementation of the various conversion of the relevant data, including the string and 16 binary conversion, built-in function hex () and the exchange of related content, the need for friends can refer to the following to see together.

Objective

Play CTF often encountered in the conversion of the problem, just do a conversion summary, share out for everyone to reference the study, the following to see a detailed introduction:

String and 16 binary conversions

For example, Baidu CTF December's second first misc


666c61677b65633862326565302d336165392d346332312d613031322d3038616135666137626536377d

A simpler approach is to call the decryption of a string directly .decode('hex') , but what would you do if you didn't use the function?

One idea is to first 2 groups, to solve the ASCII value of each group, combined with the following string can be obtained, the specific code is as follows


Import res= ' 666c61677b65633862326565302d336165392d346332312d613031322d3038616135666137626536377d ' s = Re.findall ( R '. {2} ', s) s = map (lambda x:chr (int (x,16)), s) print '. Join (s) >>>flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}

The function of the string is mentioned earlier, and decode('hex') there are two functions that go to 16, which are summarized here.

Built-in function hex ()

Only 10 binary integers can be converted to 16 and cannot be translated into strings

Binascii Library of Hexlify () and B2a_hex ()

The function of these two functions is to convert the string into 16 binary, corresponding decryption function is unhexlify()a2b_hex()

In-process mutual transfer

Binary, octal, hex to 10 binary simple, direct call

int function


Int (str,base)//Returns a decimal integer, but note that at this point the first argument is a string

The corresponding decryption functions are


Bin ()//10 binary to binary Oct ()//Decimal to octal hex ()//Decimal to Hex

But the binary direct to 16 binary system needs to go one more step, first with int to decimal, in the function mentioned above hex() to convert decimal into 16 binary, the more concise way to write is


Map (Lambda x:hex (int (x,2)), [' 0011 '])//LAMBDA expression

or a


[Hex (int (x,2)) for x in [' 0011 ']]//list parsing

The corresponding decryption function is


Map (Lambda x:bin (int (x,16)), [' EF '])

Finally, I enclose a binary conversion gadget written in Python, the main function is to convert a set of binary, or ASCII, or 16 binary into a string, presumably the CTF also often encounter such questions


# Make by Jiang Sir#coding:utf-8import reimport argparse def bintostr (text): Text = Text.replace (",") Text = Re.findall (R '). { 8} ', text) s = map (lambda x:chr (int (x,2)), text) #批量二进制转十进制 flag = ". Join (s) return flag def asciitostr (text): If ' in Te Xt:text = Text.split (') elif ', ' in text:text = Text.split (', ') s = map (lambda x:chr (int (x)), text) flag = '. Join (s) r Eturn Flag def hextostr (text): Text = Re.findall (R '. { 2} ', text) #print text s = map (lambda x:chr (int (x,16)), text) #print s flag = ". Join (s) return flag  if name = = ' main ': Parser = Argparse. Argumentparser () parser.add_argument ("-B") parser.add_argument ("-A") parser.add_argument ("-X") argv = Parser.parse_ Args () #print argv if argv.b:res = Bintostr (argv.b) elif argv.a:res = Asciitostr (ARGV.A) elif argv.x:res = Hextostr (arg v.x) Print Res

Usage:

Hex-To-string:

666c61677b65633862326565302d336165392d346332312d613031322d3038616135666137626536377d


Bintostr.py-x "666c61677b65633862326565302d336165392d346332312d613031322d3038616135666137626536377d" flag{ EC8B2EE0-3AE9-4C21-A012-08AA5FA7BE67}

Binary transcoding string:

You can have spaces or no spaces

00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110 000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 0 0110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100


Bintostr.py-b "00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 0110010 0 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 011 00011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100 "/f6732410aadc037 Fb0cbaa00c7531373.txt

ASCII to String

Can be either a space-delimited or a delimited

S= ' 45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32 '

Bintostr.py-a "45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 4 6----------"-.-. - .. -. .-.. ... -... .. --.- -... ... . -..

The above examples are from some of the CTF problems

summary

Related Article

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.