简体   繁体   English

python中的加法密码系统

[英]Additive Cipher system in python

So I am fairly new in python and I try to work on some programs for practice.所以我对python相当陌生,我尝试编写一些程序进行练习。 Today I started working on an additive cipher system about which I learned in a book.今天我开始研究我在书中学到的加法密码系统。 I wrote the code(it's pretty shitty, to be honest🤣) and I tried some inputs.我写了代码(老实说它很糟糕🤣)并尝试了一些输入。 It did well when I entered words with 2 characters but when I tried to encrypt the word "hello" (with shift = 3) for some reason it returns "khuuu" instead of "khoor".当我输入 2 个字符的单词时它表现良好,但是当我出于某种原因尝试加密单词“hello”(shift = 3)时,它返回“khuuu”而不是“khoor”。 Anyone that has any idea please reply.任何有任何想法的人请回复。 Thank you!!谢谢!!

plain = input("Enter your plaintext in lowercase: ")

key = int(input("Enter your cipher key: "))
alphabet = "abcdefghijklmnopqrstuvwxyz"

for x in range(0, len(plain)):
 if plain[x] == "a":
    plain = plain.replace(plain[x] , alphabet[0 + int(key)])
 elif plain[x] == "b":
    plain = plain.replace(plain[x] , alphabet[1 + int(key)])
 elif plain[x] == "c":
    plain = plain.replace(plain[x] , alphabet[2 + int(key)])
 elif plain[x] == "d":
    plain = plain.replace(plain[x] , alphabet[3 + int(key)])
 elif plain[x] == "e":
    plain = plain.replace(plain[x] , alphabet[4 + int(key)])
 elif plain[x] == "f":
    plain = plain.replace(plain[x] , alphabet[5 + int(key)])
 elif plain[x] == "g":
    plain = plain.replace(plain[x] , alphabet[6 + int(key)])
 elif plain[x] == "h":
    plain = plain.replace(plain[x] , alphabet[7 + int(key)])
 elif plain[x] == "i":
    plain = plain.replace(plain[x] , alphabet[8 + int(key)])
 elif plain[x] == "j":
    plain = plain.replace(plain[x] , alphabet[9 + int(key)])
 elif plain[x] == "k":
    plain = plain.replace(plain[x] , alphabet[10 + int(key)])
 elif plain[x] == "l":
    plain = plain.replace(plain[x] , alphabet[11 + int(key)])
 elif plain[x] == "m":
    plain = plain.replace(plain[x] , alphabet[12 + int(key)])
 elif plain[x] == "n":
    plain = plain.replace(plain[x] , alphabet[13 + int(key)])
 elif plain[x] == "o":
    plain = plain.replace(plain[x] , alphabet[14 + int(key)])
 elif plain[x] == "p":
    plain = plain.replace(plain[x] , alphabet[15 + int(key)])
 elif plain[x] == "q":
    plain = plain.replace(plain[x] , alphabet[16 + int(key)])
 elif plain[x] == "r":
    plain = plain.replace(plain[x] , alphabet[17 + int(key)])
 elif plain[x] == "s":
    plain = plain.replace(plain[x] , alphabet[18 + int(key)])
 elif plain[x] == "t":
    plain = plain.replace(plain[x] , alphabet[19 + int(key)])
 elif plain[x] == "u":
    plain = plain.replace(plain[x] , alphabet[20 + int(key)])
 elif plain[x] == "v":
    plain = plain.replace(plain[x] , alphabet[21 + int(key)])
 elif plain[x] == "w":
    plain = plain.replace(plain[x] , alphabet[22 + int(key)])
 elif plain[x] == "x":
    plain = plain.replace(plain[x] , alphabet[23 + int(key)])
 elif plain[x] == "y":
    plain = plain.replace(plain[x] , alphabet[24 + int(key)])
 elif plain[x] == "z":
    plain = plain.replace(plain[x] , alphabet[25 + int(key)])


print (plain)

replace replaces ALL instances of the first argument with the second argument. replace用第二个参数替换第一个参数的所有实例。 If we go through this in steps, we can see what's really happening.如果我们分步进行,我们可以看到真正发生的事情。 This is a fantastic opportunity for you to learn how to use your debugger to step through the code.这是您学习如何使用调试器单步调试代码的绝佳机会。 This example is easy, but future projects might be too difficult to trace down a problem like this.这个例子很简单,但未来的项目可能很难追踪这样的问题。 Knowing how to use a debugger is a very valuable skill.知道如何使用调试器是一项非常宝贵的技能。

The v indicates where your for loop is at at that step v 表示您的 for 循环在该步骤中的位置

1. Original: plain = hello 
                             v
2. Replace h with k: plain = kello
                              v
3. Replace e with h: plain = khllo
                               v
4. Replace l with o: plain = khooo <-- Because you replaced ALL l's with o's.
                                v
5. Replace o with r: plain = khrrr <-- Again, because you replaced ALL o's with r's.
                                 v
6. Replace r with u: plain = khuuu <--- Final result

So how do you fix it?那你怎么解决呢? Since strings are immutable, you can't change them in place... but you can change lists!由于字符串是不可变的,您不能就地更改它们……但是您可以更改列表! And as a bonus, you can iterate over lists just like you do with strings.作为奖励,您可以像处理字符串一样遍历列表。

plain = list(input("Enter your plaintext in lowercase: "))
print(plain) # <--- It's a list

key = int(input("Enter your cipher key: "))
alphabet = "abcdefghijklmnopqrstuvwxyz"

for x in range(len(plain)):
  # Find the index of the letter in alphabet
  ix = alphabet.index(plain[x].lower())
  plain[x] = alphabet[(ix + key)%26]

# Turn plain back into a string
plain = "".join(plain)
print(plain)

This algorithm is kind of expensive, since you have to find the index of each letter at each iteration but I wanted to keep it in the style as you had.这个算法有点昂贵,因为你必须在每次迭代时找到每个字母的索引,但我想保持它的风格。

If you want to improve it, take a look at Jean's comment about using ord and chr so you can skip the index lookup operation.如果您想改进它,请查看 Jean 关于使用ordchr的评论,以便您可以跳过索引查找操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM