当前位置: 代码网 > it编程>前端脚本>Python > Python不同版本实现配置文件加密

Python不同版本实现配置文件加密

2025年01月08日 Python 我要评论
aes对称加密aes(advanced encryption standard),在密码学中又称rijndael加密法,是美国采用的一种区块加密标准。这个标准用来替代原先的des,已经被多方分析且广为

aes对称加密

aes(advanced encryption standard),在密码学中又称rijndael加密法,是美国采用的一种区块加密标准。这个标准用来替代原先的des,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(nist)于2001年11月26日发布于fips pub 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

高级加密标准(aes,advanced encryption standard)是密码学中的高级加密标准,aes为分组加密法,把明文分成一组一组的,每组长度相等,每次加密一组数据,直到加密完整个明文,在aes标准规范中,分组长度只能是128位,aes是按照字节进行加密的,也就是说每个分组为16个字节(每个字节8位)。密钥的长度可以使用128位、192位或256位。这导致密钥长度不同,推荐加密的轮数也不同。

1.明文p

没有经过加密的数据

2.密钥k

用来加密明文的密码,在对称加密算法中,加密与解密的密钥是相同的。密钥为接收方与发送方协商产生,但不可以直接在网络上传输,否则会导致密钥泄漏,通常是通过非对称加密算法加密密钥,然后再通过网络传输给对方,或者直接面对面商量密钥。密钥是绝对不可以泄漏的,否则会被攻击者还原密文,窃取机密数据

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 字节的随机密钥
    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 字节的随机密钥
    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 动态修改配置文件模块
  • klippy 读取配置模块

挤出机配置剥离

在打印机初始化加载完配置文件后,只针对挤出机的参数进行赋值,达到弃用配置文件里的参数值

挤出机配置参数赋值实现,可以创建一个常量类(放在util目录下),或者单独创建一个挤出机的ini配置,放在klippy目录下

或者在toolhead模块加载挤出机对象之前进行挤出机配置参数赋值

配置文件参数

extruder 用到的所有参数值信息

到此这篇关于python不同版本实现配置文件加密的文章就介绍到这了,更多相关python配置文件加密内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com