将文本转换为二进制字符串会得到与用于生成文本的二进制字符串不同的二进制字符串

发布于 2025-01-11 17:49:08 字数 1986 浏览 0 评论 0原文

我正在用 python 创建一个简单的 XOR 程序。用户输入简单的明文,该明文被转换为一串位。生成的密钥与明文位长度相同,并且经过异或运算。我将异或位转换回文本并将其写为密文。这一切都可以完美运行,没有任何问题。

我遇到的问题是我想获取相同的密文并将其转换回二进制形式以准备解密。当我这样做时,我得到了一串完全不同的位。

可重现的示例:

import secrets
import sys

def encrypt(plaintext):
    binary_plaintext = [format(x, 'b').zfill(8) for x in bytearray(plaintext, 'utf-8')] # Convert Plaintext to Binary and separates into bytes in a list
    binary_plaintext = "".join(binary_plaintext)                                        # Take list of bytes and join together in one long binary string

    key = key_generator(binary_plaintext) # Create key of 'n' bits where n == len(binary_plaintext)

    ciphertext_binary_string = "".join([str(int(bit1, 2) ^ int(bit2, 2)) for bit1, bit2 in zip(binary_plaintext, key[0])]) # XOR Operation with Key and Plaintext
    ciphertext_binary_bytes = " ".join([ciphertext_binary_string[idex:idex+8] for idex in range(0, len(ciphertext_binary_string), 8)]) # Split the Ciphertext into bytes
    ciphertext = ''.join([chr(int(byte,2)) for byte in ciphertext_binary_bytes.split(" ")]) # Convert Ciphertext to text i.e. ~ýÄÞ$~Ý

    decrypt(ciphertext, key[0], ciphertext_binary_string) # Pass the exact same ciphertext to the decryption

def decrypt(ciphertext, key, previous_ciphertext):
    ciphertext_binary = bin(int.from_bytes(ciphertext.encode('utf-8'), sys.byteorder))
    print(f"{ciphertext_binary[2:]} does not match {previous_ciphertext}")

def key_generator(binary_plaintext):
    integer_key = secrets.randbits(len(binary_plaintext))  # Build a random key that is the same length as the plaintext
    binary_key = bin(integer_key)[2:].zfill(len(binary_plaintext))
    return binary_key, integer_key

encrypt("Hello There")

本质上有:

  1. 第 10 行以位形式存在的密文
  2. 第 12 行将此密文转换为文本
  3. 将相同的密文传递给解密()
  4. 将相同的密文转换回其二进制格式(如第 10 行所示)第 17 行文本返回到第 17 行的位。
  5. 第 10 行和第 17 行不匹配。这就是我试图理解的。二进制>正文>二进制但两个二进制形式不匹配。

任何见解都值得赞赏。谢谢

I am creating a simple XOR program in python. The user inputs simple plaintext which is converted to a string of bits. A key is generated of equal length to the plaintext bits, and it is XOR'd. I convert the XOR'd bits back to text and write it as ciphertext. This all works perfectly without issues.

The issue I am having is I want to take that same ciphertext and convert it back to its binary form in preparation for decryption. When I do so, I get a completely different string of bits.

Reproducible example:

import secrets
import sys

def encrypt(plaintext):
    binary_plaintext = [format(x, 'b').zfill(8) for x in bytearray(plaintext, 'utf-8')] # Convert Plaintext to Binary and separates into bytes in a list
    binary_plaintext = "".join(binary_plaintext)                                        # Take list of bytes and join together in one long binary string

    key = key_generator(binary_plaintext) # Create key of 'n' bits where n == len(binary_plaintext)

    ciphertext_binary_string = "".join([str(int(bit1, 2) ^ int(bit2, 2)) for bit1, bit2 in zip(binary_plaintext, key[0])]) # XOR Operation with Key and Plaintext
    ciphertext_binary_bytes = " ".join([ciphertext_binary_string[idex:idex+8] for idex in range(0, len(ciphertext_binary_string), 8)]) # Split the Ciphertext into bytes
    ciphertext = ''.join([chr(int(byte,2)) for byte in ciphertext_binary_bytes.split(" ")]) # Convert Ciphertext to text i.e. ~ýÄÞ$~Ý

    decrypt(ciphertext, key[0], ciphertext_binary_string) # Pass the exact same ciphertext to the decryption

def decrypt(ciphertext, key, previous_ciphertext):
    ciphertext_binary = bin(int.from_bytes(ciphertext.encode('utf-8'), sys.byteorder))
    print(f"{ciphertext_binary[2:]} does not match {previous_ciphertext}")

def key_generator(binary_plaintext):
    integer_key = secrets.randbits(len(binary_plaintext))  # Build a random key that is the same length as the plaintext
    binary_key = bin(integer_key)[2:].zfill(len(binary_plaintext))
    return binary_key, integer_key

encrypt("Hello There")

Essentially there is:

  1. Ciphertext in the form of bits on line 10
  2. Convert this Ciphertext to text on line 12
  3. Pass this same Ciphertext to decryption()
  4. Convert the same Ciphertext back to it's binary format (as we have on line 10) on line 17 text back to bits on line 17.
  5. Line 10 and line 17 do not match. This is what I am trying to understand. Binary > Text > Binary but the two binary forms do not match.

Any insight is appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文