Cryptopals WriteUp - Set 1 - Challenge 1 - Converting hex to base64

by Brian Ritchie

Last Updated: Aug 8, 2017 13:02
664 words · 4 minutes read Cryptography | Technical Writeups |

Objective: To provide the reader with a detailed writeup on my solution for the Cryptopals Set 1 Challenge 1 as specified in the following link -

NOTE : The solution outlined below is not necessarily an optimized version and does not claim to be. The writeup merely serves to present the method that was undertaken to achieve the required outcome and thus forms the basis of the foundational solution.

The code for this solution can be found on my Github.

This writeup is also available in PDF form: Click to View

Table of contents

№01. Challenge Requirement
№02. Solution WriteUp

Challenge Requirement

Challenge 1 of Set 1 requires us to take a specified hex string and convert it to base64.

The challenge however does specify that all operations should be done on raw bytes and not on encoded strings.


Solution WriteUp

This section will aim to outline how we achieved the aforementioned requirements and the necessary logic and tools used to do so.

Solution Workflow

Given the challenge requirements, we can broadly assume that the solution workflow will be as follows:

The image visualizes the solution workflow that is explained below in three steps

In the workflow above, the solution should:

  • Accept the provided Hex encoded string (and verify that it is encoded in hexadecimal)
  • Decode the hex string to a raw unencoded byte form, and
  • Subsequently, encode the raw byte string to a base64 encoded string


For the sake of convenience, the solution will output to screen the result of each step in the workflow.


Code Walkthrough

Language Selected: GoLang

Packages Used:

  • encoding/hex
  • encoding/base64
  • fmt



Given that there is only a singular hex string to be decoded, we create a variable and assign the provided hex string value to the variable as follows:

hex_string := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"


We then proceed to lay the foundation for decoding the variable hex_string to an unencoded form. We do this through the code segment below:

byte_string, err := hex.DecodeString(hex_string)
    if err != nil {
    } else {
        fmt.Println("The ASCII byte conversion is --> ", string(byte_string))


In the code segment above, a new variable byte_string is created and is assigned the value of the decoded hex_string variable using the hex.DecodeString function.

In addition, it also includes an error handling (which was included in these circumstance to ensure the solution worked as intended) which can be omitted if not required.

Upon decoding, the code above outputs the results to screen using the Println function within the fmt package by formatting the unencoded value within the byte_string into a string.

Running the code up to this point (and assuming no errors) should render the following results:

$  go run solution.go
"The ASCII byte conversion is -->  I'm killing your brain like a poisonous mushroom"


Given that the output above looks correct thus far (read: It actually forms a readable sentence), we can assume that we can proceed to the next step, which is to re-encode it to base64 (or radix-64 representation).

We achieve it through the following line of code (which is explained below):

base64_encode := base64.StdEncoding.EncodeToString([]byte(byte_string))


The line of code above does the following:

  • Declares an empty slice of type byte called base64_encode
  • Calls the base64 package and employs the StdEncoding variable and the EncodeToString function to encode the value raw value held by the byte_string variable
  • Stores the result of the encoding in the base64_encode slice


The solution then outputs the value of the base64_encode slice to screen for verification, and this is achieved again through the Println function within the fmt package using the line of code below:

fmt.Println("The Base64 encoding of the above is --> ", base64_encode)


Running the code now (and assuming no errors) should render the following results:

$  go run solution.go
"The ASCII byte conversion is -->  I'm killing your brain like a poisonous mushroom"
"The Base64 encoding of the above is -->  SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"


Upon inspection, the value on screen does match the required outcome, thus confirming that the solution works.