当我不想这样做时,我该如何处理(或使用)此功能返回十六进制字符? (Python 3)
我编写了一个加密功能,该功能通过在明文中的字母和密钥中的相应字母中执行XOR函数来起作用。请参阅下面的代码:
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
当加密函数起作用时,对于Pycharm(我的IDE)似乎无法表示的某些Unicode字符,返回的ciphertext中具有十六进制:
Enter the message to be encrypted Hello world
8
?
;
l
=
6
('\x01\x178?;l\x07\x00=\x0e6')
如您所见,对于Ciphertext中的某些字符,我假设我假设我要假设是一种被使用的占位者。然后,这些字符在底部的最终输出键中表示为十六进制。这是一个问题,因为我希望使用此键来解密此文本,为此,必须发生两件事之一:
将十六进制转换为最终键中的Unicode字符。不确定这是否是明智的,因为多个不同字符将由相同的答案表示
是否会识别文本中的十六进制字符,并将其转换为Unicode本身
我该如何完成其中的任何一个?
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. 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:
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:
Convert the hexadecimal into a unicode character in the final key. Not sure if that would be wise as multiple different characters will be represented by the same answer
Have the decryption algorithm recognise the hexadecimal characters in the text and convert them into unicode themselves
How would I accomplish either of these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您描述的问题的核心是您的混乱与Python中的可变类型以及用于存储在文件中的文本/字符串的编码有关。
Python字符串保留Unicode字符,字节字符串在范围内包含ASCII代码/整数(0,255),依此类推。让我们从a 演示在评论中链接到注释与我鼓励您阅读的问题:
ℛℯα∂α♭ℓℯη☺歇ѧ$☾ℐℐ$
☾ℐℐ ,混乱将消失,您可以提出正确的问题。我建议您考虑如何在Unicode和字节之间进行转换以及UTF-8,UTF-16等。
您看到的是不是您所拥有的。
这一事实通常是为什么这个问题在这么多人中造成如此沉重的困惑的原因。例如,如果您看到文本编辑器中有下一行,通常不会看到该行的断裂是由两个字符组成的(如果您使用MS Windows,默认值)或仅一个字符(UNIX/Linux System中的默认值) 。与编码和存储文本有关的问题,并在文本编辑器中查看文本并不是一件容易的事,需要深入了解。
很遗憾地说,没有办法学习如何指定和使用编码来编写和阅读文件(除了您希望始终依靠外部帮助)。
没有加密的代码和解密的代码,也没有写入文件和从文件读取的代码,就很难判断出事情是否会按预期奏效。
混乱已经从一个问题开始:如何读取并解码存储在文件中的文本中?有字节吗?文件中是否存储UTF-8或UTF-16字符?还是使用代码页?哪种编码用于将文件写入文件?哪种编码用于从文件中读取?
看来您不知道上述所有这些问题。但是您应该,如果出现问题,您是否会理解如何修复它们。
开始学习编码的一个很好的点是访问此 stackoverflow问题(如何知道我使用搜索引擎和关键字的python中文件编码?[重复] ) em> > 什么是字符编码,为什么我应该打扰它。
我一直在stackoverflow上写作,已经在编码的主题上(使用'用户:7711283编码'在stackoverflow自己的搜索中搜索8个结果的完整列表)。 look 在这里(如果您在Python(或File)中有字符串/文本(或文件),那么您永远都不会能够看到它“是” )。您越理解为什么您永远无法看到字符串',因为它是'',您对所看到的东西的困惑就越少。外观在这里
(没有办法避免编码/解码,但是有一种方法是不明确的。)
下一步是找出在保存时使用您的文本编辑器,请使用哪个文件编码使用您的文本编辑器或加载Python脚本或文本,以帮助您解释您实际上在编辑器中看到的到底显示的内容。在此处查找此信息的提示下方:

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.
A Python string holds Unicode characters, a byte string holds ASCII code/integers in range(0,255), and so on. 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:
ℛℯα∂α♭ℓℯ ♭ʊ☂ η☺т Ѧ$☾ℐℐ ¡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.
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). 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? Are there bytes? Are there Unicode UTF-8 or UTF-16 characters stored in the file? 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.
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). 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'). 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. Below a hint where to look for this information:
