Skip to main content
 首页 » 编程设计

python之与凯撒密码的 ASCII 循环作斗争

2024年11月01日8tuyile006

这是我发布到 stackoverflow 的第一个问题,感谢任何和所有的帮助/批评/帮助...我需要我能得到的所有帮助哈哈。

我对编程很陌生。

目的是创建一个凯撒密码,加密和解密用户字符串输入到一个 ord,添加用户输入 offset_value,然后将其更改回 char。

我正在使用 ASCII 字符。问题是我需要将加密和解密隔离为 ASCII 32 ('a') - ASCII 126 ('~')。我不确定如何创建一个循环返回 94 个字符的函数。

因此,例如,如果字符是“Z”,它是 ASCII ord 94,如果我们添加用户输入 offset_value,它可能是 90,这将使 ord 184。这是在外面范围。

这导致了真正的问题,暴力加密。它有效……有点。它需要显示 offset_value 在 1 到 94 之间变化的所有可能结果。例如,如果我们用 x 的 offset_value 解密每个字母会发生什么(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.') 

请您参考如下方法:

我不会为您重写您的程序,而是要描述如何解决这样的问题,这主要涉及做一点数学运算。

您的问题的本质归结为如何将给定范围的整数值偏移给定数量并包装结果,以便它们仍然在原始值范围内。

这可以通过减去范围内的最小值,加上偏移量,计算范围内高值和低值之间的差值 (+1) 的总和,对范围内的每个值进行数学计算,然后然后最后将最小值加回去以补偿最初减去它。关键部分是 modulo % 操作来约束总和。

下面 offset_range() 函数中的代码显示了如何在 list comprehension 中执行所有这些操作这是在 Python 中创建列表的一种简写方式。

那个和 print_range() 函数也用于说明如何在 Python 中定义函数。

def print_range(name, range_): 
    """ Pretty-print given range of character ordinals. """ 
    ints = ', '.join('{:3d}'.format(val) for val in range_) 
    chrs = ', '.join('{:>}'.format(repr(chr(val))) for val in range_) 
    print('{}: [{}]'.format(name, ints)) 
    print('{}  [{}]'.format(len(name)*' ', chrs)) 
 
def offset_range(offset_value, range_): 
    """ Add offset value to integers in given range and wrap result. """ 
    min_val, max_val = range_[0], range_[-1]  # Get upper and lower limits. 
    diff_plus_1 = (max_val - min_val) + 1  # Their difference + 1 for computation. 
    # Use a list comprehension to create new list. 
    offset_range = [(((val - min_val) + offset_value) % diff_plus_1) + min_val 
                        for val in range_] 
    return offset_range 
 
ascii_range = list(range(ord('a'), ord('~')+1)) 
print_range('ascii_range', ascii_range) 
 
print() 
offset_range = offset_range(1, ascii_range) 
print_range('offset_range', offset_range) 

输出:

ascii_range: [ 97,  98,  99, 100, 101, 102, 103, 104, 105, ... 122, 123, 124, 125, 126] 
             ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ... 'z', '{', '|', '}', '~'] 
 
offset_range: [ 98,  99, 100, 101, 102, 103, 104, 105, ... 122, 123, 124, 125, 126,  97] 
              ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ... 'z', '{', '|', '}', '~', 'a']