移位XORyption


15

编写一个程序或函数(或一组程序/函数),以给定以下规范来加密和解密数据:

加密

  1. 通过将每个字节彼此异或来对输入进行XOR哈希计算。

  2. 通过此哈希对输入的每个字节进行XOR。

  3. 将结果左移四位。

  4. 用XOR哈希的前四位填充左侧。

  5. 在XOR哈希的最后四位填充右侧。

  • 给定的输入:"G0lf"0x47306C66

  • 计算XOR哈希: 0x47 ^ 0x30 ^ 0x6C ^ 0x66 = 0x7D

  • 通过哈希对每个字节进行XOR: 0x3A4D111B

  • 预期结果(后移和垫): ()"s¤Ñ\x11½"0x73A4D111BD

规则

  • 只要输入/输出是实际的字节,您的程序/函数就可以在您选择的高尔夫语言(字符串,字节数组等)中有意义的任何类型的输入/输出。例如,您可能不输出十六进制字符串。

  • 加密和解密可以分为单独的程序(分数将是它们的总大小)或单个程序。单个方法可以采用参数进行加密还是解密。

  • 预计加密输入至少为1个字节。

  • 解密输入至少应为2个字节。

  • 不可打印字节不需要在输出中转义。


1
十进制数组可以用作输出形式吗?
ɐɔıʇǝɥʇuʎs

@ɐɔıʇɥʇuʎs将输入和输出作为整数数组表示字节是可以接受的。
nderscore

是否有最大输入长度(例如14个字节(56位),以便最终结果适合64位整数)?
门把手

1
请注意:从加密的角度来看,这不是加密,因为它没有密钥(或0位密钥)。
圣保罗Ebermann

1
我只是在等待某人发布有关永不滚动自己的加密功能的信息,而忽略了
它所

Answers:


9

CJam,28 + 27 = 55字节

对于每一部分,我都会介绍一个程序,该程序期望输入/输出采用整数数组的形式,而另一个程序则使用字符串。上面的字节数用于整数数组版本,但链接的脚本和说明用于基于字符串的版本(可用于测试问题中给出的示例)。

加密

q~_:^_GbYUe[\@f^Gfbe_*2/Gfbp
q:i_:^_GbYUe[\@f^Gfbe_*2/Gfb:c

解密

q~{_G/\G%}%)\(G*@+\2/Gfbf^p
q:i{_G/\G%}%)\(G*@+\2/Gfbf^:c

这是一个测试脚本,它执行一次完整的往返并在再次进行解密之前打印加密的代码。

说明

q:i_:^_GbYUe[\@f^Gfbe_*2/Gfb:c
q:i                            e# Read the input and convert characters to byte values.
   _:^                         e# Duplicate and fold XOR onto the characters to get 
                               e# the hash.
      _Gb                      e# Duplicate and convert to base 16 to get nibbles.
         YUe[                  e# Pad to width 2 with zeroes.
             \@                e# Pull up other copy and integer array.
               f^              e# XOR each integer with the hash.
                 Gfbe_         e# Convert each result to base 16 and flatten that.
                      *        e# Join the hash nibbles with this array.
                       2/      e# Split into pairs.
                         Gfb   e# Interpret each pair as base 16.
                            :c e# Convert each integer to a character.

q:i{_G/\G%}%)\(G*@+\2/Gfbf^:c
q:i                            e# Read the input and convert characters to byte values.
   {      }%                   e# Map this block onto each byte.
    _G/\G%                     e# Get the two base 16 digits individually.
            )\(                e# Slice off the last and first nibble.
               G*@+\           e# Combine into byte (the hash) and swap with array.
                    2/Gfb      e# Split array into pairs and interpret each as base 16.
                         f^    e# XOR each with the hash.
                           :c  e# Convert each integer to a character.

6

CJam,36 + 34 = 70字节

使用二进制形式的方法有些不同

加密器

q_:^:Hf^H+{i2b8Ue[}%)4/~@\]e_8/2fb:c

怎么运行的:

q_:^                                  e# Read input as string, copy and XOR all the chars
    :Hf^                              e# Store the XOR in H and XOR each char with H
        H+                            e# Append H to the char array
          {       }%                  e# On each of the elements in the array
           i2b                        e# Convert the ASCII value to binary
              8Ue[                    e# Pad with 0 so that the length is 8
                    )                 e# Pop out the last array element, which is H
                     4/~@\            e# Put first 4 bits of H before the input array
                                      e# And rest 4 after it
                          ]e_8/       e# Flatten everything into a single array and group
                                      e# into pieces of 8 bits
                               2fb:c  e# Convert each 8 bit part to integer and then to
                                      e# its character form

解密器

q{i2b8Ue[4/~}%)\(@+2b\:+8/2fb\f^:c

怎么运行的:

q{          }%                      e# For each character of the input string
  i2b                               e# Convert to ASCII code and then to its binary form
     8Ue[                           e# Pad with enough 0 so that length is 8 bit
         4/~                        e# Split into parts of 4 and unwrap
              )\(@+                 e# Take out the first and last 4 bit group and join
                                    e# them together to get the XOR Hash H
                   2b\              e# Convert H to decimal form and swap to put the
                                    e# remaining converted input array on top
                      :+8/          e# Join all bits together and split into groups of 8
                          2fb       e# Convert each 8 but group to decimal form
                             \f^    e# Swap to put H on top and XOR each number with H
                                :c  e# Get character from each of the ASCII value

在线尝试加密解密


6

Pyth,69个字节

Ksm>+0jCd16_2zJ?,hKeKQmxFdCcK2=KsmmxFkC,dJc?tPKQK2smCid16c?KQ++hJKeJ2

这结合了加密和解密,只需0为加密添加一个as参数或1解密。这样做的原因是简单的。在Pyth中,将字符串转换为位(或4位整数)或相反的操作确实非常长。通过将这两个函数组合到一个程序中,我可以节省很多字节。

在线演示:加密解密

说明:

第一部分将输入转换为4位整数列表(每个char转换为2个4位整数)并将其存储在中K

  m          z   map each character d of input (=z) to:
       Cd            the ascii-value of d
      j  16          convert the result into base 16
   >+0     _2        insert a zero to the front and take the last 2 values
                     (so that each char gets mapped to exactly 2 numbers)
Ks               chain all these tuples and assign them to K

第二部分确定哈希值并将其存储在中J。如果Q==0通过xor进行计算,则采用的第一个和最后一个值K

 ?     Q           ... if Q (=second input) else ...
  ,hKeK            [K[0], K[-1]]
        m   CcK2   map each d of zipped(K chopped into pairs) to:
                   [zipped(...) gives me 2 lists, one with the values of the even indices, and one with the odd indices]
         xFd           fold the list d by xor
J                  store the result in J (this is the hash value)

下一部分使用哈希值进行异或。在Q == 0完整列表上执行时K,否则仅在K没有第一个和最后一个值的列表上执行。

=KsmmxFkC,dJc?tPKQK2
             ?tPKQK    K[1:-1] if Q else K 
   m        c      2   map each d of [... chopped into pairs] to:
    m   C,dJ              map each pair k of zip(d,J) to:
     xFk                     apply xor to the 2 values in k
=Ks                    chain all these tuples and assign them to K

最后一部分转换K回字符:

smCid16c?KQ++hJKeJ2
        ?KQ++hJKeJ    K if Q else J[0] + K + J[1]
 m     c          2   map each pair of [... chopped into pairs] to:
   id16                  convert d into a single integer
  C                      convert to char
s                     join all chars and print

0

Javascript(ES6)83 + 73 = 156

这两个函数都将as作为输入并输出一个数字数组来表示字节。

加密85 84 83

E=s=>s.concat((h=s.reduce((x,y)=>x^y))<<4&240^h).map(x=>a<<4&240|(a=x^h)>>4,a=h>>4)

解密75 73

D=s=>s.map(x=>(a<<4&240|(a=x)>>4)^h,h=(a=s.shift())&240|s[~-s.length]&15)

演示(仅Firefox)

E=s=>s.concat((h=s.reduce((x,y)=>x^y))<<4&240^h).map(x=>a<<4&240|(a=x^h)>>4,a=h>>4)
D=s=>s.map(x=>(a<<4&240|(a=x)>>4)^h,h=(a=s.shift())&240|s[~-s.length]&15)

toHexString = x=>'0x'+x.map(y=>y.toString(16)).join('')

input = [...'G0lf'].map(x=>x.charCodeAt());
document.write('Input: ' + toHexString(input) + '<br />');

encrypted = E(input);
document.write('Encrypted: ' + toHexString(encrypted) + '<br />');

decrypted = D(encrypted);
document.write('Decrypted: ' + toHexString(decrypted) + '<br />');


使用字符串131 + 129 = 260

只是为了好玩...这里有些版本使用字符串代替输入/输出。

E=(s,h=0)=>[for(x of s)(h^=y=x.charCodeAt(),y)].concat(h<<4&240^h).map(x=>String.fromCharCode(a<<4&240|(a=x^h)>>4),a=h>>4).join('')

D=s=>(s=[s.charCodeAt(j=i)for(i in s)]).map(x=>String.fromCharCode((a<<4&240|(a=x)>>4)^h),h=(a=s.shift())&240|s[~-j]&15).join('')

E('G0lf') // 's¤Ñ\x11½'
D('s¤Ñ\x11½') // 'G0lf'
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.