我遇到的问题是我的decrypt_file函数无法解密加密的文本,没有提供错误只是意外返回。
加密函数使用salt和密码加密文本文件,然后将其保存为.enc文件。然后在尝试运行我的decrypt_file函数后,它会识别salt,iv,密码,迭代等,但不会正确解密()。这是代码。干涸。任何指导或帮助将不胜感激。
加密/解密:
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from os import urandom
import hashlib
def key_generation(password, salt, iterations):
assert iterations > 0
print "Salt: " + salt, '\n', "Password: " + password
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
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encryption(message, password, salt, iterations, key_size=256):
key = key_generation(password, salt, iterations)
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
print "Random IV: " + iv
enc = salt + iv + cipher.encrypt(message)
print "Length: " + str(len(enc))
enc = pad(enc)
print "Length: " + str(len(enc))
return enc
def decryption(ciphertext, password, iterations):
salt = ciphertext[0:16]
iv = ciphertext[:AES.block_size]
print len(str(iv))
print len(str(salt))
key = key_generation(password, salt, iterations)
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
print "Plaintext: " + plaintext
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, password, salt, iterations):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
print "Text: " + plaintext
enc = encryption(plaintext, password, salt, iterations)
print "Encrypted Contents: " + enc
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, password, iterations):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
print "Cipher'd Text: " + ciphertext
dec = decryption(ciphertext, password, iterations)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
encrypt_file('HelloWorld.txt', 'password', 'randomsalt', 64000)
decrypt_file('HelloWorld.txt.enc', 'password', 64000)
干运行
加密
Text: }¦—Z“Íd¥çgZw?øÈH™«Nœfra¥)ÊãjnÞª»^}K^Ì„¦ý
×¾Šv“_3w€mG9‚ä¤Å¥žUƯ0£Óy®0²
nrfÖÖ «–‰¯ò
Salt: randomsalt
Password: password
Key: /Îbdヘ5è!ニヒAᆰv=L*øK/ò)Ü
Random IV: eミý1ËÈUÓbIワᄡムl
Length: 138
Length: 144
Encrypted Contents: randomsalteミý1ËÈUÓbIワᄡムl$֭>oリ)<L゙y\I!%wÙßÞlモÊJt$ワ
è Ì-ᄈMᄀ8ヘ!ᄚܩᄃÆ4ÒO9AÃðO.ä3}ヘål{ヌヒ@ÿËzᄋgDᆰ\éUʼaà8タLᅠMupaÔAミマX0ンᄉi3ヨ˧cᄃ;ᆱÛo
解密:
Cipher'd Text: randomsalteミý1ËÈUÓbIワᄡムl$֭>oリ)<L゙y\I!%wÙßÞlモÊJt$ワ
è Ì-ᄈMᄀ8ヘ!ᄚܩᄃÆ4ÒO9AÃðO.ä3}ヘål{ヌヒ@ÿËzᄋgDᆰ\éUʼaà8タLᅠMupaÔAミマX0ンᄉi3ヨ˧cᄃ;ᆱÛo
16
16
Salt: randomsalteミý1Ë
Password: password
Key: 1ÜA !TzxGÑ`wß~|º‹|¡(—ª-%òÇŒÖ
Plaintext: Rネ*SᄊÕñÛ.
t-Îテýト͛'úᄎSタ&2ᆴæマéヌᄏýýᄑtçØÉe?×ûìrモᄈÞcᄎᄇ×_Kメ
ᄎÀ~ä'ᄅ,ᄉ-Èᄀt&gSð:WÕ|
メ^リøᄃ]ノヤÏYvísgl/ᆵレホ*`\ᄚåᄌŴÇlヒÓ!Â`゚
答案 0 :(得分:0)
所以请用十六进制打印出来。然后,您可以清楚地看到在解密期间确实盐和IV重叠。在您的代码中,您还假设盐的长度与给定的不同。
from Crypto import Random
from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from os import urandom
import hashlib
import binascii
def key_generation(password, salt, iterations):
assert iterations > 0
print "Salt: " + tohex(salt), '\n', "Password: " + password
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]
return key
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encryption(message, password, salt, iterations, key_size=256):
print " === ENCRYPTION === "
key = key_generation(password, salt, iterations)
print "Key: " + tohex(key)
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
print "Random IV: " + tohex(iv)
enc = salt + iv + cipher.encrypt(message)
return enc
def decryption(ciphertext, password, iterations):
print " === DECRYPTION === "
salt = ciphertext[0:10]
iv = ciphertext[10:10+AES.block_size]
print "Random IV: " + tohex(iv)
key = key_generation(password, salt, iterations)
print "Key: " + tohex(key)
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[10+AES.block_size:])
print "Plaintext: " + plaintext
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, password, salt, iterations):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
print "Text: " + plaintext
enc = encryption(plaintext, password, salt, iterations)
print "Encrypted Contents: " + enc
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, password, iterations):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
print "Cipher'd Text: " + ciphertext
dec = decryption(ciphertext, password, iterations)
with open(file_name + ".dec", 'wb') as fo:
fo.write(dec)
def tohex(data):
return binascii.hexlify(data)
encrypt_file('HelloWorld.txt', 'password', 'randomsalt', 64000)
decrypt_file('HelloWorld.txt.enc', 'password', 64000)