# How to decrypt a substitution cipher using a Base64 alphabet [duplicate]

Let us consider a cipher that works like the following:

• Plaintext is encoded to Base64.
• The characters in the encoded plaintext are substituted with a randomly shuffled character set (a-z, 0-9, A-Z, /, +).

The shuffled alphabet are the key. How do I decipher it to get the base64 encoded string without knowing the shuffled character set?

## marked as duplicate by e-sushiMar 9 '17 at 14:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• i guess you could break it into 6-bit groups and perform a frequency analysis, even better if you can see a known large plaintext, then you can crack it completely – Richie Frame Jan 20 '17 at 0:29
• Related Q/A here. Note that due to the shift you can be reasonably certain that the highest bit is set to zero for ASCII characters. Fun thing is that this will affect multiple positions in the encoding, which should make it easier to perform frequency analysis. I think an answer should definitely contain tricks that uses this information. Also, only certain characters are possible at the end of base 64 ciphertext (where the input in octets is not dividable by 3), further restricting the possible values. – Maarten Bodewes Jan 20 '17 at 9:35
• The key size is about ~88 bits (I think the key size is $26!$) so brute forcing is definitely out by the way. – Maarten Bodewes Jan 20 '17 at 9:41
• This question is missing which information can be assumed to be present to distinguish the result of decoding. As base64 can encode any binary you cannot decode without information about the plaintext. It would only possibly leak a tiny bit of information at the end of the base64 encoding (as the last character only encodes 2, 4 or 6 bits out of 8). – Maarten Bodewes Feb 2 '17 at 13:28
• Have you looked at this link? crypto.stackexchange.com/questions/20802/… – Carl Knox Feb 7 '17 at 14:35

## 1 Answer

Base64 encoding regroups three consecutive 8-bit values (0-255) into four 6-bit values (0-63). Each value is used to index of a small ASCII array which is typically sequential, such as,

var base64Map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

Base64 decoding is the reverse process. Each character's position is an indexof a matching ASCII array which results in a 6-bit value. Four of these are regrouped into three 8-bit values.

In your case, I suspect the base64Map was shuffled, rather than a separate shuffle operation of the encoded plaintext. If this is the case, then you need to match the order of the encoding array. If truly random, this would become a monumental effort. Perhaps try a Caesar Shift of the array.