【问题标题】:Data Encryption static encrypt数据加密静态加密
【发布时间】:2021-11-29 22:06:29
【问题描述】:

我的hexidigit 每天都在变化。我怎样才能把它改回静态

代码

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))

从数据库调用后,我能够解密字符串,但第二天加密的字符串发生了变化,这导致我的代码失败。我怎样才能冻结这个?每天保持不变的字符串?

【问题讨论】:

  • 这里根本没有更新您的数据库...所以不确定您希望有人如何告诉您如何不更新此代码中的数据库...
  • 我们怎么可能知道您的数据库?
  • @FrankYellin 问题是如何保持加密密钥静态。从数据库中,我得到了存储为hexi 字符串的输入。为简化起见,我将密码转换为hexi 格式并将其存储到数据库中。稍后,在从数据库中取回字符串后,我将其提供给解密()。这里的问题是 hexi 字符串每天都在变化。因此,代码失败。有什么办法让它静态吗?
  • @JoranBeasley 数据库仅用于存储字符串。我没有对字符串应用任何类型的函数或任何东西。整个加密和解密都是使用 python 进行的。问题是我每天都会收到不同的加密字符串。我怎样才能使是静态的?
  • 相同的字符串将始终使用相同的字符串进行解密...因此数据库中的字符串正在更改,或者您用于解密它的密钥正在更改...这些事件均未显示在这段代码中

标签: python python-3.x python-cryptography


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2016-11-01
    • 1970-01-01
    • 2020-07-05
    • 2018-03-13
    • 2019-11-28
    • 2018-08-03
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多