日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python怎样打开加密的文件_如何在Python中解密OpenSSL AES加密的文件?

發(fā)布時間:2025/3/19 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎样打开加密的文件_如何在Python中解密OpenSSL AES加密的文件? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

拉莫斯之舞

我將通過一些更正重新發(fā)布您的代碼(我不想掩蓋您的版本)。當您的代碼正常工作時,它不會檢測到填充周圍的一些錯誤。特別是,如果提供的解密密鑰不正確,則填充邏輯可能會做一些奇怪的事情。如果您同意我的更改,則可以更新您的解決方案。from hashlib import md5from Crypto.Cipher import AESfrom Crypto import Randomdef derive_key_and_iv(password, salt, key_length, iv_length):? ? d = d_i = ''? ? while len(d) < key_length + iv_length:? ? ? ? d_i = md5(d_i + password + salt).digest()? ? ? ? d += d_i? ? return d[:key_length], d[key_length:key_length+iv_length]# This encryption mode is no longer secure by today's standards.# See note in original question above.def obsolete_encrypt(in_file, out_file, password, key_length=32):? ? bs = AES.block_size? ? salt = Random.new().read(bs - len('Salted__'))? ? key, iv = derive_key_and_iv(password, salt, key_length, bs)? ? cipher = AES.new(key, AES.MODE_CBC, iv)? ? out_file.write('Salted__' + salt)? ? finished = False? ? while not finished:? ? ? ? chunk = in_file.read(1024 * bs)? ? ? ? if len(chunk) == 0 or len(chunk) % bs != 0:? ? ? ? ? ? padding_length = bs - (len(chunk) % bs)? ? ? ? ? ? chunk += padding_length * chr(padding_length)? ? ? ? ? ? finished = True? ? ? ? out_file.write(cipher.encrypt(chunk))def decrypt(in_file, out_file, password, key_length=32):? ? bs = AES.block_size? ? salt = in_file.read(bs)[len('Salted__'):]? ? key, iv = derive_key_and_iv(password, salt, key_length, bs)? ? cipher = AES.new(key, AES.MODE_CBC, iv)? ? next_chunk = ''? ? finished = False? ? while not finished:? ? ? ? chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))? ? ? ? if len(next_chunk) == 0:? ? ? ? ? ? padding_length = ord(chunk[-1])? ? ? ? ? ? if padding_length < 1 or padding_length > bs:? ? ? ? ? ? ? ?raise ValueError("bad decrypt pad (%d)" % padding_length)? ? ? ? ? ? # all the pad-bytes must be the same? ? ? ? ? ? if chunk[-padding_length:] != (padding_length * chr(padding_length)):? ? ? ? ? ? ? ?# this is similar to the bad decrypt:evp_enc.c from openssl program? ? ? ? ? ? ? ?raise ValueError("bad decrypt")? ? ? ? ? ? chunk = chunk[:-padding_length]? ? ? ? ? ? finished = True? ? ? ? out_file.write(chunk)

總結

以上是生活随笔為你收集整理的python怎样打开加密的文件_如何在Python中解密OpenSSL AES加密的文件?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。