A summary of implementation of various hexadecimal conversions in python, and a summary of python

Source: Internet
Author: User
Tags binary to decimal

A summary of implementation of various hexadecimal conversions in python, and a summary of python

Preface

Ctf often encounters the problem of hexadecimal conversion. we just make a summary of hexadecimal conversion and share it for your reference. Let's take a look at the details below:

String and hexadecimal conversion

For example, Baidu ctf's first misc in the second game in December

666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D

One simple method is to directly call the string.decode('hex')You can decrypt it, but how can you solve it without using this function?

One idea is to first group two groups, calculate the ascii value of each group, and combine the strings to obtain the Code 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}

As mentioned abovedecode('hex')Function. There are two other hexadecimal functions.

Built-in function hex ()

Only hexadecimal integers can be converted into hexadecimal integers. Strings cannot be converted.

Binascii library's hexlify () and b2a_hex ()

The functions of these two functions are to convert strings into hexadecimal notation, and the corresponding decryption functions areunhexlify()Anda2b_hex()

Conversion

Binary, octal, and hexadecimal to hexadecimal are relatively simple and can be called directly.

Int Function

Int (str, base) // returns a decimal integer. Note that the first parameter is a string.

The corresponding decryption functions are:

Bin () // 10 to binary oct () // convert decimal to octal hex () // convert decimal to hexadecimal

However, to convert binary data directly to hexadecimal data, you need to take one more step. First, use int to convert to decimal.hex()The function converts decimal to hexadecimal. The simplified syntax is:

Map (lambda x: hex (int (x, 2), ['20140901']) // lambda expression

Or

[Hex (int (x, 2) for x in ['000000'] // list resolution

The corresponding decryption function is

map(lambda x:bin(int(x,16)),['ef'])

Finally, attach a Binary Conversion Tool written in python. The main function is to convert a group of binary data, ascii data, or hexadecimal data to a string, certainly, this type of question type is also frequently encountered in ctf.

# Make by Jiang sir # coding: The 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) # batch binary to decimal flag = ''. join (s) return flag def asciitostr (text): if ''in text: text = text. split ('') elif ', 'in text: text = text. split (',') s = map (lambda x: chr (int (x), text) flag = ''. join (s) return 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 (argv. x) print res

Usage:

Hexadecimal string:

Bytes

bintostr.py -x "666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D"flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}

Binary to string:

There can be spaces or spaces

00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100

bintostr.py -b "00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100"/f6732410aadc037fb0cbaa00c7531373.txt

Ascii to string

It can be separated by spaces, or yes

S = '45 46 45 46 32 45 46 46 46 46 46 32 46 46 46 46 46 46 46 32 45 46 46 46 32 46 46 46 46 45 45 45 32 45 46 46 46 32 46 46 46 32 46 46 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 46 46 32 46 46 46 32 46 45 46 46 32"-.-. - ..-. .-.. ... -... ..--.- -... ... .-..

The above examples all come from some ctf competition questions

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.