C# 在Dapper.NET中使用varbinary参数

C# 在Dapper.NET中使用varbinary参数,c#,.net,sql-server,dapper,C#,.net,Sql Server,Dapper,我试图在Dapper.NET中使用varbinary参数,如下所示 string secret = "secret"; // from SELECT ENCRYPTBYPASSPHRASE('secret', N'xx') >>; string ciphertext = "0x01000000393FE233AE939CA815AB744DDC39667860B3B630C82F36F7"; using (var conn = new SqlConnection(...)) {

我试图在Dapper.NET中使用
varbinary
参数,如下所示

string secret = "secret";

// from SELECT ENCRYPTBYPASSPHRASE('secret', N'xx') >>;
string ciphertext = "0x01000000393FE233AE939CA815AB744DDC39667860B3B630C82F36F7"; 
using (var conn = new SqlConnection(...))
{
    var result = conn.ExecuteScalar(@"SELECT CONVERT(NVARCHAR(4000), DECRYPTBYPASSPHRASE(@secret, @ciphertext)) as decrypted", 
        new
        {
            secret,
            ciphertext = Encoding.Unicode.GetBytes(ciphertext)
        });
}
但是结果是
null
。但是如果我直接运行SQL,它会返回一个有效的结果

SELECT CONVERT(NVARCHAR(40), DECRYPTBYPASSPHRASE('secret',  0x01000000393FE233AE939CA815AB744DDC39667860B3B630C82F36F7))
返回加密文本
xx


你知道我做错了什么吗?

为了让别人觉得有用,以下内容起了作用(感谢@Rob的评论)

而hexstring to bytes和bytes to hexstring函数是

public static byte[] StringToByteArray(string hex)
{
    int startIndex = 0;
    if (hex.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
        startIndex = 2;

    return Enumerable.Range(startIndex, hex.Length - startIndex)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

public static string ByteArrayToString(byte[] arr)
{
    return "0x" + BitConverter.ToString(arr).Replace("-", String.Empty);
}

Encoding.Unicode.GetBytes
将获取十六进制值的字符串表示形式的字节数组,用于获取
密文的正确值
public static byte[] StringToByteArray(string hex)
{
    int startIndex = 0;
    if (hex.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
        startIndex = 2;

    return Enumerable.Range(startIndex, hex.Length - startIndex)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

public static string ByteArrayToString(byte[] arr)
{
    return "0x" + BitConverter.ToString(arr).Replace("-", String.Empty);
}