如何使解密文本显示为仅一行而不是每个字母一个新行?

时间:2019-04-29 18:09:02

标签: python string encryption concatenation

我们有一个课堂项目,我们必须创建自己的密码和哈希算法。我选择在python中使用Xor密码,它可以工作,除了当我打印解密文本时,它会为每个字母或空格打印新行。 所以看起来像这样:

*****Decrypt*****

Enter the encrypted message here: 030a090e450d030c0a4b1216190e0a

Enter the provided key: key

Decrypted Text:  h

Decrypted Text:  ho

Decrypted Text:  hop

Decrypted Text:  hope

Decrypted Text:  hope 

Decrypted Text:  hope t

Decrypted Text:  hope th

Decrypted Text:  hope thi

Decrypted Text:  hope this

Decrypted Text:  hope this 

Decrypted Text:  hope this w

Decrypted Text:  hope this wo

Decrypted Text:  hope this wor

Decrypted Text:  hope this work

Decrypted Text:  hope this works

我只希望最后一行显示为“解密的文本:希望能行。”

这是代码。

def encrypt():
    msg = input("Type your message here: ")
    key = input("Enter your desired key: ")

    encrypt_hex = ""
    key_itr = 0
    for i in range(len(msg)):
        temp = ord(msg[i]) ^ ord(key[key_itr])
        encrypt_hex += hex(temp) [2:].zfill(2)
        key_itr += 1
        if key_itr >= len(key):
            key_itr = 0

    print("Encrypted Text:  {}\n".format(encrypt_hex))

    main()

def decrypt():
    msg = input("Enter the encrypted message here: ")
    key = input("Enter the provided key: ")

    hex_to_text = ""
    for i in range(0, len(msg), 2):
        hex_to_text += bytes.fromhex(msg[i:i+2]).decode('utf-8')

        decrypt_text = ""
        key_itr = 0
        for i in range(len(hex_to_text)):
            temp = ord(hex_to_text[i]) ^ ord(key[key_itr])
            decrypt_text += chr(temp)
            key_itr += 1
            if key_itr >= len(key):
                key_itr = 0
        print("Decrypted Text:  {}\n".format(decrypt_text))

    main()

def hash():
    import hashlib
    msg1 = input("Enter your text here:  ")
    msg1_hash = hashlib.sha512(msg1.encode())
    print("Here's the hash value for this text: {}\n".format(msg1_hash.hexdigest()))

    main()

def check():
    import hashlib
    msg_check = input("Enter hash here: ")
    new_msg = input("Enter text to check here:  ")

    new_msg_hash = hashlib.sha512(new_msg.encode())
    if msg_check == new_msg_hash.hexdigest():
        print("The hasehes match text is unaltered.\n")
    else:
        print("The hashes don't match the text has been altered.\n")

    main()

def main():
    choice = int(input("1:  Encrypt String\n2:  Decrypt String\n3:  Hash SHA512\n4:  Check Hash SHA512\nSelect(1,2,3,4):    "))
    if choice == 1:
        print("*****Encrypt*****")
        encrypt()
    elif choice == 2:
        print("*****Decrypt*****")
        decrypt()
    elif choice == 3:
        print("*****Hash SHA512*****")
        hash()
    elif choice == 4:
        print ("*****Hash Check SHA512*****")
        check()
    else:
        print("This is not a valid selection, please chose 1 or 2.")

if __name__ == "__main__":
    main()```

2 个答案:

答案 0 :(得分:2)

这应该有效

 def decrypt():
    msg = input("Enter the encrypted message here: ")
    key = input("Enter the provided key: ")

    # variable initiation moved to before loop
    decrypt_text = ""
    hex_to_text = ""
    for i in range(0, len(msg), 2):
        hex_to_text += bytes.fromhex(msg[i:i+2]).decode('utf-8')

    key_itr = 0
    # For loop moved out of the previous for loop
    for i in range(len(hex_to_text)):
        temp = ord(hex_to_text[i]) ^ ord(key[key_itr])
        decrypt_text += chr(temp)
        key_itr += 1
        if key_itr >= len(key):
            key_itr = 0
    # print statement moved to after loop
    print("Decrypted Text:  {}\n".format(decrypt_text))

答案 1 :(得分:0)

`def解密():     msg = input(“在此处输入加密的消息:”)     key = input(“输入提供的密钥:”)

decrypt_text = ""
hex_to_text = ""
for i in range(0, len(msg), 2):
    hex_to_text += bytes.fromhex(msg[i:i+2]).decode('utf-8')

    decrypt_text = ""
    key_itr = 0
    for i in range(len(hex_to_text)):
        temp = ord(hex_to_text[i]) ^ ord(key[key_itr])
        decrypt_text += chr(temp)
        key_itr += 1
        if key_itr >= len(key):
            key_itr = 0
print("Decrypted Text:  {}\n".format(decrypt_text))`