gpt4 book ai didi

How to encrypt single column in sqlite database in my Python Flask app?(如何在我的应用程序中加密SQLite数据库中的单列?)

转载 作者:bug小助手 更新时间:2023-10-28 10:28:45 30 4
gpt4 key购买 nike



I want one column in my sqlite database to be encrypted but still visible to my Flask app. When I use the solution below - my Flask app is able to see data in the encrypted column, but when I open the database I see that the data is encrypted.

我希望我的SQLite数据库中的一列被加密,但对我的Flaskapp仍然可见。当我使用下面的解决方案时-我的Flaskapp能够看到加密列中的数据,但当我打开数据库时,我看到数据是加密的。


But I'm not sure if my solution is secure to host publicly on pythonanywhere.com. What are the vulnerabilities that I face. Is it safe to keep my 'key' in the code? This is my code below:

但我不确定我的解决方案是否安全,可以在pythonanywhere网站上公开托管。我面临的弱点是什么。把我的“钥匙”保存在密码里安全吗?以下是我的代码:


from flask import Flask
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required
from sqlalchemy_utils import EncryptedType

key = "my_encryption_key_here"

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hello'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)

class Rx(db.Model):
id = db.Column(db.Integer, primary_key=True)
sx_string = db.Column(EncryptedType(db.String, key), nullable=True, unique=False)


更多回答
优秀答案推荐

I'd suggest putting it into a separate file, which is explicitly ignored in your .gitignore file, and then importing it from there. Alternatively, you could use a .env file containing environment variables that you could load in (there's a help page on the PythonAnywhere website that explains how to do that, just search the front page for "environment".

我建议将其放到一个单独的文件中,该文件在.gitignore文件中被明确忽略,然后从那里导入。或者,您也可以使用包含可以加载的环境变量的.env文件(在PythonAnywhere网站上有一个帮助页面解释如何执行此操作,只需在首页搜索“Environment”)。


The reason is that if it's mixed in with the rest of your code, if you have your code in a git repo stored on GitHub or something similar, your encryption key will be on that service -- so in the event of a breach of that service, or if you accidentally make the repo public or something like that, your key will be leaked.

原因是,如果它与你的其余代码混合在一起,如果你的代码存储在GitHub或类似的Git repo中,你的加密密钥将存储在该服务上--所以如果该服务被破坏,或者如果你意外地将repo公开或类似的事情,你的密钥将被泄露。


更多回答

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com