新的密码问题,如何解码给定的密码? [英] New cipher problem, how to decode given enciphered code?

查看:96
本文介绍了新的密码问题,如何解码给定的密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试解决凯撒密码时,我遇到了一些问题。

  #enciphered message ='I wtvp olel decfnefcpd lyo lwrzctesxd'

plain ='abcdefghijklmnopqrstuvwxyz'

cipher ='lmnopqrstuvwxyzabcdefghijk'
cipher_text = input('输入已加密的消息:')
clean_ '''

for i in密文:

如果i!=:
clean_text = clean_text + plain [plain.index(cipher [(ord [i )-ord('a'))])]
其他:
clean_text = clean_text +

print(clean_text)

上面是我创建的代码,这就是我得到的结果:



< pre class = lang-none prettyprint-override> 输入加密的消息:I wtvp olel decfnefcpd lyo lwrzctesxd

n hega zwpw opnqypqnao wjz whcknepdio

这是我的相关问题:


  1. 为什么解码不正确?就像,应该是我喜欢数据结构和算法


  2. 我也对加密消息开头的大写字母 I感到困惑。


  3. 最后,我不知道如何同时解码大写和小写。我应该怎么做?



解决方案


(1)为什么解码不正确?就像,应该是我喜欢数据结构和算法


首先,字母已经以ASCII形式出现。因此,无需在 plain cipher 中重新定义字母。关键是从纯文本字符到密文字符的偏移量,直接在 z 之后环绕。



所以您通常将字符从0..25转换为字母索引,然后添加(用于加密)或减去(用于解密)密钥,模数为26-字母表的大小。然后,要获得结果,请转换回一个字符。您已经在使用 ord(character)-ord(a)进行索引转换。相反,可以使用 chr


(2)我也是加密的消息开头对大写字母 I感到困惑。您对此有任何见识吗?


嗯,单字符单词有更多的可能性。


(3)最后,我有一个 A 这个词。不知道如何同时解码大写和小写;


最好的方法是创建一个变量,该变量指示大写或小写; is_uppercase 是个好名字。然后将字符转换为小写。执行加密/解密操作,然后根据需要将结果字符转换回大写。这样,您的加密/解密操作根本不会受到影响,并且保持相对简单。


While I am trying to solve Caesar cipher, I faced few problems.

#enciphered message = 'I wtvp olel decfnefcpd lyo lwrzctesxd'

plain = 'abcdefghijklmnopqrstuvwxyz'

cipher = 'lmnopqrstuvwxyzabcdefghijk'
cipher_text = input('Enter enciphered message: ')
clean_text = ' '

for i in cipher_text:

    if i != " ":
        clean_text = clean_text + plain[plain.index(cipher[(ord(i)-ord('a'))])]
    else:
        clean_text = clean_text + " "

print(clean_text)

Above is the code, that I created and this is what I got as a result:

Enter enciphered message: I wtvp olel decfnefcpd lyo lwrzctesxd

 n hega zwpw opnqypqnao wjz whcknepdio

Here are my related questions:

  1. Why wasn't it decoded properly? Like, It should be 'I like data structures and algorithms'

  2. I'm also confused about the capital "I" at the beginning of the enciphered message. Do you have any insight on that?

  3. Finally I have no idea how to decode uppercase and lowercase at the same time; how should I do that?

解决方案

(1) Why wasn't it decoded properly? Like, It should be 'I like data structures and algorithms'

First of all, the alphabet is already present in ASCII. So there is no need to redefine the alphabet in plain or cipher. The key is the offset from the plaintext character to the ciphertext character, wrapping around directly after the z.

So you generally convert from a character to an index in the alphabet from 0..25, then you add (for encryption) or subtract (for decryption) the key, modulo 26 - the size of the alphabet. Then, to get the result, you convert back into a character. You're already doing the conversion to index using ord(character) - ord(a). The opposite can be done using chr.

(2) I'm also confused about the capital "I" at the beginning of the enciphered message. Do you have any insight on that?

Well, there are more possibilities for single character words. The word A would be a prime suspect.

(3) Finally I have no idea how to decode uppercase and lowercase at the same time; how should I do that?

The best way is to create a variable that indicates that something is uppercase or not; is_uppercase would be a good name. Then convert the character to lowercase. Perform the encryption / decryption operation and then convert the resulting character back into uppercase, if required. That way your encryption / decryption operation is not affected at all and kept relatively simple.

这篇关于新的密码问题,如何解码给定的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆