- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 Vigenére 密码制作了一个基于菜单的加密工具。到目前为止,该程序会加密空格,我怎样才能让程序跳过空格。
#creating variables to be used
text_in_use = ''
encrypt_key = ''
decrypt_key = ''
#function to encypypt input text
def encrypt(plaintext, key):
keyLength = len(key)
keyAsIntegers = [ord(i) for i in key] #create list with the ASCII value for each charachter in key
plaintextAsIntegers = [ord(i) for i in plaintext] #create list with the ASCII value for each charachter in text
encyptedtext = ''
for i in range(len(plaintextAsIntegers)): #
encryptvalue = (plaintextAsIntegers[i] + keyAsIntegers[i % keyLength]) % 26 #execute encryption or characters according to vigenere definition
encyptedtext += chr(encryptvalue + 65)
return encyptedtext #return the encyptes tex
#function to decrypt the encrypted text
def decrypt(encyptedtext, key):
keyLength = len(key)
keyAsIntegers = [ord(i) for i in key] #create list with the ASCII value for each charachter in key
encryptedTextAsIntegers = [ord(i) for i in encyptedtext] #create list with the ASCII value for each charachter in text
plaintext = ''
for i in range(len(encryptedTextAsIntegers)):
value = (encryptedTextAsIntegers[i] - keyAsIntegers[i % keyLength]) % 26 #decryption of encrypted characters
plaintext += chr(value + 65)
return plaintext #return decrypted text
#check if user input is valid
def check_value(userEntry):
while True:
try: #check if userinput is an integer
userInput = int(input(userEntry))
if userInput not in range(1,6): #check if userinput is in valid range
print("Invalid choice, valid choices are 1-5! Try again! \n")
except ValueError:
print("Invalid choice! Input can't be empty or a string! \n")
print("""1: Input text to work with
2: Print the current text
3: Encrypt the current text
4: Decrypt the current text
5: Exit""")
else:
return userInput #return valid userinput
def menu():
while True:
print("""1: Input text to work with
2: Print the current text
3: Encrypt the current text
4: Decrypt the current text
5: Exit""")
choice = check_value("Enter Choice: ")
if choice == 1: #allows user to input text for use
text_in_use = str(input("Enter Text: ")).upper()
print("Text is set to:",text_in_use,"\n")
elif choice == 2: #prints set text
print("Your text:",text_in_use,"\n")
elif choice == 3: #ask user to set encryptionkey
encrypt_key = str(input("Enter an encryptionkey: ")).upper()
text_in_use = encrypt(text_in_use, encrypt_key)
print("Your text:", text_in_use)
elif choice == 4: #ask user for decryptionkey
decrypt_key = str(input("Enter a the decryptionkey: ")).upper()
text_in_use = decrypt(text_in_use, decrypt_key)
print("Your text:", text_in_use)
elif choice == 5:
exit()
menu()
我希望程序像它已经运行的那样运行,但它应该跳过加密中的空格。
如:
"HELLO MY MAN" --> encryption(key = asd) --> "HWOLG MQ MSQ"
换句话说,加密文本中的空格应该仍然存在。
最佳答案
当明文是“HELLO MY MAN”并且 key 是“asd”时,不确定你是如何得到“HWOLG MQ MSQ”的。我得到了别的东西。
无论如何,也许是这样的:
def encrypt(plaintext, key):
from itertools import cycle
from string import ascii_uppercase as alphabet
offset = ord("A")
key_char = cycle(key)
encrypted_plaintext = ""
for char in plaintext:
# If the current character is any kind of whitespace...
if char.isspace():
# Append it to the final string with no changes.
encrypted_plaintext += char
else:
# The character is not whitespace, so you have to encrypt it.
char_sum = ord(char) + ord(next(key_char))
char_sum_wrapped = char_sum % len(alphabet)
encrypted_plaintext += chr(char_sum_wrapped + offset)
return encrypted_plaintext
如果当前字符是空格,则将其附加到最终字符串而不作任何更改。 str.isspace
如果字符串中的每个字符(当前字符)都是某种空白(空格、制表符、换行符、回车符等),则返回 true。
我尽量避免使用索引和硬编码数字,所以我改变了一些东西。例如,我没有像您那样在执行任何其他操作之前将明文和 key 中的所有字符都转换为整数,而是只转换循环中的字符。顺便说一句,循环也不同——我迭代明文中的字符,而不是执行基于范围的 for 循环,然后将 i
视为当前字符的索引。其余的基本相同(除了 key_char
和 itertools.cycle
东西,请阅读下面我的笔记)。
另一件需要注意的事情是,在这个实现中,key_char
迭代器只会在明文中的当前字符不是空格时前进 - 然而,您可能希望它前进而不管。只是要记住一些事情。
没关系,这似乎是该密码所需的行为。
另外,请注意,您的程序从以下几行开始:
#creating variables to be used
text_in_use = ''
encrypt_key = ''
decrypt_key = ''
它们根本没有贡献,您可以安全地删除它们。
编辑 - 更多信息:
itertools.cycle
是一个函数,给定一个可迭代对象(如字符串或列表),返回一个迭代器,该迭代器产生该可迭代对象中的元素。例如:
>>> from itertools import cycle
>>> char_iterator = cycle("ABC")
>>> next(char_iterator)
'A'
>>> next(char_iterator)
'B'
>>> next(char_iterator)
'C'
>>> next(char_iterator)
'A'
>>> next(char_iterator)
'B'
>>> next(char_iterator)
'C'
>>> next(char_iterator)
'A'
>>>
如您所见,循环无限期地重复。出于这个原因,我选择使用 itertools.cycle
来替换原始代码中的 keyAsIntegers[i % keyLength]
。
string.ascii_uppercase
只是一个由 A-Z 之间的所有大写字母组成的字符串。在我的代码中,我导入了 ascii_uppercase
并在同一行中将其重命名为 alphabet
,但它们是相同的。
>>> from string import ascii_uppercase as alphabet
>>> alphabet
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>>
关于python - 使 Vigenére 加密/解密跳过空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257217/
我有这个代码来查找这个模式:201409250200131738007947036000 - 1,在文本内 final String patternStr = "(\\d{
我正在尝试使用正则表达式清除一些用户输入,以删除 [ 和 ] 并删除任何大于 1 个空格的空格。但我似乎无法实现我想要的效果。这是我第一次使用正则表达式,所以我对如何写出来有点困惑。 (preg_re
我正在尝试构建这个简单的正则表达式来匹配 Java 中的单词+空格,但我在尝试解决它时感到困惑。该网站上有很多类似的示例,但答案大多给出了正则表达式本身,而没有解释它是如何构造的。 我正在寻找的是形成
好吧,我已经阅读了很多建议如何消除多余空间的帖子,但无论出于何种原因,我似乎无法将这些建议应用到我的系统中,所以我在这里寻求您的帮助。 这些是我代码的最后几行: for line in rli
所以我正在我的测试存储上学习网页抓取,但我不确定如何正确地从“sizes”数组中删除空的新行。 const $ = cheerio.load(body) $('div.lis
这个问题已经有答案了: How to prevent invalid characters from being typed into input fields (8 个回答) 已关闭 9 年前。 是
有人知道如何让扫描仪忽略空间吗?我想输入名字和第二个名字,但扫描仪不让我输入,我想保存全名 String name; System.out.print("Enter name: "); name =
这个问题在这里已经有了答案: Make Vim show ALL white spaces as a character (23 个回答) 关闭 8 年前。 VIM(使用 Solarized Dar
我想使用 StreamTokenizer 从 java 文件中提取名称。我已将空格设置为逗号 inputTokenizer.whitespaceChars(',', ','); 但是,
我正在使用此代码逐行读取 txt 文件。 // Open the file that is the first command line parameter FileInputStream fstre
我似乎无法弄清楚我需要的正则表达式。这就是我想要实现的目标: {ANY CHAR} + @javax.persistence.Column(name = "{ANY 30 CHARS}") + {AN
我正在运行 StyleCop(顺便说一句,如果你想提供高质量的代码,我完全推荐它)... 我有这条线 [System.Xml.Serialization.XmlRootAttribute(Namesp
我刚刚更新到 PhpStorm 2016,我突然注意到,每次我按 Ctrl + S 保存文件时,它都会删除我在测试这段代码后按下以继续编写的空格/制表符。 请帮忙,这对我来说很烦人,因为我在每一行代码
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我已经看过几十个关于这个主题的问题和答案,但我仍然无法解决我的问题。 我在我的代码中使用了一个外部 ffmpeg 转换器,我将文件路径作为参数传递,如下所示: OutputPackage oo = c
谁能详细解释一下它们是什么以及它们之间的区别。提前致谢。 最佳答案 转义序列是代表其他内容的字符序列。例如(“\n” = 新行,“\?” = 问号等)。有关更详细的列表,请检查:https://en.
我无法从我的 javascript 文本中删除换行符。这是我正在处理的数据示例: 0: "Christian Pulisic" 1: "↵" 2: "From Wikipedia, the free
我有一个问题 - 我似乎无法从字符串的开头/结尾删除新行/空格。我在正则表达式的开头和结尾使用 \s ,甚至在获取字符串后使用 .trim() ,但无济于事。 public void extractI
我是 php 的新手,我正在尝试将一系列变量添加到 html 超链接中。但是,任何返回空格的变量都会弄乱超链接。 Grants Test
我是一名优秀的程序员,十分优秀!