Cryptopals WriteUp - Set 1 Challenge 1 - Converting hex to base64
·672 words·4 mins·
Author
Brian Ritchie
Table of Contents
Table of Contents
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 - https://cryptopals.com/sets/1/challenges/1
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
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:
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:
3
4
5
6
7
8
byte_string,err:=hex.DecodeString(hex_string)iferr!=nil{panic(err)}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:
10
11
$ 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):
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:
15
fmt.Println("The Base64 encoding of the above is --> ",base64_encode)
Running the code now (and assuming no errors) should render the following results:
17
18
19
$ 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.