Python不同版本實現配置文件加密
AES對稱加密
AES(Advanced Encryption Standard),在密碼學中又稱Rijndael加密法,是美國采用的一種區(qū)塊加密標準。這個標準用來替代原先的DES,已經被多方分析且廣為全世界所使用。經過五年的甄選流程,高級加密標準由美國國家標準與技術研究院(NIST)于2001年11月26日發(fā)布于FIPS PUB 197,并在2002年5月26日成為有效的標準。2006年,高級加密標準已然成為對稱密鑰加密中最流行的算法之一。
高級加密標準(AES,Advanced Encryption Standard)是密碼學中的高級加密標準,AES為分組加密法,把明文分成一組一組的,每組長度相等,每次加密一組數據,直到加密完整個明文,在AES標準規(guī)范中,分組長度只能是128位,AES是按照字節(jié)進行加密的,也就是說每個分組為16個字節(jié)(每個字節(jié)8位)。密鑰的長度可以使用128位、192位或256位。這導致密鑰長度不同,推薦加密的輪數也不同。
1.明文P
沒有經過加密的數據
2.密鑰K
用來加密明文的密碼,在對稱加密算法中,加密與解密的密鑰是相同的。密鑰為接收方與發(fā)送方協商產生,但不可以直接在網絡上傳輸,否則會導致密鑰泄漏,通常是通過非對稱加密算法加密密鑰,然后再通過網絡傳輸給對方,或者直接面對面商量密鑰。密鑰是絕對不可以泄漏的,否則會被攻擊者還原密文,竊取機密數據
3.AES加密函數
設AES加密函數為E,則 C = E(K, P),其中P為明文,K為密鑰,C為密文。也就是說,把明文P和密鑰K作為加密函數的參數輸入,則加密函數E會輸出密文C
4.密文C
經加密函數處理后的數據
5.AES解密函數
設AES解密函數為D,則 P = D(K, C),其中C為密文,K為密鑰,P為明文。也就是說,把密文C和密鑰K作為解密函數的參數輸入,則解密函數會輸出明文P
Python Crypto 加密庫
# python不同版本不一樣 pip install pycryptodome (python3.7.3) pip install cryptography (python3.12.2)
python3.7.3 版本
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
def encrypt_config(config_file, encrypted_file):
"""加密配置文件"""
# 生成一個 16 字節(jié)的隨機密鑰
key = get_random_bytes(16)
print("Key: ", key)
# 讀取配置文件
with open(config_file, 'rb') as f:
plaintext = f.read()
# 加密
cipher = AES.new(key, AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
with open(encrypted_file, 'wb') as f:
f.write(cipher.iv)
f.write(ciphertext)
print(f'File encrypted: {encrypted_file}')
def encrypt_config_one_key(config_files, encrypted_files):
"""加密配置文件,多個文件用同一個密鑰"""
if not isinstance(config_files, list) or not isinstance(encrypted_files, list):
raise TypeError('config_files and encrypted_files must be list')
# 生成一個 16 字節(jié)的隨機密鑰
key = get_random_bytes(16)
print("Key: ", key)
config_add_key(config_files[0], encrypted_files[0], key)
config_add_key(config_files[1], encrypted_files[1], key)
return
def config_add_key(config_file, encrypted_file, key):
# 讀取配置文件
with open(config_file, 'rb') as f:
plaintext = f.read()
# 加密
cipher = AES.new(key, AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
with open(encrypted_file, 'wb') as f:
f.write(cipher.iv)
f.write(ciphertext)
print(f'File encrypted: {encrypted_file}')
def decrypt_config(encrypted_file, key):
"""解密配置文件"""
# 解密
with open(encrypted_file, 'rb') as f:
iv = f.read(16)
encrypted_data = f.read()
cipher = AES.new(key, AES.MODE_CBC, iv)
config_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)
print(f'解密后的數據: {config_data.decode()}')
if __name__ == '__main__':
# file1 = "/home/pi/printer_data/config/printer.cfg"
# target1 = "/home/pi/printer_data/config/test/printer_test.cfg"
# # encrypt_config(file1, target1)
# key1 = b'.)7[\x8c\xab\x12Y\xd6tm\x06\xe2\xd1l\xb5'
# key2 = b'\xf9\x19Uf\x11\xe9q\x0ci\x87\xe32%\xf8:W'
# # decrypt_config(target1, key1)
# decrypt_config(target2, key2)python3.12.2 版本
import pickle
import configparser
from cryptography.fernet import Fernet
import os
def cfg2bin(file, result_file):
# 讀取配置文件
config = configparser.ConfigParser()
config.read(file)
# 將配置文件對象序列化為二進制文件
with open(result_file, 'wb') as f:
pickle.dump(config, f)
def encrypt_config(config_file, encrypted_file):
"""加密配置文件"""
# 生成密鑰
key = Fernet.generate_key()
print("Key: ", key)
# 讀取配置文件內容
with open(config_file, 'r') as f:
config_data = f.read()
# 使用密鑰加密配置數據
fernet = Fernet(key)
encrypted_data = fernet.encrypt(config_data.encode())
# 寫入加密后的文件
with open(encrypted_file, 'wb') as f:
f.write(encrypted_data)
print(f'配置文件已加密并保存為: {encrypted_file}')
def decrypt_config(encrypted_file, key):
"""解密配置文件"""
# 讀取加密后的文件
with open(encrypted_file, 'rb') as f:
encrypted_data = f.read()
# 使用密鑰解密配置數據
fernet = Fernet(key)
config_data = fernet.decrypt(encrypted_data).decode()
print(f'解密后的數據: {config_data}')
if __name__ == '__main__':
file1 = "/home/cln/printer_data/config/printer.cfg"
file2 = "/home/cln/printer_data/config/mainsail.cfg"
target1 = "/home/cln/printer_data/config/test/printer_test.cfg"
target2 = "/home/cln/printer_data/config/test/mainsail.cfg"
# encrypt_config(file1, target1)
key1 = b'FN8eGSYuXtK2K7-X2jUcm6r1FK16PAzdTMOpg6wyLGQ='
decrypt_config(target1, key1)涉及模塊
- moonraker 動態(tài)修改配置文件模塊
- klippy 讀取配置模塊
擠出機配置剝離
在打印機初始化加載完配置文件后,只針對擠出機的參數進行賦值,達到棄用配置文件里的參數值
擠出機配置參數賦值實現,可以創(chuàng)建一個常量類(放在util目錄下),或者單獨創(chuàng)建一個擠出機的ini配置,放在klippy目錄下
或者在toolhead模塊加載擠出機對象之前進行擠出機配置參數賦值
配置文件參數
extruder 用到的所有參數值信息
到此這篇關于Python不同版本實現配置文件加密的文章就介紹到這了,更多相關Python配置文件加密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何用python腳本實現一次獲取token,多次使用token
這篇文章主要介紹了如何用python腳本實現一次獲取token,多次使用token問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Python利用pandas和matplotlib實現繪制雙柱狀圖
在數據分析和可視化中,常用的一種圖形類型是柱狀圖,柱狀圖能夠清晰地展示不同分類變量的數值,并支持多組數據進行對比,本篇文章將介紹python如何使用pandas和matplotlib繪制雙柱狀圖,需要的可以參考下2023-11-11

