Convert hex to base64

stefanalfbo

Stefan Alfbo

Posted on April 28, 2024

Convert hex to base64

This is the first crypto challenge in set 1 from cryptopals, which is the qualifying set.

The difficulty level is relatively easy, to solve this problem I have used the programming language Go.

Problem description

Convert a string with hex values to a base64 string. The rules are:

Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.

The input is:

49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d

which should give the following output

SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t

Solution

The go language has great support for this kind of problems through their standard libraries in encoding package.

There are two of interest in this case.

import (
    "encoding/base64"
    "encoding/hex"
)
Enter fullscreen mode Exit fullscreen mode

The names of these packages are pretty much self-explanatory, base64 implements base64 encoding and hex implements hexadecimal encoding and decoding.

First step is to convert the hexadecimal string to a slice of bytes. The hex package has a perfect function for this, DecodeString, which takes a hexadecimal string.

    bytes, err := hex.DecodeString(theString)
    if err != nil {
        fmt.Println("Error decoding hexadecimal string:", err)
        return
    }
Enter fullscreen mode Exit fullscreen mode

The last step of the problem is to encode this slice of bytes to base64. Naturally we will look after a good function for this job in the base64 package. There are two candidates for this, EncodeToString and Encode. We will go with EncodeToString, since we want to print a string to see if we got the correct result.

str := base64.StdEncoding.EncodeToString(bytes)
Enter fullscreen mode Exit fullscreen mode

Now we have all the pieces to solve the problem, and the program could look like this.

package main

import (
    "encoding/base64"
    "encoding/hex"
    "fmt"
)

func convertToBase64(hexString string) (string, error) {
    bytes, err := hex.DecodeString(hexString)
    if err != nil {
        return "", err
    }

    return base64.StdEncoding.EncodeToString(bytes), nil
}

func main() {
    theString := "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" // Replace with your hex string

    str, err := convertToBase64(theString)
    if err != nil {
        fmt.Println("Error decoding hexadecimal string:", err)
        return
    }

    fmt.Println("Base64:", str)
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The problem is easy to solve when having access to Go's standard packages and its great documentation.

References

💖 💪 🙅 🚩
stefanalfbo
Stefan Alfbo

Posted on April 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Convert hex to base64
100daystooffload Convert hex to base64

April 28, 2024