简体   繁体   English

从SQL Server 2008 R2解密数据

[英]Decrypt Data From SQL Server 2008 R2

I need some help translating this procedure (see below) to Entity Framework 4.0. 我需要一些帮助将此过程(见下文)转换为Entity Framework 4.0。 Does anyone have any suggestions of how to port this over. 有没有人有任何关于如何移植它的建议。 The target project includes; 目标项目包括; Silverlight 4, WCF RIA Services, EF 4.0, SQL Server 2008 R2. Silverlight 4,WCF RIA服务,EF 4.0,SQL Server 2008 R2。

The only requirement I have is that it will need to be placed in the managed code and not in a stored procedure. 我唯一的要求是它需要放在托管代码中而不是存储过程中。

    Try 
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        string sql = "OPEN SYMMETRIC KEY " + this._encryptKey;
        sql += " DECRYPTION BY CERTIFICATE " + this._encrpytCert; 
        sql += " SELECT TOP (1) CONVERT(nvarchar(50),DECRYPTBYKEY(Field1)) AS Name";
        sql += " FROM Table3"; 
        sql += " ORDER BY CONVERT(nvarchar(50),DECRYPTBYKEY(Field1))";
        cmd.CommandText = sql;
        Name = (String)cmd.ExecuteScalar();
        bRtn = false;
    }
        catch (Exception ex)
    {
        System.Diagnostics.Debug.Print(ex.ToString());
    }

Please let me know how I should set this up and thanks! 请让我知道如何设置它并谢谢!

You could run the query via the Entity Framework and get strongly typed results by using the ObjectContext.ExecuteStoreQuery<>() function (see this example ). 您可以通过Entity Framework运行查询,并使用ObjectContext.ExecuteStoreQuery<>()函数获取强类型结果(请参阅此示例 )。

Unfortunately, however, I don't think there's any way to get around having to generate the T-SQL statement yourself. 然而不幸的是,我认为没有办法让你自己生成T-SQL语句。 While you can use many of SQL Server's functions in Linq-to-Entities queries via the SqlFuntions class, there is no function that translates SQL Server's DECRYPTBYKEY function, not to mention the fact that the Entity Framework won't generate a statement to open the key. 虽然您可以通过SqlFuntions类在Linq-to-Entities查询中使用许多SQL Server的函数,但是没有函数可以转换SQL Server的DECRYPTBYKEY函数,更不用说实体框架不会生成一个语句来打开键。

To decrypt before querying sensitive data, all you need to do is to use a DbTransaction in the EF ObjectContext connection. 要在查询敏感数据之前解密,您需要做的就是在EF ObjectContext连接中使用DbTransaction。

Ie

  • connection.Open(); connection.Open();
  • connection.BeginTransaction(); connection.BeginTransaction();
  • execute your "OPEN SYMMETRIC KEY..." command or stored procedure with ExecuteStoreCommand execute your sensitive data queries, stored procedures etc. 使用ExecuteStoreCommand执行“OPEN SYMMETRIC KEY ...”命令或存储过程,执行敏感数据查询,存储过程等。
  • Commit or Rollback you transaction if required 如果需要,提交或回滚您的事务

This forces EF to maintain the same db connection because you have started a db transaction and it makes sense because you might execute a whole bunch of SP's as part of the same db transaction. 这会强制EF维护相同的数据库连接,因为您已经启动了数据库事务,这是有意义的,因为您可能会执行一大堆SP作为同一数据库事务的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM