Trifid Cipher(无关键字)


19

介绍:

我曾经在小时候编译过的文档中存储了许多不同的密码,我选择了一些我认为最适合挑战的密码(不太琐碎,也不太难),并将它们转变为挑战。它们中的大多数仍在沙箱中,我不确定是否要全部发布还是仅发布其中几个。这是第二个(计算机密码是我发布的第一个)。


对于Trifid Cipher(不使用关键字),字母(和附加的通配符)被分为三个3×3表:

table 1:     table 2:     table 3:
 |1 2 3       |1 2 3       |1 2 3
-+-----      -+-----      -+-----
1|a b c      1|j k l      1|s t u
2|d e f      2|m n o      2|v w x
3|g h i      3|p q r      3|y z  

我们要加密的文本是第一个字符,每个字符编码为表行-列号。例如,文本this is a trifid cipher变为:

        t h i s   i s   a   t r i f i d   c i p h e r
table:  3 1 1 3 3 1 3 3 1 3 3 2 1 1 1 1 3 1 1 2 1 1 2
row:    1 3 3 1 3 3 1 3 1 3 1 3 3 2 3 2 3 1 3 3 3 2 3
column: 2 2 3 1 3 3 1 3 1 3 2 3 3 3 3 1 3 3 3 1 2 2 3

然后,我们将所有内容按上表三行一组的顺序逐行放置:

311 331 331 332 111 131 121 121 331 331 313 133 232 313 332 322 313 313 132 333 313 331 223

然后使用相同的表格将它们转换回字符:

s   y   y   z   a   g   d   d   y   y   u   i   q   u   z   w   u   u   h       u   y   o

注意,输入长度应为3的互质。因此,如果长度是3的倍数,我们将附加一个或两个尾随空格以使输入长度不再是3的倍数。

挑战:

给定一个字符串sentence_to_encipher,如上所述将其加密。

您只需加密给定的sentence_to_encipher,因此也无需创建解密程序/函数。不过,我可能会在将来对解密进行第2部分挑战(尽管我觉得这对加密过程而言是微不足道的/类似的)。

挑战规则:

  • 您可以假设sentence_to_encipherwill只包含字母和空格。
  • 您可以使用完整的小写字母或完整的大写字母(请说明您在答案中使用了哪一个)。
  • 您可以选择在输入长度为3时追加一个或两个尾随空格,以使其不再是3的倍数。
  • I / O是灵活的。输入和输出都可以是字符串,列表/数组/字符流等。

通用规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您发布使用非代码高尔夫球语言的答案。尝试针对“任何”编程语言提出尽可能简短的答案。
  • 标准规则适用于具有默认I / O规则的答案,因此允许您使用STDIN / STDOUT,具有适当参数的函数/方法以及返回类型的完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能的话,请添加一个带有测试代码的链接(即TIO)。
  • 另外,强烈建议为您的答案添加说明。

测试用例:

Input:            "this is a trifid cipher"
Output:           "syyzagddyyuiquzwuuh uyo"

Input:            "test"
Output:           "utbk"

Input:            "output"
Possible outputs: "rrvgivx" (one space) or "rrzcc lr" (two spaces)

Input:            "trifidcipher"
Possible output:  "vabbuxlzz utr" (one space) or "vabbyzv rx ie " (two spaces)

3
我读为“没有键盘 ”。那将是一个令人愉快的挑战!
科特·阿蒙-恢复莫妮卡

1
为什么输入的长度应互质为3?我看不出这有多重要。
基金莫妮卡的诉讼

不需要使用互质数来使密码起作用,但是通过确保三位数组成的组不会与初始位数列表的行划分对齐,它确实使其更加安全。
Sparr '18

@NicHartley您确实是正确的,即使输入可以被3整除,您仍然可以对表进行转置。最初,我在Sandbox中没有该规则,但是有人对我说,Trifid的Wikipedia显示它应该添加一个或两个空格,以使加密更加安全。因此,我将其添加为挑战的一部分,并使其与Wikipedia页面(除了我删除的关键字之外)更加同步。
凯文·克鲁伊森

1
@KevinCruijssen我...真的看不出这会使其更安全(如果有的话,强制输入是不是一定长度将使它不太安全,因为你可以猜测最后一个字符编码空格),但我同意将密码描述与Wikipedia保持一致是一个好主意。感谢您的解释!
基金莫妮卡的诉讼

Answers:


6

果冻29 26 25字节

⁶Øa;3ṗ¤,©Ṛy;⁶$L3ḍƊ¡ZFs3®y

在线尝试!

怎么运行的

⁶Øa;3ṗ¤,©Ṛy;⁶$L3ḍƊ¡ZFs3®y  Main link. Argument: s (string)

⁶                          Set the return value to space.
 Øa;                       Append it to "a...z".
    3ṗ¤                    Yield the third Cartesian power of [1, 2, 3].
       ,©                  Pair the results and store the pair in the register.
                           The register now holds
                           [[[1, 1, 1], ..., [3, 3, 3]], ['a', ... ,'z', ' '].
         Ṛ                 Reverse the outer array.
           ;⁶$             Append a space to s...
              L3ḍƊ¡        if the length is divisible by 3.
          y                Transliterate according to the mapping to the left.
                   ZFs3    Zip/transpose, flatten, split into chunks of length 3.
                       ®y  Transliterate according to the mapping in the register.

我从来不知道的副作用输出的规则......但这种安排;L3ḍƊ¡⁶µ⁶Øa;3ṗ¤,ðṚyZFs3⁸y可以废除的µ,如果它是可以接受的24
乔纳森·艾伦

我们对此表示微弱的共识+ 6 / -1),所以谢谢!
丹尼斯,

输出似乎不正确。我认为附加的空格不会“粘住”。
丹尼斯,

6

木炭,39字节

≔E⁺θ× ¬﹪Lθ³⌕βιθ⭆⪪E⁺÷θ⁹⁺÷θ³θ﹪鳦³§⁺β ↨³ι

在线尝试!链接是详细版本的代码。说明:

≔               Assign
   θ            Input string
  ⁺             Concatenated with
                Literal space
    ×           Repeated
         θ      Input string
        L       Length
       ﹪        Modulo
          ³     Literal 3
      ¬         Logical not
 E              Mapped over characters
             ι  Current character
           ⌕    Position found in
            β   Lowercase alphabet
              θ To variable

     θ                      List of positions
    ÷                       Vectorised integer divide by
      ⁹                     Literal 9
   ⁺                        Concatenated with
         θ                  List of positions
        ÷                   Vectorised integer divide by
          ³                 Literal 3
       ⁺                    Concatenated with
           θ                List of positions
  E                         Map over values
             ι              Current value
            ﹪               Modulo
              ³             Literal 3
 ⪪                          Split into
                ³           Groups of 3
⭆                           Map over groups and join
                   β        Lowercase alphabet
                  ⁺         Concatenated with
                            Literal space
                 §          Cyclically indexed by
                       ι    Current group
                     ↨      Converted from
                      ³     Base 3
                            Implicitly print

6

Python 2中180个 176 174 165 163字节

lambda s:''.join(chr(32+(33+a*9+3*b+c)%59)for a,b,c in zip(*[iter(sum(zip(*[(c/9,c/3%3,c%3)for c in map(o,s+' '[len(s)%3:])]),()))]*3))
o=lambda c:(ord(c)%32-1)%27

在线尝试!

输入可以是上限或下限。输出为大写


6

Pyth,34 33字节

m@+G;id3csCmtj+27x+G;d3+W!%lz3zd3

完整程序。输入应为小写,输出为字符数组。在此处在线尝试,或在此处一次验证所有测试用例。

m@+G;id3csCmtj+27x+G;d3+W!%lz3zd3   Implicit: z=input(), d=" ", G=lowercase alphabet
                           lz       Length of z
                          %  3      The above, mod 3
                        W!          If the above != 3...
                       +      zd    ... append a space to z
           m                        Map the elements of the above, as d, using:
                  +G;                 Append a space to the lowercase alphabet
                 x   d                Find the 0-based index of d in the above
              +27                     Add 27 to the above
             j        3               Convert to base 3
            t                         Discard first element (undoes the +27, ensures result is 3 digits long)
          C                         Transpose the result of the map
         s                          Flatten
        c                       3   Split into chunks of length 3
m                                   Map the elements of the above, as d, using:
     id3                              Convert to decimal from base 3
 @+G;                                 Index the above number into the alphabet + space
                                    Implicit print

备选的34字节解决方案:sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3-而不是+27和尾部,用于.[03填充0到长度3。如果s删除了前导,则可以为33 。

编辑:通过删除开头来保存字节,s因为字符数组是有效输出


5

红宝石153个 145 138 131字节

->a{a<<" "if a.size%3<1;a.map{|c|[(b=(c.ord%32-1)%27)/9,b%9/3,b%3]}.transpose.join.scan(/.{3}/).map{|x|((x.to_i(3)+65)%91+32).chr}}

在线尝试!

一种快速且幼稚的方法,适用于小写文本。输入和输出字符数组。


4

Java(JDK),192个字节

s->{String T="",R=T,C=T,r=T;for(int c:s){c-=c<33?6:97;T+=c/9;R+=c%9/3;C+=c%3;}for(var S:(s.length%3<1?T+2+R+2+C+2:T+R+C).split("(?<=\\G...)"))r+=(char)((Byte.valueOf(S,3)+65)%91+32);return r;}

在线尝试!

非常幼稚的方法。接受小写字母char[]作为输入,但输出String

说明

s->{                                       // char[]-accepting lambda
 String T="",                              //  declare variables Table as an empty string,
        R=T,                               //                    Row as an empty string,
        C=T,                               //                    Column as an empty string,
        r=T;                               //                    result as an empty string.
 for(int c:s){                             //  for each character
  c-=c<33?6:97;                            //   map each letter to a number from 0 to 25, space to 26.
  T+=c/9;                                  //   append the table-value to Table
  R+=c%9/3;                                //   append the row-value to Row
  C+=c%3;                                  //   append the column-value to Column
 }                                         //
 for(var S:                                //  For each token of...
     (s.length%3<1?T+2+R+2+C+2:T+R+C)      //    a single string out of table, row and column and take the space into account if the length is not coprime to 3...
      .split("(?<=\\G...)"))               //    split every 3 characters
  r+=(char)((Byte.valueOf(S,3)+65)%91+32); //   Parses each 3-characters token into a number, using base 3,
                                           //  and make it a letter or a space
 return r;                                 //  return the result
}

学分


1
两个小打高尔夫球:Integer.valueOfByte.valueOfR+=c<26?(char)(c+97):' ';R+=(char)(c<26?c+97:32);
凯文Cruijssen


4

R,145字节

function(s,K=array(c(97:122,32),rep(3,3)))intToUtf8(K[matrix(arrayInd(match(c(utf8ToInt(s),32[!nchar(s)%%3]),K),dim(K))[,3:1],,3,byrow=T)[,3:1]])

在线尝试!

I / O为字符串;增加一个空间。[,3:1]R的奇怪重复是因为R的自然数组索引有些不同。


亲爱的,您击败了我200多字节。您的编码@Giuseppe总是给我留下深刻的印象。我什
不理

1
@ Sumner18很好,谢谢!我通常会在尝试挑战之前先经过一两天,因为我知道现在这里还有很多其他R高尔夫球手,但是由于我在沙盒中看到了它,所以我无法抗拒。在R高尔夫聊天室随时欢迎您提出关于高尔夫的想法。:-)
朱塞佩

3
@ Sumner18尝试一下也没什么不好意思的,我在这里的第一次提交太可怕了,我变得越来越好!请继续发布,我认为获得反馈总是很好,因此您可以改善:-)
Giuseppe

3

APL + WIN,102字节

⎕av[n[c⍳(⊂[2]((⍴t),3)⍴,⍉⊃(c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3)[(⎕av[n←(97+⍳26),33])⍳t←t,(3|⍴t←⎕)↓' '])]]

说明:

t←t,(3|⍴t←⎕)↓' ' Prompts for input and applies coprime condition

(⎕av[n←(97+⍳26),33]⍳ Indices of characters in APL atomic vector 

c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3) Create a matrix of table, row column for 27 characters

⊂[2]((⍴t),3)⍴,⍉⊃ Extract columns of c corresponding to input and re-order

c⍳ Identify Column indices of re-ordered columns

⎕av[.....] Use indices back in atomic vector to give enciphered text  

测试用例的屏幕截图示例:

⎕av[n[c⍳(⊂[2]((⍴t),3)⍴,⍉⊃(c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3)[(⎕av[n←(97+⍳26),33])⍳t←t,(3|⍴t←⎕)↓' '])]]
⎕:
'output'
rrvgivx  

您介意添加(一个或多个)测试用例的屏幕快照吗?我知道WIN APL版本在TIO上不可用,但是我仍然希望看到某种验证,因为我几乎不知道如何仅通过阅读来解释APL代码,更不用说在不运行的情况下对其进行验证了。:)
Kevin Cruijssen

不太确定如何执行此操作,但这就是它的外观。我将在上方条目中添加一些内容
Graham

通常,我可以在TIO中使用Dyalog Classic,但在这种情况下,其原子向量的顺序不同,因此无法进行索引。
格雷厄姆

3

SAS,305个字节

对于这个SAS怪诞的事情而言,这真是令人兴奋的“笑声”。我认为我可以避免很多随机字符串格式化;我敢肯定,有更好的方法可以做到这一点。

data;input n:&$99.;n=tranwrd(trim(n)," ","{");if mod(length(n),3)=0then n=cats(n,'{');f=n;l=length(n);array a(999);do i=1to l;v=rank(substr(n,i,1))-97;a{i}=int(v/9);a{i+l}=mod(int(v/3),3);a{i+l*2}=mod(v,3);end;f='';do i=1to l*3by 3;f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97));end;f=tranwrd(f,"{"," ");cards;

在cards语句之后的换行符上输入输入,如下所示:

data;input n:&$99.;n=tranwrd(trim(n)," ","{");if mod(length(n),3)=0then n=cats(n,'{');f=n;l=length(n);array a(999);do i=1to l;v=rank(substr(n,i,1))-97;a{i}=int(v/9);a{i+l}=mod(int(v/3),3);a{i+l*2}=mod(v,3);end;f='';do i=1to l*3by 3;f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97));end;f=tranwrd(f,"{"," ");cards;
this is a trifid cipher
test
output
trifidcipher

输出一个数据集,其中包含变量中的输出f以及一堆辅助变量/数组值。

在此处输入图片说明

取消高尔夫/解释:

data;
input n : & $99.; /* Read a line of input, maximum 99 characters */

n=tranwrd(trim(n)," ","{"); /* Replace spaces with '{' (this is the ASCII character following 'z', so it makes it easy to do byte conversions, and lets us not have to deal with spaces, which SAS does not like) */
if mod(length(n),3)=0then n=cats(n,'{'); /* If length of n is not coprime with 3, add an extra "space" to the end */

f=n; /* Set output = input, so that the string will have the same length */
l=length(n);    /* Get the length of the input */
array a(999);   /* Array of values to store intermediate results */

do i = 1 to l; /* For each character in the input... */
    v = rank(substr(n,i,1))-97; /* Get the value of the current character, from 0-26 */

    a{i}=int(v/9);          /* Get the table of the current character and store at appropriate index, from 0-2  */
    a{i+l}=mod(int(v/3),3); /* Get the row of the current character, from 0-2 */
    a{i+l*2}=mod(v,3);      /* Get the column of the current character, from 0-2  */
end;

f='';

do i = 1 to l*3 by 3; /* For each character in the output... */
    f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97)); /* Convert values back from base 3 to base 10, and convert back into ASCII value */
end;

f = tranwrd(f,"{"," "); /* Replaces our "spaces" with actual spaces for final output */

/* Test cases */
cards;
this is a trifid cipher
test
output
trifidcipher

3

JavaScript(Node.js) 146141139136 字节

I / O是小写的。

s=>'931'.replace(/./g,d=>Buffer(s.length%3?s:s+0).map(c=>(o=(c>48?c-16:26)/d%3+o*3%27|0,++i)%3?0:(o+97)%123||32),i=o=0).split`\0`.join``

在线尝试!

已评论

s =>                       // s = input string
  '931'.replace(/./g, d => // for each digit d = 9, 3 and 1:
    Buffer(                //   create a buffer from:
      s.length % 3 ?       //     if the length of s is coprime with 3:
        s                  //       the original input string
      :                    //     else:
        s + 0              //       the input string + an extra '0'
    )                      //
    .map(c =>              //   for each ASCII code c from this Buffer:
      ( o =                //     update o:
        ( c > 48 ?         //       if c is neither a space nor the extra '0':
            c - 16         //         yield c - 16 (which gives 81 .. 106)
          :                //       else:
            26             //         this is the 26th character (space)
        ) / d % 3 +        //       divide by d and apply modulo 3
        o * 3 % 27 | 0,    //       add o * 3, apply modulo 27, coerce to integer
        ++i                //       increment i
      ) % 3 ?              //     if i mod 3 is not equal to 0:
        0                  //       yield 0 (NUL character)
      :                    //     else:
        (o + 97) % 123     //       convert o to the ASCII code of the output letter
        || 32              //       or force 32 (space) for the 26th character
    ),                     //   end of map()
    i = o = 0              //   start with i = o = 0
  ).split`\0`.join``       // end of replace(); remove the NUL characters

我认为您之前已经解释过,但是如何(o=...,++i)%3再次在JS 中工作?是(o,i)元组或其他东西,并且两个内部整数都转换为它们的模3?作为Java开发人员,它仍然使我有些困惑(a,b)%c。不错的答案!我喜欢您如何转换第三个数字,然后删除前两个空字节。向我+1。
凯文·克鲁伊森

2
@KevinCruijssen引用MDN“逗号运算符评估每个操作数(从左到右)并返回最后一个操作数的值。 ”因此,取模仅应用于++i
Arnauld

3

05AB1E,25 个字节

g3Öð׫SAð«3L3㩇ø˜3ô®Að«‡

由于尚无人发布05AB1E答案,因此我认为自己会发布自己的解决方案。我现在看到它与@ Dennis♦ Jelly的答案非常相似,即使我在提出挑战之前独立提出了它。

输入为字符串,输出为字符列表。如果长度可被3整除,则添加一个空格。

在线尝试验证所有测试用例

说明:

g3Ö         # Check if the length of the (implicit) input is divisible by 3
            # (results in 1 for truthy or 0 for falsey)
            #  i.e. "out" → 1
            #  i.e. "test" → 0
   ð×       # Repeat a space that many times
            #  i.e. 1 → " "
            #  i.e. 0 → ""
     «      # And append it to the (implicit) input
            #  i.e. "out" and " " → "out "
            #  i.e. "test" and "" → "test"
      S     # Then make the string a list of characters
            #  i.e. "out " → ["o","u","t"," "]
            #  i.e. "test" → ["t","e","s","t"]
A           # Push the lowercase alphabet
 ð«         # Appended with a space ("abcdefghijklmnopqrstuvwxyz ")
   3L       # Push list [1,2,3]
     3ã     # Cartesian repeated 3 times: [[1,1,1],[1,1,2],...,[3,3,2],[3,3,3]]
       ©    # Save that list of triplets in the registry (without popping)
           # Transliterate, mapping the letters or space to the triplet at the same index
            #  i.e. ["o","u","t"," "] → [[2,2,3],[3,1,3],[3,1,2],[3,3,3]]
            #  i.e. ["t","e","s","t"] → [[3,1,2],[1,2,2],[3,1,1],[3,1,2]]
ø           # Zip, swapping all rows/columns
            #  i.e. [[2,2,3],[3,1,3],[3,1,2],[3,3,3]] → [[2,3,3,3],[2,1,1,3],[3,3,2,3]]
            #  i.e. [[3,1,2],[1,2,2],[3,1,1],[3,1,2]] → [[3,1,3,3],[1,2,1,1],[2,2,1,2]]
 ˜          # Flatten the list
            #  i.e. [[2,3,3,3],[2,1,1,3],[3,3,2,3]] → [2,3,3,3,2,1,1,3,3,3,2,3]
            #  i.e. [[3,1,3,3],[1,2,1,1],[2,2,1,2]] → [3,1,3,3,1,2,1,1,2,2,1,2]
  3ô        # Split it into parts of size 3
            #  i.e. [2,3,3,3,2,1,1,3,3,3,2,3] → [[2,3,3],[3,2,1],[1,3,3],[3,2,3]]
            #  i.e. [3,1,3,3,1,2,1,1,2,2,1,2] → [[3,1,3],[3,1,2],[1,1,2],[2,1,2]]
®           # Push the triplets from the registry again
 Að«        # Push the lowercase alphabet appended with a space again
           # Transliterate again, mapping the triplets back to letters (or a space)
            # (and output the result implicitly)
            #  i.e. [[2,3,3],[3,2,1],[1,3,3],[3,2,3]] → ["r","v","i","x"]
            #  i.e. [[3,1,3],[3,1,2],[1,1,2],[2,1,2]] → ["u","t","b","k"]

3

Japt,42个字节

;Êv3 ?UpS:U
m!bS=iC)®+27 ì3 ÅÃÕc ò3 £SgXì3

在线尝试!

此答案的核心来自Shaggy删除的答案,但他再也没有回来处理长度可被3整除的输入,因此这是一个 固定版本

说明:

;                                 #Set C to the string "abcdefghijklmnopqrstuvwxyz"

 Ê                                #Get the length of the input
  v3 ?                            #If it is divisible by 3:
      UpS                         # Add a space
         :U                       #Otherwise don't add a space
                                  #Store the result in U

   S=iC)                          #Set S to C plus a space
m                                 #For each character in U:
 !bS                              # Get the position of that character in S
        ®        Ã                #For each resulting index:
             ì3                   # Convert to base 3
         +27    Å                 # Including leading 0s up to 3 places
                  Õ               #Transpose rows and columns
                   c              #Flatten
                     ò3           #Cut into segments of length 3
                        £         #For each segment:
                           Xì3    # Read it as a base 3 number
                         Sg       # Get the letter from S with that index

3

C#(Visual C#交互式编译器),178个字节

s=>{int[]a={9,3,1},b=(s.Length%3>0?s:s+0).Select(c=>c<97?26:c-97).ToArray();s="";for(int i=0,k,l=b.Length;i<l*3;s+=(char)(k>25?32:97+k))k=a.Sum(p=>b[i%l]/a[i++/l]%3*p);return s;}

在线尝试!

少打高尔夫球...仍然令人困惑:)

// s is an input string
s=>{
  // powers of 3
  int[]a={9,3,1},
  // ensure the length of s is coprime to 3
  // and convert to numbers from 0-26
  b=(s.Length%3>0?s:s+0).Select(c=>c<97?26:c-97).ToArray();
  // reset s to collect result
  s="";
  // main loop
  for(
    // i is the main index variable
    // k is the value of the encoded character
    // l is the length
    int i=0,k,l=b.Length;
    // i continues until it is 3x the length of the string
    i<l*3;
    // convert k to a character and append
    s+=(char)(k>25?32:97+k)
  )
    // compute the trifid
    // (this is the confusing part :)
    k=a.Sum(p=>b[i%l]/a[i++/l]%3*p);
  // return the result
  return s;
}
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.