Building a golfed rot13 encryptor is too easy because the letters are all the same order in the ASCII character space. Let's try a rot32 engine instead.

Your task is to build a function that takes a Base64 string as input and returns the same string but with each letter rotated 32 symbols from its original (in essence, with the first bit flipped).

The base64 encoding string to use for this problem is 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/ with a padding character of =. This is to prevent solutions that would otherwise use or import a built-in Base64 library where strings normally start with A instead of 0.

Example inputs and outputs:



> rot32("Daisy++daisy++give+me+your+answer+true/I+/+m+half+crazy++all+for+the+love+of+you")

The shortest program to do so in any language wins.

  • 4
    \$\begingroup\$ From the question, I would understand that we have to do a base-64 decode, some bit-twiddling, and a base-64 encode. But your sample answer suggests that all of the talk about base-64 is a red herring, and it's just like rot-13 except that the set of characters which don't get ignored is larger. Which is it? \$\endgroup\$ Nov 30, 2014 at 22:12
  • 1
    \$\begingroup\$ It's just like rot-13, but I was thinking bit-twiddling might be a possible strategy to make the code shorter. \$\endgroup\$
    – Joe Z.
    Nov 30, 2014 at 22:28

9 Answers 9


Bash / Unix shell, 29

tr 0-9a-zA-Z+/ w-zA-Z+/0-9a-v

Input from STDIN, output on STDOUT.


Perl, 41

Just a simple transliteration. Reads from STDIN, outputs to STDOUT:


Try it out here.

  • 2
    \$\begingroup\$ Who says Perl is not an esolang (and/or golfing oriented language)? \$\endgroup\$
    – Optimizer
    Nov 30, 2014 at 22:57
  • \$\begingroup\$ You could shave off a few characters by using the -p and -e command line switches to get rid of the $_=<>; and the ;print. \$\endgroup\$
    – Mark
    Dec 4, 2014 at 21:28

CJam, 24 bytes


Try it online.

How it works

q         " Read from STDIN.                                                              ";
"+"       " Push that string.                                                             ";
":/{a[A"  " Push that string.                                                             ";
{         " For each character in the second string:                                      ";
  ,       "   Push the string of all charcters with a lower ASCII code.                   ";
  ^       "   Take the symmetric difference of the two topmost strings on the stack.      ";
}/        " Result: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/0123456789'.   ";
_32m>     " Rotate a copy 32 characters to the right.                                     ";
er        " Perform character transliteration.                                            ";

CJam, 45 41 38 29 27 26 bytes


Reads the string to be encrypted from STDIN

How it works:

q                              "Read input";
 A,                            "Get 0 - 9 array";
   '{,                         "Get array/string of ASCII code 0 till ASCII code of z";
      97>                      "Remove first 96 characters to get a-z string";
         _eu                   "Copy a-z array and turn it to uppercase A-Z array";
            "+/"+++            "Push string +/ and concat all 4 arrays";
                   _32m>       "Copy the array and move first 32 characters to end";
                        er     "Transliterate input using the two arrays, leaving ="
                               "intact as it does not appear in the first array";

Try it online here


Python, 178

b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"

def rot32(s):
    o = ""
    for c in s:
        if c not in b:
            o += c
            o += b[b.find(c) ^ 32] 
    return o

This is an ungolfed, last-place reference implementation in Python that you can use to test your own implementation.

  • \$\begingroup\$ At least, I hope it stays last-place. \$\endgroup\$
    – Joe Z.
    Nov 30, 2014 at 22:04

GolfScript (41 40 bytes)


Online demo

There are two parts to this: the translation is a variant on the last technique mentioned in my tip on tr in GolfScript, and the other part is the string building, which uses the string 0:a{A[ as an array of char values and a fold to turn them into character ranges. Note the use of 32/(* to build the translated string by inserting the first 32 chars between the second 32 chars and the character we're translating.


python, 69

f = lambda s,b: ''.join(b[b.index(c)^32] if c in b else c for c in s)


>>> b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"

>>> print f('nb8ukoc6eu5liqhu9irudogjmuip8lunb8uf4tsu7ia=', b)

LiveScript, 91

r=[\0 to\9].concat [\a to\z] [\A to\Z] [\+ \/];f=(.replace /[^\W_]/ ->r[32.^.r.indexOf it])

LiveScript, 50

If the string is allowed as a second argument.

f=(a,b)->a.replace /[^\W_]/ ->r[32.^.b.indexOf it]

JavaScript 164

function rot32(s){for(i=0,o="";i<s.length;i++)c=s[i],j=b.indexOf(c),o+=j>-1?b[j^32]:c
return o}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.