我目前正在调查Microsoft SQL Server的Always Encrypted功能的使用。我正在尝试使用 pyodbc 将 blob 对象简单地存储在列加密表(“随机”)中。如果代码在用于插入任意二进制对象的非加密列上运行良好,则在加密列上运行相同代码时会失败。更奇怪的是它适用于非图像文件,但每当我尝试上传PDF、JPEG、PNG或类似文件时,它都会失败。
代码看起来像这样。
import pyodbc
server = 'tcp:XXXXX-XXXXXX-XXXXX-XXXXX-XXXXX.windows.net,1433'
database = 'db-encryption'
username = 'XXXXXX@dbs-always-encrypted'
password = 'XXXXXXXXX'
connection_string = [
'DRIVER={ODBC Driver 17 for SQL Server}',
'Server={}'.format(server),
'Database={}'.format(database),
'UID={}'.format(username),
'PWD={}'.format(password),
'Encrypt=yes',
'TrustedConnection=yes',
'ColumnEncryption=Enabled',
'KeyStoreAuthentication=KeyVaultClientSecret',
'KeyStorePrincipalId=XXXXX-XXXXXX-XXXXX-XXXXX-XXXXX',
'KeyStoreSecret=XXXXX-XXXXXX-XXXXX-XXXXX-XXXXX'
]
cnxn = pyodbc.connect( ';'.join(connection_string) )
cursor = cnxn.cursor()
insert = 'insert into Blob (Data) values (?)'
files = ['Text.txt', 'SimplePDF.pdf']
for file in files:
# without hex encode
bindata = None
with open(file, 'rb') as f:
bindata = pyodbc.Binary(f.read())
# insert binary
cursor.execute(insert, bindata)
cnxn.commit()
尝试在加密的“数据”列 (VARBINARY(MAX)) 上运行代码时收到的错误消息如下
pyodbc.DataError: ('22018', "[22018] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Operand type clash: image is incompatible with varbinary(max) encrypted with (encryption_type = 'RANDOMIZED', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_Auto1', column_encryption_key_database_name = 'db-encryption') (206) (SQLExecDirectW)")
似乎驱动程序读取字节并看到它是“已知类型”并将数据视为“图像”
有什么办法可以防止这种情况发生吗?我只是想在所述列中存储任意字节对象。