给定以下规范,编写程序或函数(或一组程序/函数)对数据进行加密和解密:
"G0lf" (0x47306C66)0x47 ^ 0x30 ^ 0x6C ^ 0x66 = 0x7D0x3A4D111B"s¤Ñ\x11½" (0x73A4D111BD)发布于 2015-04-25 17:44:34
使用二进制表单的方法有点不同
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 formq{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尝试加密器和德雷斯特在线
发布于 2015-04-25 19:39:29
Ksm>+0jCd16_2zJ?,hKeKQmxFdCcK2=KsmmxFkC,dJc?tPKQK2smCid16c?KQ++hJKeJ2这结合了加密和解密,只需添加一个0作为加密参数,或者添加一个用于解密的1。原因很简单。在Pyth中,将字符串转换为位(或4位整数)或反向转换确实很长。通过将这两个函数合并成一个程序,我可以节省大量的字节。
第一部分将输入转换为4位整数的列表(每个字符被转换为2位整数),并将其存储在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)下一部分使用哈希值执行xor操作。当Q == 0在完整列表K上执行时,否则仅在list 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 printhttps://codegolf.stackexchange.com/questions/49199
复制相似问题