Overclock.net - An Overclocking Community - View Single Post - Python Base64 encoding with a non-standard alphabet

View Single Post
post #2 of (permalink) Old 03-19-2014, 10:41 PM
jvolkman
New to Overclock.net
 
jvolkman's Avatar
 
Join Date: Nov 2009
Location: Seattle
Posts: 115
Rep: 32 (Unique: 22)
Quote:
Originally Posted by Terrere View Post

One of my classes requires that I write a command and control server for a piece of malware that accepts commands that are Base64 encoded with a non-standard alphabet. It's not anything special really.

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
This is the standard Base64 alphabet.

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/
This is the non-standard alphabet.

I know that Python has a built-in library that handles Base64 conversion, but I can't seem to make it use the non-standard library. Any ideas?

UPDATE: Would replacing the characters after the Base64 coversion is run work? In theory, each character represents a specific numerical value 0-63. If I were to replace the character with the corresponding character, would that work?

Yeahh... that idea won't work...

Your idea of replacing the characters should work. Use string.maketrans to create a translation table, and then use str.translate to use it.
Code:
import string
import base64

STANDARD_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
CUSTOM_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'
ENCODE_TRANS = string.maketrans(STANDARD_ALPHABET, CUSTOM_ALPHABET)
DECODE_TRANS = string.maketrans(CUSTOM_ALPHABET, STANDARD_ALPHABET)

def encode(input):
  return base64.b64encode(input).translate(ENCODE_TRANS)

def decode(input):
  return base64.b64decode(input.translate(DECODE_TRANS))

.
jvolkman is offline