Ori
Ori

Reputation: 31

How can I decode a base64 string using a custom letter set?

I am aware of the question here that tells me how to encode using a method provided in the answers.

But Idk how to then decode what was encoded after running the file to encode some text.

This is my code to encrypt.

import base64

print("What would you like to encode? ")
data = input()

std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/="
custom = "abzdefghkjolsniprqmtuvwyxcDBCAEGFHJKILMNOPTRSQUVXWZY0629851743-&"

x = base64.b64encode(data.encode())
print('Here is your code!: ' + str(x)[2:-1].translate(str(x)[2:-1].maketrans(std_base64chars, custom)))

How would I go about decoding the output/encrypted text of the above lines of code?.

Output Example.

What would you like to encode? 
Hello World!
Here is your code!: mgvSBg4Fv23ZBgrH  <-- How do I decode that?

Upvotes: 0

Views: 2531

Answers (1)

Brody
Brody

Reputation: 46

You just need to reverse your steps.

The transform is swapping your character sets, so swap them back.
Now you have a base64 encoded string which can be decoded into something human readable.

data = input()
x = str(data).translate(str(data).maketrans(custom, std_base64chars))
print(base64.b64decode(x).decode())

Upvotes: 2

Related Questions