Python Encryption - Unexpected Variable Return

懵懂的女人 提交于 2019-12-11 19:04:07

问题


I am currently having an issue of not being able to decrypt the text provided after encryption. It returns:

Key: ンƚ!*!゙ᆱø}Qd`Dᆱd!Þxͦ}ᄚミᄀ>'U

Unpadded Text: Hello World

Padded Text: Hello World

Salt: h5eE0b814M

Encrypted Text: WxCž~¼!Ò]Cú´=P+

Encrypted Text with Salt: h5eE0b814MWxCž~¼!Ò]Cú´=P+

Key: ンƚ!*!゙ᆱø}Qd`Dᆱd!Þxͦ}ᄚミᄀ>'U

Unencrypted Text: 

Where

Unencrypted Text: 

Should be "Unencypted Text: Hello World"

Two programs are used in this, one a module and a master. You must run the master to run the module. Any adivce or help would be greatly appricated as I have been stuck for a while. Thank you for your time.

Here is the code:

Master.py

import Encryption as encrypt

#Place Holder Variables
SALT_SIZE = 16 
padded_text = ''
ciphertext = ''
key = ''
ciphertext_with_salt = ''
#Adjustable Variables
text = "Hello World"
iterations = 62705
salt = 'h5eE0b814M'
password = 'pause232'

encrypt.key_generation(password, salt, iterations)
encrypt.encryption(text, password, SALT_SIZE, salt, iterations)
encrypt.decryption(ciphertext_with_salt, password, SALT_SIZE, salt, iterations)

Encryption.py

import Crypto.Random
from Crypto.Cipher import AES
import hashlib


#Key Generation(Used in encyption to create cipher)
def key_generation(password, salt, iterations):
    global key
    assert iterations > 0
    key = password + salt #Combines [password] and [salt] to create a [key]
    for i in range(iterations): #Hashes the [key]
        key = hashlib.sha256(key).digest() #Using Sha256 it hashes the [key] based on amount of [iterations]
    print '\nKey: ' + key #Debug Print
    return key

#Text padding function to set text to a incerment of SALT_SIZE
def pad_text(text, SALT_SIZE):
    print '\nUnpadded Text: ' + text #Debug Print
    global padded_text
    extra_bytes = len(text) % SALT_SIZE #Using the length of [text] it counts how many more characters is required to make an incerment of [SALT_SIZE]
    pad_size = SALT_SIZE - extra_bytes #Subtracts the needed bytes from the [SALT_SIZE] and sets [pad_size] as the length of pading needed.
    pad = chr(pad_size) * pad_size #Creates padding for the [text]
    padded_text = text + pad #Adds the [pad] to the [text]
    print '\nPadded Text: ' + padded_text #Debug Print

#Primary Encryption Function(using text and password)
def encryption(text, password, SALT_SIZE, salt, iterations):
    global padded_text
    global ciphertext
    cipher = AES.new(key, AES.MODE_ECB)
    padded_plaintext = pad_text(text, SALT_SIZE)
    ciphertext = cipher.encrypt(padded_text)
    ciphertext_with_salt = salt + ciphertext
    #debug script
    print '\nSalt: ' + salt #Debug Print
    print '\nEncrypted Text: ' + ciphertext #Debug Print
    print '\nEncrypted Text with Salt: ' + ciphertext_with_salt #Debug Print
    return ciphertext_with_salt

#Primary Decryption Function(using the encrypted text and password)
def decryption(ciphertext_with_salt, password, SALT_SIZE, salt, iterations):
    ciphertext_with_salt = ciphertext[SALT_SIZE:]
    key = key_generation(password, salt, iterations)
    cipher = AES.new(key, AES.MODE_ECB)
    unencrypted_text = cipher.decrypt(ciphertext_with_salt)
    print '\nUnencrypted Text: ' + unencrypted_text #Debug Print
    return unencrypted_text

#Code to allow to use as outside module
if __name__ == '__main__':
    key_generation(password, salt, iterations)
    encryption(text, password, SALT_SIZE, salt, iterations)
    decryption(ciphertext_with_salt, password, SALT_SIZE, salt, iterations)

回答1:


You are transferring your ciphertext and key as strings. You should make sure that the bytes that make up the ciphertext and keys stay intact. If you want to transport them using character strings, please use either hexadecimal encoding or base 64. Hexadecimals are easier to read and check for length, base 64 is more efficient.

Note that the ciphertext will contain bytes with any value, including ones that are ASCII control characters (< 0x20) which also contain termination characters such as 0x00.



来源:https://stackoverflow.com/questions/22816617/python-encryption-unexpected-variable-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!