欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python加密主機文件幾種方法實現(xiàn)

 更新時間:2023年02月09日 15:25:25   作者:一青一檸  
本文主要介紹了使用python加密主機文件幾種方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

數(shù)據(jù)加密是一種保護數(shù)據(jù)安全的技術(shù),通過對數(shù)據(jù)進行編碼,使得未經(jīng)授權(quán)的用戶無法讀取或改動數(shù)據(jù)。

加密是通過使用加密算法和密鑰實現(xiàn)的。加密算法是一種特定的算法,可以將數(shù)據(jù)編碼成不可讀的形式,密鑰是一串字符串,用于控制加密和解密的過程。加密后的數(shù)據(jù)只能使用對應(yīng)的密鑰解密。

加密在保護數(shù)據(jù)的安全性方面非常重要,尤其是在互聯(lián)網(wǎng)和移動設(shè)備環(huán)境中,用戶的隱私數(shù)據(jù)容易遭到黑客的攻擊。加密技術(shù)不僅可以保護數(shù)據(jù)的隱私,還可以保護數(shù)據(jù)的完整性,確保數(shù)據(jù)不被篡改。

在企業(yè)環(huán)境中,數(shù)據(jù)加密也被廣泛應(yīng)用于保護公司數(shù)據(jù)的安全。從合同到秘密文件,加密技術(shù)可以保護公司數(shù)據(jù)不被未經(jīng)授權(quán)的人員查看。

總的來說,數(shù)據(jù)加密是一種重要的保護數(shù)據(jù)安全的技術(shù),對于任何用戶和企業(yè)都非常重要。

要求:利用python對某個目錄的全部文件進行加密,密鑰由用戶的輸入的字符串經(jīng)過變化而生成,封裝為一個函數(shù),輸入?yún)?shù)有目錄路徑和用戶輸入的字符串。

以下是使用 Python 實現(xiàn)的一種簡單的文件加密方案:

import os
import hashlib

def encrypt_directory(directory, password):
? ? for root, dirs, files in os.walk(directory):
? ? ? ? for file in files:
? ? ? ? ? ? path = os.path.join(root, file)
? ? ? ? ? ? with open(path, 'rb') as f:
? ? ? ? ? ? ? ? data = f.read()
? ? ? ? ? ? key = hashlib.sha256(password.encode()).hexdigest()
? ? ? ? ? ? encrypted_data = bytearray(x ^ ord(key[i % len(key)]) for i, x in enumerate(data))
? ? ? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? ? ? f.write(encrypted_data)

該函數(shù)接收兩個參數(shù):目錄路徑和用戶輸入的字符串。它通過使用 os.walk 對目錄下的每個文件進行加密,并將加密后的數(shù)據(jù)寫回文件。加密使用了異或操作,密鑰是對用戶輸入的字符串進行 SHA-256 哈希的十六進制字符串。

以下是對上面的加密函數(shù)的對應(yīng)解密函數(shù):

scssCopy code
def decrypt_directory(directory, password):
    for root, dirs, files in os.walk(directory):
        for file in files:
            path = os.path.join(root, file)
            with open(path, 'rb') as f:
                encrypted_data = f.read()
            key = hashlib.sha256(password.encode()).hexdigest()
            data = bytearray(x ^ ord(key[i % len(key)]) for i, x in enumerate(encrypted_data))
            with open(path, 'wb') as f:
                f.write(data)

該函數(shù)與上面的加密函數(shù)類似,但是它讀入加密后的文件,并使用相同的密鑰對數(shù)據(jù)進行異或操作,以獲得原始文件。

將上述代碼更換加密算法,使用AES加密,代碼如下:

import os
import hashlib
from Crypto.Cipher import AES

def encrypt_directory(directory, password):
? ? # 計算密鑰
? ? key = hashlib.sha256(password.encode()).digest()[:16]
? ? for root, dirs, files in os.walk(directory):
? ? ? ? for file in files:
? ? ? ? ? ? path = os.path.join(root, file)
? ? ? ? ? ? # 讀取文件
? ? ? ? ? ? with open(path, 'rb') as f:
? ? ? ? ? ? ? ? data = f.read()
? ? ? ? ? ? # 對數(shù)據(jù)進行補位
? ? ? ? ? ? padding_length = 16 - len(data) % 16
? ? ? ? ? ? data += bytes([padding_length] * padding_length)
? ? ? ? ? ? # 初始化加密器
? ? ? ? ? ? cipher = AES.new(key, AES.MODE_ECB)
? ? ? ? ? ? # 加密數(shù)據(jù)
? ? ? ? ? ? encrypted_data = cipher.encrypt(data)
? ? ? ? ? ? # 將加密后的數(shù)據(jù)寫回文件
? ? ? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? ? ? f.write(encrypted_data)

def decrypt_directory(directory, password):
? ? # 計算密鑰
? ? key = hashlib.sha256(password.encode()).digest()[:16]
? ? for root, dirs, files in os.walk(directory):
? ? ? ? for file in files:
? ? ? ? ? ? path = os.path.join(root, file)
? ? ? ? ? ? # 讀取文件
? ? ? ? ? ? with open(path, 'rb') as f:
? ? ? ? ? ? ? ? encrypted_data = f.read()
? ? ? ? ? ? # 初始化解密器
? ? ? ? ? ? cipher = AES.new(key, AES.MODE_ECB)
? ? ? ? ? ? # 解密數(shù)據(jù)
? ? ? ? ? ? data = cipher.decrypt(encrypted_data)
? ? ? ? ? ? # 刪除補位數(shù)據(jù)
? ? ? ? ? ? padding_length = data[-1]
? ? ? ? ? ? data = data[:-padding_length]
? ? ? ? ? ? # 將解密后的數(shù)據(jù)寫回文件
? ? ? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? ? ? f.write(data)

注:上面的代碼僅供參考,不建議在生產(chǎn)環(huán)境中使用。AES ECB 模式并不是很安全,應(yīng)該使用其他模式。

或者使用非對稱加密:

這里使用RSA加密算法實現(xiàn)數(shù)據(jù)的加密解密:

import os
import rsa

def encrypt_file(file_path, public_key_file):
? ? """使用RSA算法加密文件
? ??
? ? 參數(shù):
? ? file_path: 需要加密的文件路徑
? ? public_key_file: 公鑰文件路徑
? ??
? ? 返回值:
? ? 無
? ? """
? ? # 讀取文件內(nèi)容
? ? with open(file_path, "rb") as file:
? ? ? ? file_content = file.read()
? ? # 讀取公鑰
? ? with open(public_key_file, "rb") as key_file:
? ? ? ? public_key = rsa.PublicKey.load_pkcs1(key_file.read())
? ? # 加密文件內(nèi)容
? ? encrypted_content = rsa.encrypt(file_content, public_key)
? ? # 將加密后的內(nèi)容寫入文件
? ? with open(file_path, "wb") as file:
? ? ? ? file.write(encrypted_content)

def decrypt_file(file_path, private_key_file, password):
? ? """使用RSA算法解密文件
? ??
? ? 參數(shù):
? ? file_path: 需要解密的文件路徑
? ? private_key_file: 私鑰文件路徑
? ? password: 私鑰文件密碼
? ??
? ? 返回值:
? ? 無
? ? """
? ? # 讀取文件內(nèi)容
? ? with open(file_path, "rb") as file:
? ? ? ? encrypted_content = file.read()
? ? # 讀取私鑰
? ? with open(private_key_file, "rb") as key_file:
? ? ? ? private_key = rsa.PrivateKey.load_pkcs1(key_file.read(), password)
? ? # 解密文件內(nèi)容
? ? file_content = rsa.decrypt(encrypted_content, private_key)
? ? # 將解密后的內(nèi)容寫入文件
? ? with open(file_path, "wb") as file:
? ? ? ? file.write(file_content)

需要注意的是,RSA加密的效率較低,適用于加密少量數(shù)據(jù),如對文件進行加密

到此這篇關(guān)于使用python加密主機文件幾種方法實現(xiàn)的文章就介紹到這了,更多相關(guān)python加密主機文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python爬蟲分析匯總

    Python爬蟲分析匯總

    這篇文章主要詳細的介紹了Python爬蟲的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到你
    2021-09-09
  • Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼

    Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼

    這篇文章主要介紹了Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Django自定義全局403、404、500錯誤頁面的示例代碼

    Django自定義全局403、404、500錯誤頁面的示例代碼

    這篇文章主要介紹了Django自定義全局403、404、500錯誤頁面的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • pycharm配置Anaconda虛擬環(huán)境全過程

    pycharm配置Anaconda虛擬環(huán)境全過程

    這篇文章主要介紹了pycharm配置Anaconda虛擬環(huán)境全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Python enumerate遍歷數(shù)組示例應(yīng)用

    Python enumerate遍歷數(shù)組示例應(yīng)用

    遍歷數(shù)組的python代碼
    2008-09-09
  • python?隨時間序列變動畫圖的方法

    python?隨時間序列變動畫圖的方法

    這篇文章主要介紹了python?基礎(chǔ)繪圖之關(guān)于隨時間序列變動的圖的畫法,首先大家要明白畫圖需要考慮的問題,如何在圖中適當?shù)娘@示軸標簽的樣式和數(shù)量,詳細代碼跟隨小編一起看看吧
    2022-01-01
  • python里面單雙下劃線的區(qū)別詳解

    python里面單雙下劃線的區(qū)別詳解

    本文主要介紹了python里面單雙下劃線的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • python使用列表的最佳方案

    python使用列表的最佳方案

    這篇文章主要介紹了python使用列表的最佳方式,幫助大家更好的理解和學習python,感興趣的朋友可以了解下
    2020-08-08
  • QT5 Designer 打不開的問題及解決方法

    QT5 Designer 打不開的問題及解決方法

    這篇文章主要介紹了QT5 Designer 打不開的問題及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Python將二維列表list的數(shù)據(jù)輸出(TXT,Excel)

    Python將二維列表list的數(shù)據(jù)輸出(TXT,Excel)

    這篇文章主要介紹了Python將二維列表list的數(shù)據(jù)輸出(TXT,Excel),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04

最新評論