提问人:cargt4 提问时间:11/6/2023 最后编辑:Svyatoslav Danylivcargt4 更新时间:11/7/2023 访问量:102
我的 SQL 脚本,用于解密由 EF Core 加密的数据
My SQL script to decrypt data encrypted by EF Core
问:
我有一些由 ef core 加密的列:
var keyBase = System.Text.Encoding.UTF8.GetBytes("xxxxxxxxxxxxxxxxxxxxxxxx");
var key = Convert.ToBase64String(keyBase);
_provider = new AesProvider(System.Text.Encoding.UTF8.GetBytes(key));
modelBuilder.UseEncryption(this._provider);
出于测试目的,我想通过sql脚本解密它
SET @keyBase = 'xxxxxxxxxxxxxxxxxxxxxxxx';
SET @encrypted_data = 'some encrypted data from column';
-- Decode the keyBase from Base64 encoding
SET @key = UNHEX(@keyBase);
-- Generate a random IV (Initialization Vector) or use a predetermined one if available
SET @iv = UNHEX(SHA2(UUID(), 128));
-- Decrypt the data using the key and IV
SET @decrypted_data = AES_DECRYPT(FROM_BASE64(@encrypted_data), @key, @iv);
-- Return the decrypted data
SELECT CAST(@decrypted_data AS CHAR) AS decrypted_data;
不幸的是,这将返回 null
答:
1赞
cargt4
11/7/2023
#1
这就是答案
SET @keyBase = 'xxx';
SET @encrypted_data = 'xxx';
SET @@SESSION.block_encryption_mode = 'aes-256-cbc';
-- Convert the keyBase to binary
SET @keyBaseBinary = CONVERT(@keyBase USING utf8);
-- Convert the binary keyBase to Base64
SET @key = TO_BASE64(@keyBaseBinary);
-- Decode the keyBase from Base64 encoding
SET @binary_encrypted_data = FROM_BASE64(@encrypted_data);
-- Extract the first 16 bytes from the decoded data as the IV
SET @iv = SUBSTRING(@binary_encrypted_data, 1, 16);
-- Remove the IV from the binary data
SET @data_without_iv = SUBSTRING(@binary_encrypted_data, 17);
-- Decrypt the data using AES_DECRYPT in MySQL
SELECT CAST(AES_DECRYPT(@data_without_iv, @key, @iv) AS CHAR) AS decrypted_data;
评论
-- Generate a random IV (Initialization Vector)