【问题标题】:Python - Pseudocode to decrypt a XOR encrypted stringPython - 用于解密 XOR 加密字符串的伪代码
【发布时间】:2019-10-22 11:41:37
【问题描述】:

我正在尝试解决这个挑战,但遇到了一些麻烦。首先,我对 XOR 不是很熟悉。我知道 XOR 是对称的,所以解密这个字符串所需要做的就是用我拥有的相同密钥再次加密它。

我不想给我答案,所以如果可能的话,有人可以用一些伪代码来帮助启动我的大脑,你将如何创建一个 Python 脚本来获取用户输入,并解密他们的 XOR 加密字符串?

【问题讨论】:

  • 向我们展示您迄今为止所做的尝试 - 您知道如何简单地将两个数字相异或吗?你知道如何将字符转换为数字,反之亦然吗?

标签: python python-3.x encryption xor


【解决方案1】:

让我这样解释。

XOR table
=========
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

所以,想象一个二进制数

10110011

而且,你有一把钥匙

11001100

如果你对它们进行异或,你会得到:

01111111

和我一起?现在,将加密的数字与密钥进行异或,然后返回原始数字。

10110011

这里还有一些伪代码。展示作品是一件很沉重的事情。

Get the input-string from user
Get the encrypt-key from user     #Assume it is also a string;

Loop through the input-string character by character
    Convert character to its binary representation as a string
    Concatenate that to input-string-converted-to-binary string

Loop through the encrypt-key character by character
    Convert character to its binary representation as a string
    Concatenate that to encrypt-key-converted-to-binary string

Get the length of the encrypt-key-converted-to-binary string
Calculate totalloops by dividing that into the length of the input-string-converted-to-binary string

Loop for totalloops
    Loop for each character in the subsection of the input-string-converted-to-binary string
        Calculate XOR of the digit
        Concatenate into encrypted-value string

你现在有了二进制字符串。您可以反转将二进制字符串转换为字符的过程。但是,您可能会得到无法打印的字符,这就是为什么这样做会让您有更多的检查时间。

【讨论】:

  • 谢谢,这更有意义。我在这里找到了一些示例代码,它基本上实现了您的布局。 abhishekshukla.com/python/…
  • 很高兴我能给你一些帮助。
【解决方案2】:
user_number = input("Enter your number")

if( user_number. isdigit()):

print("User input is Number ")
else:

print("User input is string ")

因为它的两个输入不能被明确地重构 从它的单一输出。

【讨论】:

  • 这似乎与 XOR 无关。这基本上只是确定用户输入是整数还是字符串。
猜你喜欢
  • 1970-01-01
  • 2020-03-20
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
相关资源
最近更新 更多