Creating an encrypted string type
In this age of identity theft, data security is more important than ever. Sensitive data such as credit card numbers should always be encrypted. In this recipe, we will show you how to use NHibernate to encrypt a single property.
How to do it…
Create a new class library project named
EncryptedStringExample
.Install the
NHibernate
andlog4net
packages using the NuGet Package Manager Console by executing the following command:Install-Package NHibernate Install-Package log4net
Add a new public interface named
IEncryptor
with the following three method definitions:public interface IEncryptor { string Encrypt(string plainText); string Decrypt(string encryptedText); string EncryptionKey { get; set; } }
Create an implementation of
IEncryptor
namedSymmetricEncryptorBase
using the following code:public abstract class SymmetricEncryptorBase : IEncryptor { private readonly SymmetricAlgorithm _cryptoProvider; private byte[] _myBytes; protected SymmetricEncryptorBase...