【问题标题】:my decrypting program is doing the wrong thing我的解密程序做错了
【发布时间】:2017-11-14 15:04:55
【问题描述】:

今天我制作了一个 .py 文件,该文件可以解密使用 vigenere 正方形加密的字符串。我已经做到了这一点,但我似乎无法在密码列表和 encr_txt 中添加空格,因为它会使解密的消息出现乱码。而不是“消息是,你好,我的名字是苗条的阴影”,你会得到“消息是,hellprvmwhwebwrw k d thady”,就像我在 encr_txt 和密码列表中留下空格一样,我得到了一个很好的消息。我不知道如何解决这个问题也没有错误,我几天前才开始在 python 中编码,所以如果很明显我很抱歉。我也知道这可以更容易地完成,但我正在学习列表,所以我选择这样做而不是这样:

<a href="https://stackoverflow.com/questions/35711747/ascii-vigenere-cipher-not-decrypting-properly">Another question i found relating my problem but does not describe my situation</a>

代码:

# -*- coding: utf-8 -*-
# ^ encoding

# Encrypted text
# encr_txt = 'tkedobaxoudqrrffhhhalbmmcnedeo'
encr_txt = 'qexpg vy zeen ie wdrm elsmy'
#encr_list = list(encr_txt)
txtpos = 0
# Key to ^
key = 'james'
keypos = 0

limit = len(encr_txt)
limitpos = 0
# Vigenere square
ciphr = ['abcdefghijklmnopqrstuvwxyz ',
         'bcdefghijklmnopqrstuvwxyz a',
         'cdefghijklmnopqrstuvwxyz ab',
         'defghijklmnopqrstuvwxyz abc',
         'efghijklmnopqrstuvwxyz abcd',
         'fghijklmnopqrstuvwxyz abcde',
         'ghijklmnopqrstuvwxyz abcdef',
         'hijklmnopqrstuvwxyz abcdefg',
         'ijklmnopqrstuvwxyz abcdefgh',
         'jklmnopqrstuvwxyz abcdefghi',
         'klmnopqrstuvwxyz abcdefghij',
         'lmnopqrstuvwxyz abcdefghijk',
         'mnopqrstuvwxyz abcdefghijkl',
         'nopqrstuvwxyz abcdefghijklm',
         'opqrstuvwxyz abcdefghijklmn',
         'pqrstuvwxyz abcdefghijklmno',
         'qrstuvwxyz abcdefghijklmnop',
         'rstuvwxyz abcdefghijklmnopq',
         'stuvwxyz abcdefghijklmnopqr',
         'tuvwxyz abcdefghijklmnopqrs',
         'uvwxyz abcdefghijklmnopqrst',
         'vwxyz abcdefghijklmnopqrstu',
         'wxyz abcdefghijklmnopqrtsuv',
         'xyz abcdefghijklmnopqrtsuvw',
         'yz abcdefghijklmnopqrtsuvwx',
         'z abcdefghijklmnopqrtsuvwxy',
         'abcdefghijklmnopqrtsuvwxyz ']

first = ciphr[0]
string = ''


def start():
    global limitpos
    limitpos += 1
    global keypos
    for i in ciphr:
        if keypos == len(key):
            keypos = 0
        else:
            pass
        if i[0] == key[keypos]:
            #print "%s, %s" % (i[0], i)
            global currenti
            currenti = i
            #print currenti
            finder()
            break
        else:
            pass

def finder():
    global keypos
    global txtpos
    done = False
    position = 0
    while done == False:
        for i in currenti[position]:
            if i == '_':
                pass

            if i == encr_txt[txtpos]:
                global string
                string = string + first[position]
                #print "message is, %s" % string
                keypos += 1
                txtpos += 1

                done = True
                if limitpos == limit:
                    print "message is, %s" % string
                    break
                else:
                    start()
            else:
                position += 1
                pass

start()

【问题讨论】:

    标签: python list encryption


    【解决方案1】:

    在表格中添加空格会改变密码的工作方式。你不能指望做出那种改变而不影响消息的加密和解密方式!

    顺便说一句,表格的最后一行不正确。它与第一行相同,但它应该在第一个位置有空格。

    【讨论】:

    • 知道如何在不混淆信息的情况下添加空格吗?
    • @PizzaCat 你不能。您尝试解密的消息是使用跳过其输入中的空格字符的密码加密的。您必须保留空格才能正确解密!
    • 我想知道我是否可以这样写pastebin.com/LCfhwRys
    猜你喜欢
    • 2021-07-02
    • 2016-06-10
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多