将字母转换为数值

发布于 2025-01-12 18:50:03 字数 742 浏览 2 评论 0原文

我正在尝试在知道密文和明文的情况下解码Python中的维吉尼亚密码,

我有一个这种形式的文本字符串'SDFJNKSJDFOSNFDF'。 我想将每个字母转换为数字,以便能够解码文本,即 A 为 1,B 为 2...

我的想法是使用 ascii 进行转换,检查大写字母对应的含义我可以推断这一点,我没有不起作用的示例代码,我写了一些细节,希望能让我的想法更容易理解:

我的字符串看起来像这样:

string = 'SDFJNKSJDFOSN'

但是,当我使用维吉尼亚密码时,我知道我的块长度是六,所以我也有

blocks = ['SDFJNK', 'SJDFOS', 'N']

我不知道是否会更容易转换为数字然后分成块或相反?我的想法是使用 ascii 文本,例如,如果在 ascii D = 35 中,因为这包含多个字符,我可以用它来重写所有数字,无论它们的 ascii 是什么 - 31 所以 D = 4 和所有其他字母都会效仿

对于缺乏我的意思的书面示例表示歉意,但这里的其他问题有很多变化,我不知道从哪里开始,理想情况下输出看起来像这样:

output:
['19,4,6,10,14,11','19,10,4,6,15,19','15']

或者

output:
['19,4,6,10,14,11,19,10,4,6,15,19,15']

I am trying to decode a Vigenere cipher in python knowing the ciphertext and plaintext

I have a string of text in this form 'SDFJNKSJDFOSNFDF'.
I want to convert each letter to a number so I'm able to decode the text, i.e. A to 1, B to 2...

The idea I had was to convert this using ascii, checking what the capital letters correspond to would mean I could deduct this, I don't have an example code that hasn't worked, I've written some details that will hopefully make my idea easier to understand:

My string looks like this:

string = 'SDFJNKSJDFOSN'

However as I am working with a Vigenere cipher, I know my blocklength is six so I also have

blocks = ['SDFJNK', 'SJDFOS', 'N']

I don't know whether it would be easier to convert to numbers then break into blocks or the other way around? My thoughts were to use ascii text, for example if in ascii D = 35 as this is inclusive of multiple characters, I could use this to rewrite all numbers as whatever their ascii is - 31 so D = 4 and all other letters would follow suit

Apologies for the lack of written examples of what I mean but other questions on here have so much variation I don't know where to start, ideally the output would look something like this:

output:
['19,4,6,10,14,11','19,10,4,6,15,19','15']

OR

output:
['19,4,6,10,14,11,19,10,4,6,15,19,15']

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2025-01-19 18:50:03

的值

string = 'SDFJNKSJDFOSN'
count = 0
for letter in string:
    print(f'{ord(letter)=}')

print('\nOr outputs as a list\n')

letter_codes = []
for letter in string:
    letter_codes.append(ord(letter))
print(letter_codes)

使用 ord(character) 获取每个字母输出

ord(letter)=83
ord(letter)=68
ord(letter)=70
ord(letter)=74
ord(letter)=78
ord(letter)=75
ord(letter)=83
ord(letter)=74
ord(letter)=68
ord(letter)=70
ord(letter)=79
ord(letter)=83
ord(letter)=78

Or outputs as a list

[83, 68, 70, 74, 78, 75, 83, 74, 68, 70, 79, 83

Use ord(character) to get the value for each letter

string = 'SDFJNKSJDFOSN'
count = 0
for letter in string:
    print(f'{ord(letter)=}')

print('\nOr outputs as a list\n')

letter_codes = []
for letter in string:
    letter_codes.append(ord(letter))
print(letter_codes)

output

ord(letter)=83
ord(letter)=68
ord(letter)=70
ord(letter)=74
ord(letter)=78
ord(letter)=75
ord(letter)=83
ord(letter)=74
ord(letter)=68
ord(letter)=70
ord(letter)=79
ord(letter)=83
ord(letter)=78

Or outputs as a list

[83, 68, 70, 74, 78, 75, 83, 74, 68, 70, 79, 83
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文