首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变相XORyption

变相XORyption
EN

Code Golf用户
提问于 2015-04-25 16:55:24
回答 2查看 950关注 0票数 17

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

加密

  1. 通过异或计算输入的异或散列.
  2. XOR此散列输入的每个字节。
  3. 将结果左移四位。
  4. 用XOR散列的前四位填充左侧。
  5. 用XOR散列的最后四位填充右侧。

示例

  • 给定输入:"G0lf" (0x47306C66)
  • 计算异或散列:0x47 ^ 0x30 ^ 0x6C ^ 0x66 = 0x7D
  • XOR每一个字节的散列:0x3A4D111B
  • 预期结果(移位后和垫片):"s¤Ñ\x11½" (0x73A4D111BD)

规则

  • 只要输入/输出是实际字节,您的程序/函数就可以在所选择的高尔夫语言(String、bytes等)中输入/输出任何类型的内容。例如,您可能不会输出一个十六进制字符串。
  • 加密和解密可以分为单独的程序(分数将是它们的合并大小)或一个单独的程序。单个方法可以对其进行加密或解密。
  • 加密输入的大小可预计至少为1字节。
  • 用于解密的输入至少可以是2个字节。
  • 不需要在输出中转义不可打印的字节。
EN

回答 2

Code Golf用户

发布于 2015-04-25 17:44:34

CJam,36 + 34 = 70字节

使用二进制表单的方法有点不同

加密器

代码语言:javascript
复制
q_:^:Hf^H+{i2b8Ue[}%)4/~@\]e_8/2fb:c

它的工作原理:

代码语言:javascript
复制
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

Decrypter

代码语言:javascript
复制
q{i2b8Ue[4/~}%)\(@+2b\:+8/2fb\f^:c

它的工作原理:

代码语言:javascript
复制
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
EN

Code Golf用户

发布于 2015-04-25 19:39:29

Pyth,69字节

代码语言:javascript
复制
Ksm>+0jCd16_2zJ?,hKeKQmxFdCcK2=KsmmxFkC,dJc?tPKQK2smCid16c?KQ++hJKeJ2

这结合了加密和解密,只需添加一个0作为加密参数,或者添加一个用于解密的1。原因很简单。在Pyth中,将字符串转换为位(或4位整数)或反向转换确实很长。通过将这两个函数合并成一个程序,我可以节省大量的字节。

在线演示:加密解密

解释:

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

代码语言:javascript
复制
  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的第一个和最后一个值。

代码语言:javascript
复制
 ?     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)

下一部分使用哈希值执行xor操作。当Q == 0在完整列表K上执行时,否则仅在list K上执行,而不包含第一个和最后一个值。

代码语言:javascript
复制
=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转换回字符:

代码语言:javascript
复制
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
票数 6
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/49199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档