How do I decode dB using Python pycryptodome?

Asked 1 years ago, Updated 1 years ago, 90 views

I'm asking you because I'm curious about how to use it in practice.

try:
    cursor.execute("""create table test(
    id serial primary key, 
    plainData varchar(10), 
    cihperData varchar(255),
    nonce varchar(255)
    )""")
    conn.commit()
except:
    conn.rollback()

key = b'0000000000000000'

for i in range(10):
    randomValue = str(random.randint(1, 10000))
    plain_data = randomValue.encode("utf-8")
    cipher = AES.new(key, AES.MODE_EAX)
    nonce = cipher.nonce
    cipherdata = cipher.encrypt(plain_data)
    # # print(cipherdata)
    cursor.execute("insert into test(plainData, cihperData, nonce) values(%s, %s, %s)", (randomValue, cipherdata, nonce))
    conn.commit()

cursor.execute("select * from test")
serchDB = cursor.fetchone()
strTobyteCipherData = bytes(str(serchDB[2]).replace('\\',''), "utf-8")
strTobyteNonce = bytes(str(serchDB[3]).replace('\\', ''), "utf-8")
# # print(serchDB, strTobyteCipherData, strTobyteNonce)

cipher = AES.new(key, AES.MODE_EAX, strTobyteNonce)
# # print(cipher)
verify = cipher.verify(cipher) #Error Occurred
de_cihperdata = cipher.decrypt(serchDB[2]).decode("utf-8")
# # # print(de_cihperdata)

encryption python postgresql

2022-09-30 01:00

1 Answers

Just one thing.

In Python, b'\xe8\xc1\xf2\xd9' is 4-byte data.

>>> bbb = b'\xe8\xc1\xf2\xd9'
>>> bbb
b'\xe8\xc1\xf2\xd9'
>>> print(bbb)
b'\xe8\xc1\xf2\xd9'
>>> for b in bbb: print(b)


232
193
242
217
>>> for b in bbb: print(hex(b))


0xe8
0xc1
0xf2
0xd9


2022-09-30 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.