修改过的Vigenere密码在python中-字母 [英] Modified Vigenere Cipher in python - Alphabet
问题描述
这是我必须做的:
在Python中编写脚本,该脚本是Vigenere密码版本的实现英文文本。您的脚本应区分大小写字母(即,加密密钥和明文允许由小写字母和大写字母组成,但密文应为大写字母)。除字母外,纯文本中还将包含其他四个字符:逗号(26),点(27),破折号(28),下划线(29),将加密功能更改为mod 30以下。
Write a script in Python that is an implementation of a version of the Vigenere cipher for English text. Your script should distinguish lowercase and uppercase letters (i.e., the encryption key and the plaintext are allowed to be composed of lowercase and uppercase letters but the ciphertext should be uppercase). In addition to letters, there will be four other characters in the plain text: comma (26), dot (27), dash (28), underscore (29) changing the encryption function to be under mod 30.
您的脚本应从标准输入读取并写入标准输出。它应该提示用户输入大小为k的加密密钥。不允许像标准Vigenere密码一样重复密钥。相反,我们将遵循基于分组密码的思想。基本上,明文和密文将具有与密钥大小相同的大小为k的块。如果密钥长度短于明文,则将前一个块的块大小k的密文连接到密钥。下面是一个示例,其中关键字为k = 10的 Carbondale:
Your script should read from standard input and write to standard output. It should prompt the user for the encryption key of size k. The key is not allowed to be repeated as in the standard Vigenere cipher. Instead, we will follow a block cipher-based idea. Basically, the plaintext and ciphertext will have blocks of size k which is same as the key size. If the key length is shorter than the plaintext, the ciphertext of block size k of the previous block is concatenated to the key. Here is an example when the keyword is "Carbondale" with k = 10:
Plaintext : SIU_CS-Department_is_the_best
Key : CarbondaleUIHAQBBDPTUZ,MUOUCX
Ciphertext: UIHAQBBDPTUZ,MUOUCXHTODQTPYUM
因此,我想知道如何处理多余字符,的部分。 / _。这是进行加密的功能:
So, I want to know how to tackle the part of the extra characters "," "." "/" "_". This is the function that is making the encryption:
a = len(key)
b = len(text)
while (len(key1) <= len(text1)):
for i in range(0,a):
num1 = ord(text1[i+var])%97
num2 = ord(key1[i+var])%97
num3 = num1+num2
if (num3 > 25):
encr.append(num3%25)
else:
encr.append(num3)
i + 1
for i in range(0,a):
encr1.append(chr(encr[i+var]+97))
i + 1
for i in range(0,a):
key1.append(encr1[i+var])
i + 1
var = var + a
推荐答案
您的代码当前存在以下问题(我假设 var = 0
, encr = []
, encr1 = []
, key1 = list(key)
和 text1 = list(text)
发生在您发布的代码之前):
You code currently has the following problems (I am assuming that var = 0
, encr = []
, encr1 = []
, key1 = list(key)
and text1 = list(text)
happen before the code you have posted):
- 您的
while
循环将永远不会开始,否则就不会结束,因为您永远不会缩短text1
(这样做会破坏索引); - 您无需手动为
中的计数器c>循环(如果愿意,
i + 1
如果没有有效分配,则无济于事,您需要i + = 1
); - 使用mod(
%
)时,无需检查是否num3< 25
;和 - 如果要包含它们,请注意,您列出的多余字符与指定的字符不同(
/!=-
)。
如果键的长度比纯文本长,
- Your
while
loop will never start if the key is longer than the plaintext, and never end otherwise, as you never shortentext1
(and doing so would break your indexing); - You don't need to manually increment the counter in a
for
loop (and if you wanted to,i + 1
without assignment effectively does nothing, you needi += 1
); - When using mod (
%
), you don't need to check if e.g.num3 < 25
; and - If you get around to including them, note that the extra characters you list aren't the same as those specified (
"/" != "-"
).
而不是使用 ord
和 chr
,我将构建自己的字母进行循环,例如
Rather than using ord
and chr
, I would build my own alphabet to cycle over, e.g.
from string import ascii_uppercase
alphabet = list(ascii_uppercase) + [",", ".", "-", "_"]
您可能会发现标准Vigenère的此实现很有帮助:
You might find this implementation of the "standard" Vigenère helpful:
from itertools import cycle
def vigenere(plain, key, alphabet):
for p, k in zip(plain, cycle(key)):
index = alphabet.index(p) + alphabet.index(k)
yield alphabet[index % len(alphabet)]
这里是一些伪修改后的实现的代码:
Here is some pseudo code for your modified implementation:
convert all text (plain, key) to upper case
create a list to hold output
split plain into blocks of length len(key)
for each block in plain:
if it's the first block, use key to encode
otherwise, use the last block of encoded text in output
这篇关于修改过的Vigenere密码在python中-字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!