尝试将RSA密钥(元组)插入SQL数据库时出错

时间:2019-02-11 13:53:04

标签: python python-3.x sqlite rsa

我正在学习import datetime import re date_list = ['2019-02-10', '2018-01-13', '2019-02-8',] now = datetime.date.today() for date_ in date_list: match = re.match('.*(\d{4})-(\d{2})-(\d{2}).*', date_) if match: year = match.group(1) month = match.group(2) day = match.group(3) delta = now - datetime.date(int(year), int(month), int(day)) print(delta) ,并尝试将import datetime import re date_list = ['2019-02-10', '2018-01-13', '2019-02-8',] now = datetime.date.today() for date_ in date_list: match = re.match('.*(\d{4})-(\d{2})-(\d{2}).*', date_) if match: year = match.group(1) month = match.group(2) day = match.group(3) delta = now - datetime.date(int(year), int(month), int(day)) dates_range.append(int(delta.days)) days = min(s for s in dates_range) 键插入我创建的一列中,但出现此错误:

sqlite3

这是我的代码:

rsa

我正在使用rsa.newkeys生成密钥,并且它们生成为元组。例如,元组将类似于以下内容:

PublicKey(7993225774562669856453147392958346571937702133919317490212017912216372852796080413531673713173968180340315415460310318908937895213458133041784535151317298739,65537)

我查看了rsa和rsa.newkeys()的文档dpes返回一个元组,但是却收到错误消息,指出它是错误的数据类型。

2 个答案:

答案 0 :(得分:0)

看看您的RSA密钥-它可能是十六进制的(即有数字和字母),因此int无法正常工作。 Int仅用于数字。

我建议使用文本或Blob作为类型。

答案 1 :(得分:0)

pubKey和privKey是类的实例(rsa.key.PublicKey和rsa.key.PrivateKey)

在实例中,您可以将两个值都当作int:

pubKey.n-在您的示例中是7993225774562662666453147392958346571937702133919317490212017912216372852796080413531673713168168180380315440310310318908937895213458133041784535151317298739

pubKey.e-65537

如果仅需要密钥,则将int保存到数据库:

db.execute('create table user (username text, password text, pubKey int, privKey int)')
...
db.execute('insert into user (username, password, pubKey, privKey) values (?, ?, ?, ?)', (username, password, pubKey.n, privKey.n))

如果您需要一个完整的字符串,请转换为str并另存为文本:

db.execute('create table user (username text, password text, pubKey text, privKey text)')
...
db.execute('insert into user (username, password, pubKey, privKey) values (?, ?, ?, ?)', (username, password, str(pubKey), str(privKey)))