简体   繁体   English

IQuery NHibernate-是否必须对作为加密IUserType的参数进行加密?

[英]IQuery NHibernate - do I have to Encrypt a parameter that is an encrypted IUserType?

Situation : suppose I have a column on an entity which is encrypted in the database using IUserType: 情况 :假设我有一个关于实体的列,该列已使用IUserType在数据库中加密:

public class EncryptedStringUserType : IUserType
{

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        object r = rs[names[0]];
        if (r == DBNull.Value)
            return null;
        return CryptoProvider.Instance.Decrypt((string) r);
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        object paramVal = DBNull.Value;
        if (value != null)
            paramVal = CryptoProvider.Instance.Encrypt((string) value);
        IDataParameter parameter = (IDataParameter)cmd.Parameters[index];
        parameter.Value = paramVal;
    }

   // Other IUserType members as usual..
}

As explained by Ayende himself in: http://ayende.com/Blog/archive/2008/07/31/Entities-dependencies-best-practices.aspx 正如Ayende本人在以下网站中所解释的: http : //ayende.com/Blog/archive/2008/07/31/Entities-dependencies-best-practices.aspx

Now when querying, using the NHibernate IQuery interface, I need to encrypt the parameter I am passing into the Query: 现在,在查询时,使用NHibernate IQuery接口,我需要加密要传递给Query的参数:

query.SetString("DbEncryptedParameter", 
    CryptoProvider.Instance.Encrypt(UnencryptedValueObject.ToString()));

Question : Is there a better way of performing this query, leveraging the knowledge NHibernate has of this Encrypted Type, so the encryption doesn't have to be performed while setting the parameter? 问题 :是否有更好的方法可以利用NHibernate具有的此加密类型的知识来执行此查询,因此在设置参数时不必执行加密?

Am I missing something? 我想念什么吗? Why do you need to encrypt the value when sending it in? 为什么在发送值时需要加密值? The point of IUserType is that you encapsulate the difference between the native form (plaintext) and persisted form (encrypted). IUserType的要点是您封装了本机形式(纯文本)和持久性形式(加密)之间的差异。 When you provide the plaintext version to the query, NHibernate should be invoking the IUserType.NullSafeSet to encrypt the value and set the query parameter to the encrypted value. 当您向查询提供纯文本版本时,NHibernate应该调用IUserType.NullSafeSet来加密该值并将查询参数设置为加密后的值。

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

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