【问题标题】:Decrypting AES in SQL with key and IV使用密钥和 IV 解密 SQL 中的 AES
【发布时间】:2017-10-03 04:26:46
【问题描述】:

我有一个项目要求,我需要使用 AES 和我提供的密钥/IV(初始化向量)在我的应用程序 (c#.net) 中加密/解密字符串。

很简单。

更令人困惑的地方是,我还要求我还必须能够使用 SQL 解密字符串。这是我开始挣扎的地方。

这是我使用 (c#.net) 来加密我的字符串的代码:

public string Encrypt(string str)
{
    byte[] IV = { 57, 87, 122, 110, 97, 109, 111, 53, 65, 95, 114, 101, 120, 73, 72, 84 };
    string key = "MKE7612Y4ADF8JD1";

    byte[] clearBytes = Encoding.UTF8.GetBytes(str);
    byte[] keyBytes = Encoding.UTF8.GetBytes(key);

    using (Aes encryptor = Aes.Create())
    {
        encryptor.Key = keyBytes;
        encryptor.IV = IV;

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            str = Convert.ToBase64String(ms.ToArray());
        }
    }
    return str;
}

一切都很好:

string encrypted = Encrypt("UAT-6127592");
// returns: 9ZzHICvqQsm/ZI/Uuh8QLQ==

这正是我所期望的。现在到我正在努力解决的部分 - 能够从 SQL 中解密它。

我正在尝试使用如下对称密钥来执行此操作:

USE tempdb;

CREATE SYMMETRIC KEY AES128SecureSymmetricKey 
WITH 
    ALGORITHM = AES_128, -- based on the length of my key
    IDENTITY_VALUE = N'9Wznamo5A_rexIHT',  -- the UTF8 equivalent of the decimal IV entered from my c# code above
    KEY_SOURCE = N'MKE7612Y4ADF8JD1' -- the same key I used in my c# code above
ENCRYPTION BY PASSWORD = N'MKE7612Y4ADF8JD1';

OPEN SYMMETRIC KEY AES128SecureSymmetricKey 
DECRYPTION BY PASSWORD = N'MKE7612Y4ADF8JD1';

DECLARE @strBase64 NVARCHAR(max);
SET @strBase64 = N'9ZzHICvqQsm/ZI/Uuh8QLQ=='; -- for testing, just hard coding the same string I encoded above

-- Converting the base64 string to HEX
DECLARE @strHex VARBINARY(MAX);
SET @strHex = CAST(N'' as xml).value('xs:base64Binary(sql:variable("@strBase64"))', 'varbinary(MAX)');

-- Try to decrypt the value
DECLARE @decrypted_hex_with_key NVARCHAR(MAX);
SET @decrypted_hex_with_key =  DecryptByKey(@strHex);
SELECT CONVERT(NVARCHAR(256), @decrypted_hex_with_key) AS DecryptedValue;

-- close and drop the key
CLOSE SYMMETRIC KEY AES128SecureSymmetricKey;
DROP SYMMETRIC KEY AES128SecureSymmetricKey;

上面的查询结果为“DecryptedValue”返回NULL。

我很确定我在上面的查询中没有正确使用键和 IV 值。我已经尝试过(看起来像)一千种组合,但无法让它完全正常工作。

谁能提供一些关于我做错了什么的见解?

我对密码学世界有点陌生,所以我很可能离这里很远! :)

谢谢你的帮助!!!

【问题讨论】:

    标签: .net sql-server encryption cryptography encryption-symmetric


    【解决方案1】:

    您的代码的问题在于将 SQL Server 中的 IDENTITY_VALUE 和 .NET 中的初始化向量 (IV) 视为相同的事物。他们不是。 IV 是用于随机化密文的标准参数,IDENTITY_VALUE 是 SQL Server 特定的密钥标识符。请参阅我的回答 here 了解更多详情。

    SQL Server 的方法需要包含特定元数据的密文,并且不允许您显式传递 IV。最好的选择是将解密留给应用层。如果你真的需要,你可以尝试在 SQL Server 中使用带有 .NET 代码的 SQLCLR。这听起来是个坏主意,因为您必须以某种方式与 SQL Server 共享主密钥。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多