Base64 Magic!

--

I was just surfing Twitter, when I found this TIL (‘Today I Learned’):

With this, we take a Base64 encoded string, and decode it to a byte array and print the answer. Next, we take this byte array and convert it back into a Base64 string and print it. And — as if by magic — the value for the Base64 string generated differs from the original. Basically, this operation should just give the same as the output, so I felt a slight panic coming on.

And so I grabbed some code, and tried it with a range of values:

When I ran the program, it showed that the decoding back into Base64 most of the time gave the wrong answer, and where a “w” or a “Q” replaced the second Base64 character:

00== d3 0w== d3
10== d7 1w== d7
20== db 2w== db
30== df 3w== df
40== e3 4w== e3
50== e7 5w== e7
60== eb 6w== eb
70== ef 7w== ef
80== f3 8w== f3
90== f7 9w== f7
a0== 6b aw== 6b
b0== 6f bw== 6f
c0== 73 cw== 73
d0== 77 dw== 77
e0== 7b ew== 7b…

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.