简体   繁体   English

当我不希望它返回十六进制字符时,如何解决(或使用)这个函数? (Python 3)

[英]How can I work around (or with) this function returning hexadecimal characters when I don't want it to? (Python 3)

I've written an encryption function that works by performing an XOR function on a letter in the plaintext and the corresponding letter in the key.我编写了一个加密函数,它通过对明文中的字母和密钥中的相应字母执行 XOR 函数来工作。 See the code below:请看下面的代码:

def vernam(y):
    ciphertext = ""  # this declares the ciphertext variable
    vernamkey = []
    for letter in y:
        individualletterkey = secrets.choice(string.ascii_letters)  # this generates a different key for each letter
        vernamkey.append(individualletterkey)
        newletter = chr(ord(letter) ^ ord(individualletterkey))
        print(newletter)
        ciphertext += newletter
    for element in vernamkey:  # this loop ensures that the key for every letter is in a text file that can be passed
        # on to the intended recipient for them to decrypt
        vkey.write(str(element))
        vkey.write("\n")
    return ciphertext

While the encrypt function works, for certain unicode characters that pycharm (my IDE) can seemingly not represent, the returned ciphertext has hexadecimal in it:虽然 encrypt 函数有效,但对于 pycharm(我的 IDE)似乎无法表示的某些 unicode 字符,返回的密文中包含十六进制:

Enter the message to be encrypted Hello world


8
?
;
l

 
=

6
('\x01\x178?;l\x07\x00=\x0e6')

As you can see, for certain characters in the ciphertext what I'm assuming is a sort of placeeholder is used.如您所见,对于密文中的某些字符,我假设使用了一种占位符。 These characters are then represented as hexadecimal in the final outputted key at the bottom.然后这些字符在底部的最终输出密钥中表示为十六进制。 This is a problem because I wish to use this key to decrypt this text, and for that to be done one of two things has to happen:这是一个问题,因为我希望使用此密钥来解密此文本,并且要做到这一点,必须发生以下两件事之一:

  1. Convert the hexadecimal into a unicode character in the final key.将十六进制转换为最终密钥中的 unicode 字符。 Not sure if that would be wise as multiple different characters will be represented by the same answer不确定这是否明智,因为多个不同的字符将由相同的答案表示

  2. Have the decryption algorithm recognise the hexadecimal characters in the text and convert them into unicode themselves让解密算法识别文本中的十六进制字符并自行将其转换为 unicode

How would I accomplish either of these?我将如何完成其​​中任何一项?

The core of the problem you describe is your confusion related to variable types in Python and to encoding of texts/strings for storage in a file.您描述的问题的核心是您对 Python 中的变量类型以及用于存储在文件中的文本/字符串编码的混淆。

A Python string holds Unicode characters, a byte string holds ASCII code/integers in range(0,255), and so on. Python 字符串包含 Unicode 字符,字节字符串包含范围 (0,255) 内的 ASCII 代码/整数,依此类推。 Let's put here a bit of Unicode fun from a presentation linked in the comments to your question which I encourage you to read:让我们从评论中链接到您的问题的演示文稿中放一些 Unicode 乐趣,我鼓励您阅读:

ℛℯα∂α♭ℓℯ ♭ʊ☂ η☺т Ѧ$☾ℐℐ ¡ooʇ ןnɟǝsn sı uʍop-ǝpısdn ℛℯα∂α♭ℓℯ ♭ʊ☂ η☺т Ѧ$☾ℐℐ ¡ooʇ ןnɟǝsn sı uʍop-ǝpısdn

Once you are clear in mind what do you want to achieve, the confusion will be gone and you can ask the right questions.一旦你清楚你想要达到什么目标,困惑就会消失,你可以提出正确的问题。 I suggest you consider to study how to convert between Unicode and bytes and what UTF-8, UTF-16 etc. are.我建议你考虑研究一下如何在 Unicode 和字节之间进行转换,以及 UTF-8、UTF-16 等是什么。

What you see is not what you have got.你所看到的并不是你所拥有的。

This fact is usually the reason why this issues create so heavy confusion in so many people.这个事实通常是为什么这个问题在这么多人中造成如此严重的困惑的原因。 For example if you see there is a next line in the text editor you usually don't see if the break of the line consists of two characters (default if you use MS Windows) or only one character (default in Unix/Linux system).例如,如果您在文本编辑器中看到下一行,您通常看不到换行符是由两个字符组成(如果您使用 MS Windows,则为默认值)或只有一个字符(在 Unix/Linux 系统中为默认值) . The issues related to coding and storing texts in files and viewing the text in a text editor are not trivial and need some deep understanding.与在文件中编码和存储文本以及在文本编辑器中查看文本相关的问题并非易事,需要深入了解。

Sorry to say that there is no way around learning how to specify and use encoding for writing and reading from files (except you want always to rely on external help).很抱歉,没有办法学习如何指定和使用编码来写入和读取文件(除非您总是希望依赖外部帮助)。

Without both the code for encryption and the code for decryption, and both the code for writing to file and reading from file, it would be hard up to impossible to tell if things will work out as expected.如果没有加密代码和解密代码,以及写入文件和读取文件的代码,就很难判断事情是否会按预期进行。

The confusion begins already with the question: How to read and decode text stored in a file into a Python variable?困惑已经从一个问题开始:如何读取存储在文件中的文本并将其解码为 Python 变量? Are there bytes?有字节吗? Are there Unicode UTF-8 or UTF-16 characters stored in the file?文件中是否存储了 Unicode UTF-8 或 UTF-16 字符? Or are code pages used??还是使用了代码页?? Which encoding was used to write out to the file?使用哪种编码写入文件? Which encoding is used to read from the file?使用哪种编码从文件中读取?

It seems that you are not aware of all this above mentioned issues.您似乎没有意识到上述所有这些问题。 But you should, will you understand how to fix them if things go wrong.但是,如果出现问题,您应该了解如何解决它们。

A good point to start learning about encoding is to visit this stackoverflow question ( How to know the encoding of a file in Python? [duplicate] ) I found using a search engine and the keywords: 'python file encoding' or this one: What is character encoding and why should I bother with it .开始学习编码的一个好点是访问这个stackoverflow 问题如何知道 Python 中文件的编码?[重复] )我使用搜索引擎和关键字发现:'python 文件编码'这个:什么是字符编码,我为什么要打扰它

I had been writing here on stackoverflow already on the subject of encoding ( use 'user:7711283 encoding' in the stackoverflow own search for a complete list of 8 results ).我已经在stackoverflow上写了关于编码的主题(在stackoverflow自己的搜索中使用'user:7711283 encoding'来搜索8个结果的完整列表)。 Look here ( If you have a string/text in Python (or file) you are never ever would be able to see it 'as it is' ).这里如果您在 Python(或文件)中有一个字符串/文本,您将永远无法“按原样”看到它)。 The better you understand why you never ever would be able to see a string 'as it is' the less you are confused about what you see.您越了解为什么您永远无法“按原样”看到字符串,您就越不会对所看到的内容感到困惑。 Look also here ( there is NO WAY to avoid encoding/decoding but there is a way of doing it in a not explicit way. )也看这里没有办法避免编码/解码,但有一种方法可以以不明确的方式进行。)

The next step would be to find out which file encoding uses your text editor when it saves or loads a Python script or text to help you with interpretation of what exactly you actually see displayed in the editor.下一步是找出在保存或加载 Python 脚本或文本时使用文本编辑器的文件编码,以帮助您解释您在编辑器中实际看到的内容。 Below a hint where to look for this information:以下提示在何处查找此信息: 文本文件编辑器菜单文件中的编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Function 在我不想要的时候重复 - Function is repeating when I don't want it to 如何使用不想在python中删除的正则表达式保留字符? - How to keep characters with regular expressions that I don't want to delete in python? 当我不想重复值时,在 python 中执行合并 function - performing a merge function in python, when I don't want the values to repeat 我不明白如何使用它能够工作的函数处理返回一个数字 - I don't understand how returning a number using the function processing it is able to work 如何在 GitHub 源 (Python) 上向 Heroku 添加一个我不想要的秘密文件 - How can I add a secret file to Heroku that I don't want on the GitHub source (Python) 如何使用 Python 隔离我不想要的字符串的最后部分? - How can I isolate the final part of the string that I don't want with Python? Python 在我不想要的时候更新列表 - Python updating list when I don't want it to 在Python中,如何删除特定字符周围的字符? - In Python, how can I remove characters around a specific character? Python函数-不知道需要多少个参数时如何动态调用函数? - Python Functions - how can I dynamically call a function when I don't know how many arguments it expects? 我想在按下选择时实现 on_touch_down 功能,但不知道如何实现它 - I want to implement the on_touch_down function when a pick is pressed, but don't know how to implement it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM