gpt4 book ai didi

python - 数据加密静态加密

转载 作者:行者123 更新时间:2023-12-05 04:43:47 25 4
gpt4 key购买 nike

我的十六进制数字每天都在变化。我怎样才能将它改回静态

代码

from Crypto.Cipher import AES
import pandas as pd
import mysql.connector

myconn = mysql.connector.connect(host="######", user="##", password="######", database="#######")
query = """SELECT * from table """
df = pd.read_sql(query, myconn) #getting hexidigit back from the SQL server after dumping the ecrypted data into the database

def resize_length(string):
#resizes the String to a size divisible by 16 (needed for this Cipher)
return string.rjust((len(string) // 16 + 1) * 16)

def encrypt(url, cipher):
# Converts the string to bytes and encodes them with your Cipher
cipherstring = cipher.encrypt(resize_length(url).encode())
cipherstring = "".join("{:02x}".format(c) for c in cipherstring)
return cipherstring

def decrypt(text, cipher):
# Converts the string to bytes and decodes them with your Cipher
text = bytes.fromhex(text)
original_url = cipher.decrypt(text).decode().lstrip()
return original_url

# It is important to use 2 ciphers with the same information, else the system breaks
# Define the Cipher with your data (Encryption Key and IV)
cipher1 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
cipher2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = df['values'][4]
eypt = encrypt(message, cipher1)
print(decrypt(eypt, cipher2))

我能够在从数据库调用后解密字符串,但在第二天加密的字符串发生变化,这使我的代码失败。我怎么能卡住这个?每天保持一个不变的字符串?

最佳答案

我通过使用 bytes() 加密 key 得到了解决方案使用 byte 方法将 key 存储在安全的配置文件或数据库中,并对字节字符串进行加密以获取 key 。之后使用任何具有合适算法的密码方法对数据进行加密和屏蔽。

关于python - 数据加密静态加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69521408/

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