DEV Community

Cover image for Convert hex to base64
Stefan Alfbo
Stefan Alfbo

Posted on

1

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

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay