Python加密 - 意外的变量返回 [英] Python Encryption - Unexpected Variable Return
问题描述
我目前遇到无法解密加密后提供的文字的问题。它返回:
键:゙ƚøøÞͦͦͦͦͦミㄱ>'U
未填充的文本:Hello World
填充文本:Hello World
盐:h5eE0b814M
加密文本:WxCz〜¼!Ò]Cú'= P +
加密文本与盐:h5eE0b814MWxCž〜¼!Ò]Cú'= P +
键: !゙ㄻø} Qd`Dㄻd!Þx?ͦ}ㅀミㄱ>'U
未加密的文本:
其中
未加密文本:
应为Unencypted Text:Hello World
一个模块和一个主机。您必须运行主机才能运行模块。任何adivce或帮助将是非常抱歉,因为我被困了一段时间。
以下是代码:
Master.py
import加密作为加密
#Place持有人变量
SALT_SIZE = 16
padded_text =''
ciphertext =''
key =''
ciphertext_with_salt =''
#可调整变量
text =Hello World
iterations = 62705
salt ='h5eE0b814M'
password ='pause232'
encrypt.key_generation(password,salt,iterations)
encrypt.encryption密码,SALT_SIZE,salt,iterations)
encrypt.decryption(ciphertext_with_salt,password,SALT_SIZE,salt,iterations)
Encryption.py
import Crypto.Random
。密码导入AES
import hashlib
#Key生成(用于encyption创建密码)
def key_generation(密码,盐,迭代):
全局键
断言迭代> 0
key = password + salt #Combines [password]和[salt]为i创建一个[key]
范围(迭代):#Hashes [key]
key = hashlib .sha256(key).digest()#使用Sha256根据[iterations]的数量冻结[key]
print'\\\
Key:'+ key #Debug打印
返回键
#Text填充函数将文本设置为SALT_SIZE的强制
def pad_text(text,SALT_SIZE):
print'\\\
Unpadded Text:'+ text #Debug打印
全局padded_text
extra_bytes = len(text)%SALT_SIZE#使用[text]的长度计算需要多少字符才能强制执行[SALT_SIZE]
pad_size = SALT_SIZE - extra_bytes #Sbtracts从[SALT_SIZE]中选择所需的字节,并将[pad_size]设置为所需的延迟长度。
pad = chr(pad_size)* pad_size#为[text]创建填充
padded_text = text + pad#将[pad]添加到[text]
print'\\\
Padded Text :'+ padded_text #Debug Print
#Primary加密函数(使用文本和密码)
def加密(文本,密码,SALT_SIZE,盐,迭代):
全局padded_text
全局密文
cipher = AES.new(key,AES.MODE_ECB)
padded_plaintext = pad_text(text,SALT_SIZE)
ciphertext = cipher.encrypt(padded_text)
ciphertext_with_salt = salt +密文
#debug脚本
打印'\\\
Salt:'+ salt #Debug打印
打印'\\\
Encrypted文本:'+ ciphertext #Debug打印
print' \\\
使用Salt加密的文本:'+ ciphertext_with_salt #Debug打印
返回ciphertext_with_salt
#Primary解密函数(使用加密文本和密码)
def解密(ciphertext_with_salt, 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'\\\
Unencrypted Text:'+ unencrypted_text #Debug Print
return unencrypted_text
#Code允许用作外部模块
如果__name__ =='__main__':
key_generation(密码,salt,迭代)
加密(文本,密码,SALT_SIZE,盐,迭代)
解密(ciphertext_with_salt, SALT_SIZE,salt,iterations)
解决方案和键作为字符串。你应该确保组成密文和密钥的字节保持不变。如果你想使用字符串传输它们,请使用十六进制编码或base 64.十六进制更容易阅读和检查长度,base 64更有效。
注意,密文将包含具有任何值的字节,包括是ASCII控制字符(
< 0x20
)的字节,其还包含终止字符0x00
。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)
解决方案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 as0x00
.这篇关于Python加密 - 意外的变量返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!