0

这是我用来解密一次填充的python程序,但是程序在解码文件的顶部留下了一个空白行,我不知道它为什么会离开该行,但我知道它与如何我使用顶行,我使用顶行存储文件的名称,然后它用于为解密文本命名文件,然后删除顶行,但我不知道如何摆脱文件中的空白行。

import os

q = 1
while q == 1: 
    #opens the cipher text and it converts it to decimal
    cipher = raw_input("cipher text: ")
    cipher1 = open(cipher, "r")
    cipher2 = cipher1.read()
    cipher3 = [ord(c) for c in cipher2]

    #opens the key and coverts it to decimal
    key = raw_input("key: ")
    key1 = open(key, "r")
    key2 = key1.read()
    key3 = [ord(c) for c in key2]

    #subtracts the key from the cipher
    a = cipher3
    b = key3
    c = map(lambda x: (x[0]-x[1]) % 256, zip(a,b))

    #prints out the decrypted plain text
    decrypt = ''.join(map(chr,c))

    string1 = decrypt.index('\n')
    name = decrypt[0:string1]

    #makes a file with the decrypted output
    path1 = raw_input("out folder: ")
    path2 = path1 + "/" + name

    string3 = decrypt.index('\n')
    length = len(decrypt)
    decrypt = decrypt[string1:length]

    if os.path.exists(path2):
        f1 = file(path2, "a")
        f1 = open(path2, "a")
        f1.write(decrypt)
        f1.close()
    else:
        f1 = file(path2, "w")
        f1 = open(path2, "w")
        f1.write(decrypt)
        f1.close()
    print 50*"-"
4

2 回答 2

2

您将 string1 指向行尾。

string1 = decrypt.index('\n')

但是如果这一行,你需要从下一个字符开始切片:

decrypt = decrypt[string1:length]
于 2013-08-02T02:50:55.600 回答
1

改变

decrypt = decrypt[string1:length]

decrypt = decrypt[string1+1:length]
于 2013-08-02T02:44:12.080 回答