10
\$\begingroup\$

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("THE+QUICK+BROWN+FOX+JUMPS+OVER+THE+LAZY+DOG=")
nb8ukoc6eu5liqhu9irudogjmuip8lunb8uf4tsu7ia=

> rot32("NB8UKOC6EU5LIQHU9IRUDOGJMUIP8LUNB8UF4TSU7IA=")
h5Eoei6C8oBfckboFclo7iadgocjEfoh5Eo9AnmoDc4=

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

The shortest program to do so in any language wins.

\$\endgroup\$
2
  • 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

15
\$\begingroup\$

Bash / Unix shell, 29

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

Input from STDIN, output on STDOUT.

\$\endgroup\$
5
\$\begingroup\$

Perl, 41

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

$_=<>;y#0-9a-zA-Z+/#w-zA-Z+/0-9a-v#;print

Try it out here.

\$\endgroup\$
2
  • 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
4
\$\begingroup\$

CJam, 24 bytes

q"+"":/{a[A"{,^}/_32m>er

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.                                            ";
\$\endgroup\$
4
\$\begingroup\$

CJam, 45 41 38 29 27 26 bytes

qA,'{,97>_eu"+/"+++_32m>er

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

\$\endgroup\$
0
3
\$\begingroup\$

Python, 178

b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"

def rot32(s):
    o = ""
    for c in s:
        if c not in b:
            o += c
        else:
            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.

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

GolfScript (41 40 bytes)

{'0:a{A['2/{{,>}*}%'+/'+[1$]+.32/(*@?=}%

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.

\$\endgroup\$
0
\$\begingroup\$

python, 69

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

tests

>>> b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
>>> print f("THE+QUICK+BROWN+FOX+JUMPS+OVER+THE+LAZY+DOG=", b)
nb8ukoc6eu5liqhu9irudogjmuip8lunb8uf4tsu7ia=

>>> print f('nb8ukoc6eu5liqhu9irudogjmuip8lunb8uf4tsu7ia=', b)
THE+QUICK+BROWN+FOX+JUMPS+OVER+THE+LAZY+DOG=
\$\endgroup\$
0
\$\begingroup\$

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]
\$\endgroup\$
0
\$\begingroup\$

JavaScript 164

b="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
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}
\$\endgroup\$

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.