构建一个rot32加密器


10

构建高尔夫rot13加密器太容易了,因为字母在ASCII字符空间中的顺序都相同。让我们尝试使用rot32引擎。

您的任务是构建一个函数,该函数将Base64字符串作为输入并返回相同的字符串,但每个字母从其原始字符开始旋转32个符号(本质上,第一位被翻转)。

用于此问题的base64编码字符串0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/的填充字符为=。这是为了防止原本使用或导入内置Base64库的解决方案,该库通常以字符串开头A而不是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+

以任何语言进行的最短程序都将获胜。


4
从这个问题上,我将理解我们必须进行base-64解码,一些位纠缠和base-64编码。但是您的样本答案表明,有关base-64的所有讨论都是红色鲱鱼,就像rot-13一样,只是不被忽​​略的字符集更大。哪有
彼得·泰勒

1
就像rot-13一样,但我当时认为按位排列可能是使代码更短的一种可行策略。
Joe Z. 2014年

Answers:


4

CJam,24个字节

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

在线尝试。

怎么运行的

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.                                            ";

15

Bash / Unix shell,29岁

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

从STDIN输入,在STDOUT输出。


5

Perl,41岁

只是一个简单的音译。从STDIN读取,输出到STDOUT:

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

在这里尝试。


2
谁说Perl不是esolang(和/或面向高尔夫的语言)?
Optimizer

您可以通过使用-p-e命令行开关来删除$_=<>;和和来删除一些字符;print
2014年

4

CJam,45 41 38 29 27 26字节

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

从STDIN读取要加密的字符串

运作方式

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";

在这里在线尝试


3

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

这是Python中无法使用的最后一个参考实现,可用于测试自己的实现。


至少,我希望它保持最后位置。
Joe Z.

0

GolfScript(41 40字节)

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

在线演示

这有两部分:转换是tr在GolfScript中的技巧中提到的最后一种技术的变体,另一部分是字符串构建,它使用字符串0:a{A[作为char值数组和折叠将它们转换为char值。字符范围。请注意使用32/(*来构建翻译后的字符串,方法是在后32个字符和我们要翻译的字符之间插入前32个字符。


0

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

>>> print f('nb8ukoc6eu5liqhu9irudogjmuip8lunb8uf4tsu7ia=', b)
THE+QUICK+BROWN+FOX+JUMPS+OVER+THE+LAZY+DOG=

0

LiveScript,91岁

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

LiveScript,50岁

如果允许将字符串作为第二个参数。

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

0

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}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.