Level 1: Convert hex to base64

Task

Convert hex to base64

The string

49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d

Should produce:

SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t

So go ahead and make that happen. You'll need to use this code for the rest of the exercises.

Cryptopals Rule


Explanation

When we work in cryptography, what really matters are the bytes, not their text representation. In cryptography, data is not text; it consists of sequences of bytes. Encodings like hex or base64 exist only to make data readable for humans, but they do not affect the actual content.

If we work with strings, we might encounter errors due to different encodings that alter the bytes, for example.

We need to think of data as bytes, not text.


Resolution

First, we are going to create a file named task1.py

Understanding the code

  • Two libraries are imported

    • binascii to manipulate hexadecimal data

    • base64 for base64 encoding and decoding functions

  • After that, three variables are created

    • hex_string , which contains the hex string provided by Cryptopals

    • bytes_data , which converts the hex string to bytes using unhexlify

    • base64_data that encodes bytes to base64

  • Finally, base64_data is decoded and printed


Result

Last updated