python - 数据加密静态加密

标签 python python-3.x python-cryptography

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

代码

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/

相关文章:

python - 如果字典键的值在其他列表中,则从列表中的字典中删除元素

python - 阻止用户输入字母

macos - 在 Big Sur 上安装密码时出现 Pip 错误

python - 如何通过 urls.py 使用 DRF 引发 NotFound 异常

python - 如何使用字符串定义函数名

python - 带有另一列索引的 Pandas 切片字符串

python - 按多个条件对列表进行排序并附加

python - 在 OSX : symbol not found _mysql_affected_rows 上使用 MySQLdb 时出现问题

python - 如何在 Python 密码学中为 ECDSA (secp256k1) 生成较短的私钥

C# RFC2898DeriveBytes 正在工作,但 Python PBKDF2 生成的 key 和 IV 不适用于 Python AES 解密