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

使用python加密主機文件幾種方法實現

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

數據加密是一種保護數據安全的技術,通過對數據進行編碼,使得未經授權的用戶無法讀取或改動數據。

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

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

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

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

要求:利用python對某個目錄的全部文件進行加密,密鑰由用戶的輸入的字符串經過變化而生成,封裝為一個函數,輸入參數有目錄路徑和用戶輸入的字符串。

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

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)

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

以下是對上面的加密函數的對應解密函數:

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)

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

將上述代碼更換加密算法,使用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()
? ? ? ? ? ? # 對數據進行補位
? ? ? ? ? ? padding_length = 16 - len(data) % 16
? ? ? ? ? ? data += bytes([padding_length] * padding_length)
? ? ? ? ? ? # 初始化加密器
? ? ? ? ? ? cipher = AES.new(key, AES.MODE_ECB)
? ? ? ? ? ? # 加密數據
? ? ? ? ? ? encrypted_data = cipher.encrypt(data)
? ? ? ? ? ? # 將加密后的數據寫回文件
? ? ? ? ? ? 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)
? ? ? ? ? ? # 解密數據
? ? ? ? ? ? data = cipher.decrypt(encrypted_data)
? ? ? ? ? ? # 刪除補位數據
? ? ? ? ? ? padding_length = data[-1]
? ? ? ? ? ? data = data[:-padding_length]
? ? ? ? ? ? # 將解密后的數據寫回文件
? ? ? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? ? ? f.write(data)

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

或者使用非對稱加密:

這里使用RSA加密算法實現數據的加密解密:

import os
import rsa

def encrypt_file(file_path, public_key_file):
? ? """使用RSA算法加密文件
? ??
? ? 參數:
? ? file_path: 需要加密的文件路徑
? ? public_key_file: 公鑰文件路徑
? ??
? ? 返回值:
? ? 無
? ? """
? ? # 讀取文件內容
? ? 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())
? ? # 加密文件內容
? ? encrypted_content = rsa.encrypt(file_content, public_key)
? ? # 將加密后的內容寫入文件
? ? with open(file_path, "wb") as file:
? ? ? ? file.write(encrypted_content)

def decrypt_file(file_path, private_key_file, password):
? ? """使用RSA算法解密文件
? ??
? ? 參數:
? ? file_path: 需要解密的文件路徑
? ? private_key_file: 私鑰文件路徑
? ? password: 私鑰文件密碼
? ??
? ? 返回值:
? ? 無
? ? """
? ? # 讀取文件內容
? ? 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)
? ? # 解密文件內容
? ? file_content = rsa.decrypt(encrypted_content, private_key)
? ? # 將解密后的內容寫入文件
? ? with open(file_path, "wb") as file:
? ? ? ? file.write(file_content)

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

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

相關文章

  • Python爬蟲分析匯總

    Python爬蟲分析匯總

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

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

    這篇文章主要介紹了Python3 ID3決策樹判斷申請貸款是否成功的實現代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    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遍歷數組示例應用

    Python enumerate遍歷數組示例應用

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

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

    這篇文章主要介紹了python?基礎繪圖之關于隨時間序列變動的圖的畫法,首先大家要明白畫圖需要考慮的問題,如何在圖中適當的顯示軸標簽的樣式和數量,詳細代碼跟隨小編一起看看吧
    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的數據輸出(TXT,Excel)

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

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

最新評論