Created
January 13, 2011 15:59
-
-
Save carlhoerberg/778090 to your computer and use it in GitHub Desktop.
Implementation of an encrypted string user type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AesCryptoProvider : ICryptoProvider | |
{ | |
private string password; | |
public AesCryptoProvider(string password) | |
{ | |
this.password = password; | |
} | |
public byte[] Encrypt(string inputText) | |
{ | |
using (var rij = new AesManaged()) | |
using (var pdb = new Rfc2898DeriveBytes(password, 16)) | |
using (var encryptor = rij.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16))) | |
{ | |
var textBytes = Encoding.UTF8.GetBytes(inputText); | |
var encryptedBytes = encryptor.TransformFinalBlock(textBytes, 0, textBytes.Length); | |
return pdb.Salt.Concat(encryptedBytes).ToArray(); | |
} | |
} | |
public string Decrypt(byte[] encryptedBytes) | |
{ | |
var salt = encryptedBytes.Take(16).ToArray(); | |
using (var rij = new AesManaged()) | |
using (var pdb = new Rfc2898DeriveBytes(password, salt)) | |
using (var decryptor = rij.CreateDecryptor(pdb.GetBytes(32), pdb.GetBytes(16))) | |
{ | |
try | |
{ | |
var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 16, encryptedBytes.Length - 16); | |
return Encoding.UTF8.GetString(decryptedBytes); | |
} | |
catch (CryptographicException) | |
{ | |
return "[Wrong password]"; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EncryptedStringUserType : IUserType | |
{ | |
private readonly ICryptoProvider cryptoProvider; | |
public EncryptedStringUserType(ICryptoProvider cryptoProvider) | |
{ | |
this.cryptoProvider = cryptoProvider; | |
} | |
public object NullSafeGet(IDataReader rs, string[] names, object owner) | |
{ | |
var data = NHibernateUtil.Binary.NullSafeGet(rs, names[0]) as byte[]; | |
return cryptoProvider.Decrypt(data); | |
} | |
public void NullSafeSet(IDbCommand cmd, object value, int index) | |
{ | |
var data = cryptoProvider.Encrypt((string)value ?? ""); | |
NHibernateUtil.String.NullSafeSet(cmd, data, index); | |
} | |
public object DeepCopy(object value) | |
{ | |
return value; | |
} | |
public object Replace(object original, object target, object owner) | |
{ | |
return original; | |
} | |
public object Assemble(object cached, object owner) | |
{ | |
return cached; | |
} | |
public object Disassemble(object value) | |
{ | |
return value; | |
} | |
public SqlType[] SqlTypes | |
{ | |
get { return new[] { new SqlType(DbType.Binary) }; } | |
} | |
public Type ReturnedType | |
{ | |
get { return typeof(string); } | |
} | |
public bool IsMutable | |
{ | |
get { return false; } | |
} | |
public new bool Equals(object x, object y) | |
{ | |
return object.Equals(x, y); | |
} | |
public int GetHashCode(object x) | |
{ | |
return x.GetHashCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment