【Python】sqlite加密库pysqlcipher3编译安装步骤

目录

说明

pysqlcipher3是针对Python 3使用的pysqlcipher的一个分支, 尽管仍然维护对Python 2的支持。它仍然处于测试阶段, 尽管这个库包含的最新的代码量很少,并且大量借鉴了核心Python sqlite源代码,同时链接到libsqlcipher。

在执行任何操作之前,您必须传入PRAGMA key

复制代码
 from pysqlcipher3 import dbapi2 as sqlite
 conn = sqlite.connect('test.db')
 c = conn.cursor()
 c.execute("PRAGMA key='password'")
 c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
 c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
 conn.commit()
 c.close()

可以使用hexdump -C test.db验证您的数据库文件是否被加密,机密的二进制字段是

复制代码
  ab 7f 61 7a 33 9d 07 f4  08 68 c9 b0 4f e3 34 60  |..az3....h..O.4`|
  bb 9d 9c 3d 9e ce 69 57  b6 2f 36 c4 fd 13 bd 61  |...=..iW./6....a|
  77 bf e3 1d 65 b5 ea f7  d2 fc 98 31 23 66 a0 1e  |w...e......1#f..|
  a4 4f fa 66 49 36 84 a1  3e 0c 21 98 84 07 eb 07  |.O.fI6..>.!.....|

准备工作

openssl

安装openssl,选择对应版本即可,不要选择Light 版本
https://slproweb.com/products/Win32OpenSSL.html

环境变量增加OPENSSL_CONF

编译sqlite

下载 sqlcipher,编译文件
git clone https://github.com/sqlcipher/sqlcipher.git
nmake /f Makefile.msc

nmake需要在 x64 Native Tools Command Prompt for VS的终端下使用

编译会报错,只要生成sqlite3.h, sqlite3.c文件即可

想完整编译需要修改Makefile.msc再构建,https://youtu.be/SFHGeetZ0po 完整编译视频

将sqlite3.c和sqlite.h复制到根目录下的amalgamation,以及amalgamation\sqlcipher

tcl

magicsplat版本tcl地址为https://www.magicsplat.com/tcl-installer/

setup.py修改

quote_argument

对于python3.8版本

复制代码
def quote_argument(arg):
    quote = '"' if sys.platform != 'win32' else '\\"'
    return quote + arg + quote

对于python3.12版本

复制代码
def quote_argument(arg):
    quote = '"' if sys.platform == 'win32' else '\\"'
    return quote + arg + quote

openssl路径

修改setup.py中对应静态库位置,注意最新版libcrypto.lib的位置

安装

python 复制代码
python setup.py build_amalgamation
python 复制代码
python setup.py install

加密示例代码

借助GPT可以写一个加密sqlite的类

python 复制代码
from pysqlcipher3 import dbapi2 as sqlite3

class EncryptedDatabase:
    def __init__(
        self,
        db_name="standard_e.db",
        new_db_name="standard.db",
    ):
        self.db_name = db_name  ##未加密数据库
        self.new_db_name = new_db_name  ##加密数据库
        self.password = Config.db_code
        if os.path.exists(self.new_db_name):
            os.remove(self.new_db_name)
        # 原始数据库
        latest_tb_name = get_latest_std_table(
            db_name=db_name, encrypt=False
        )  # 替换为你的表名
        tb_names = ["users",latest_tb_name]
        # for table in tb_names:
        #     self.create_table(table)
        self.copy_data()


    def create_table(self, tb_name):
        conn = sqlite3.connect(self.new_db_name)
        cursor = conn.cursor()
        cursor.execute(f"PRAGMA key = '{self.password}'")
        create_table_query = f"""
        CREATE TABLE IF NOT EXISTS {tb_name} (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            category TEXT,
            name TEXT,
            code TEXT UNIQUE,
            effective_date TEXT,
            status TEXT,
            publish_date TEXT
        )
        """
        cursor.execute(create_table_query)
        conn.commit()
        conn.close()

    def copy_data(self):
        conn = sqlite3.connect(self.db_name)
        cursor = conn.cursor()
        cursor.execute(
            f"ATTACH DATABASE '{self.new_db_name}' AS encrypted KEY '{self.password}'"
        )
        cursor.execute(f"SELECT sqlcipher_export('encrypted')")
        cursor.execute(f"DETACH DATABASE encrypted")
        conn.close()

测试

Navicat无法打开加密的数据库

附录

编译了3个wheel文件

wheel下载地址

参考

https://blog.csdn.net/m0_37416991/article/details/130934309

相关推荐
qq59184068512 小时前
uiautomator2自动化安卓手机操作
python
80s77713 小时前
动态代理和静态代理的区别,动态代理怎么提高网络安全性
python
Flynt14 小时前
Java 27 升级实测:默认值动得比新特性多,有个老参数会让 JVM 直接起不来
java·jvm·后端
niucloud-admin14 小时前
JAVA V6 多商户商城 开发文档——job 计划任务开发
java·python·github
小叶肥辉14 小时前
LangChain链和LangGraph图的学习笔记【三】——分别用langchain_openai库和langchain_community库调用大模型
笔记·python·langchain
2401_8734794015 小时前
IP属地为什么有时显示外省?用IP查询工具核查动态分配、运营商出口与GeoIP库
python·tcp/ip·ip
OKkankan17 小时前
LangChain 能力详解!:输出解析、RAG、向量数据库与 Retriever 检索器
数据结构·python·langchain·ai应用
山哥ol18 小时前
【Geany 环境配置与中文乱码解决参考】
python
用户0942485680318 小时前
第10章:OpenJDK异常体系、栈轨迹与错误诊断入门
java·jvm
会飞锦鲤18 小时前
基于 Mask R-CNN 的药片缺陷检测系统
人工智能·pytorch·python·神经网络·resnet-50