【问题标题】:Encrypting messages in python在python中加密消息
【发布时间】:2019-04-13 10:46:27
【问题描述】:

在我的这个任务中,我需要根据给定的密钥加密消息。面临的挑战是索引字母表的键,并使更改替换消息中的字母。我到目前为止的代码是:

def encode(key,plaintext):

    for i in key:
        key.index(i)
    for i in plaintext:
        print(plaintext.index(i))

    alpha = ["abcdefghijklmnopqrstuvwxyz"]




def main():

    plaintextMessages = [
        ["This is the plaintext message.",
         "bcdefghijklmnopqrstuvwxyza"],
        ["Let the Wookiee win!",
         "epqomxuagrdwkhnftjizlcbvys"],
        ["Baseball is 90% mental. The other half is physical.\n\t\t- Yogi Berra",
         "hnftjizlcbvysepqomxuagrdwk"],
        ["I used to think I was indecisive, but now I'm not too sure.",
         "mqncdaigyhkxflujzervptobws"],
        ["Einstein's equation 'e = mc squared' shows that mass and\n\t\tenergy are interchangeable.",
         "bludcmhojaifxrkzenpsgqtywv"] ]

    codedMessages = [
        ["Uijt jt uif dpefe nfttbhf.",
         "bcdefghijklmnopqrstuvwxyza"],
        ["Qnhxgomhqm gi 10% bnjd eho 90% omwlignh. - Zghe Xmy",
         "epqomxuagrdwkhnftjizlcbvys"],
        ["Ulj njxu htgcfj C'gj jgjm mjfjcgjt cx, 'Ep pej jyxj veprx rlhu\n\t\t uljw'mj tpcez jculjm'. - Mcfvw Zjmghcx",
         "hnftjizlcbvysepqomxuagrdwk"],
        ["M 2-wdme uxc yr kylc ua xykd m qxdlcde, qpv wup cul'v gmtd mlw\n\t\t vuj aue yv. - Hdeew Rdyladxc",
         "mqncdaigyhkxflujzervptobws"] ]

    for i in plaintextMessages:
        encode(i[1],i[0])


main()

我可以获得密钥的索引和消息,但我不知道如何让这些东西相互作用或相互影响。

【问题讨论】:

  • 看看,ord 和它的逆 chr,也许还有 zipdict
  • 实际上自己解决问题的尝试是 0 次!

标签: python string list indexing cryptography


【解决方案1】:

如果密钥应该按其数字移动明文(例如,b 将移动 2),那么 ordchr 函数将有所帮助。你可以循环遍历明文,使用ord查找字母的编号,然后移位,使用chr查找新字母。

例如,

keyAscii = ord(key[i]) - 96 #I'm assuming all keys are like the above and lowercase
textAscii = ord(plaintext[i]) + keyAscii

if((textAscii > 90 and < 97) or (textAscii > 122)):   #This part is to keep it as letters
    textAscii -= 26

要解码编码的消息,您可以做相反的事情,并减去 ASCII 值。

【讨论】:

    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 2021-07-04
    • 2020-08-05
    • 2021-07-22
    • 2013-11-14
    • 1970-01-01
    • 2022-11-12
    • 2020-05-07
    相关资源
    最近更新 更多