这是我发布到stackoverflow的第一个问题,感谢所有帮助/批评/协助...我需要我所能获得的所有帮助。
我对编程非常陌生。
目标是创建一个凯撒密码,该密码可以对输入到ord的用户字符串进行加密和解密,添加用户输入offset_value
,然后将其更改回char。
我正在使用ASCII字符。问题是我需要将加密和解密隔离为ASCII 32('a')-ASCII 126('〜')。我不确定如何创建一个可以回溯94个字符的函数。
因此,例如,如果char是'Z',即ASCII ord 94,如果我们添加用户输入offset_value
,它可以是90,则将使ord184。超出范围。
这导致了真正的问题,即蛮力加密。它的工作原理它需要显示offset_value
在1到94之间变化的所有可能结果。例如,如果我们解密offset_value
为x的每个字母(x是1-94中的任意数字)会发生什么。
相反,它一直在上升。
这有什么道理吗?
我的代码在下面。我知道我还没有创建任何函数,但是我会的。
谢谢大家。
choice = 0
list1 = [1, 2, 3, 4]
list2 = list(range(1, 95))
new_ord = 0
index = 1
encryption = ''
decryption = ''
offset_value = 1
#while loop allows for multiple use, option 4 ends loop
while choice != 4:
print('*** Menu ***')
print('\r')
print('1. Encrypt string')
print('2. Decrypt string')
print('3. Brute force decryption')
print('4. Quit')
print('\r')
choice = int(input('What would you like to do [1,2,3,4]? '))
#invalid user input loop, valid entry ends loop
while choice not in list1:
print('\r')
print('Invalid choice, please enter either 1, 2, 3 or 4.')
print('\r')
choice = int(input('What would you like to do [1,2,3,4]? '))
#user chooses 'encrypt string', stores data
if choice == 1:
print('\r')
string_to_encrypt = str(input('Please enter string to encrypt: '))
offset_value = int(input('Please enter offset value (1 to 94): '))
#invalid user input loop, valid entry ends loop
while offset_value not in list2:
offset_value = int(input('Please enter offset value (1 to 94): '))
#encryption loop for length of string_to_encrypt
for letter in string_to_encrypt:
encryption = encryption + chr((ord(letter) + offset_value))
#prints encrypted string
print('\r')
print('Encrypted string:')
print(encryption)
print('\r')
#clears ecryption data
encryption = ''
#user chooses 'decrypt string', stores data
elif choice == 2:
print('\r')
string_to_decrypt = str(input('Please enter string to decrypt: '))
offset_value = int(input('Please enter offset value (1 to 94): '))
#invalid user input loop, valid entry ends loop
while offset_value not in list2:
offset_value = int(input('Please enter offset value (1 to 94): '))
#decryption loop for length of string_to_decrypt
for letter in string_to_decrypt:
decryption = decryption + chr((ord(letter) - offset_value))
#prints decrypted string
print('\r')
print('Decrypted string:')
print(decryption)
print('\r')
#clears decryption data
decryption = ''
#user chooses 'Brute Force Decryption
elif choice == 3:
string_to_decrypt = str(input('Please enter string to decrypt: '))
for number in range(94):
for letter in string_to_decrypt:
decryption = decryption + chr((ord(letter) - offset_value))
print('Offset: ', index, '= Decrypted String: ', decryption)
offset_value = offset_value + 1
index = index + 1
decryption = ''
#user chooses 'quit'
print('Goodbye.')
这是我发布到stackoverflow的第一个问题,感谢所有帮助/批评/帮助...我需要我所能获得的所有帮助。我对编程非常陌生。目的是创建一个...
我认为这可能解决了您的问题的实质:
[Caesar密码使用索引从0开始的字母。因此,您需要一些函数将一个从32到126的范围转换为0到126-32 =98。然后您需要执行计算mod 99
已经提供了一些使用ordinal % len(alphabet)
的解决方案。我想提出一个略有不同的解决方案。整个解决方案围绕str.maketrans
展开,可以为实现密码提供一种简单的方法。
如果您的目标是了解如何旋转字母字符,则可能会发现以下程序会有所帮助: