Hi,
In the meantime I implemented what I asked above. It seems to work pretty code. Maybe others can use my implementation as sample good. Here it is:
(Data structure: Table EmployeeSalary with the fields Id, EmployeeId and EncryptedNetSalary)
Decrypting data
ResultsetFields fields = new ResultsetFields(3);
fields.DefineField(EmployeeSalaryFields.Id, 0);
fields.DefineField(EmployeeSalaryFields.EmployeeId, 1);
fields.DefineField(EmployeeSalaryFields.EncryptedNetSalary, 2, "NetSalary");
fields[3].ExpressionToApply = new DbFunctionCall("DecryptByPassPhrase", new object[] { "password", EmployeeSalaryFields.EncryptedNetSalary });
DataTable results = new DataTable();
dataAdapter.FetchTypedList(fields, results, null);
byte[] bytes = (byte[])results.Rows[0]["NetSalary"];
string decryptedNetSalary = System.Text.Encoding.Unicode.GetString(bytes);
Encrypting data
EmployeeSalaryEntity employeeSalary = new EmployeeSalaryEntity();
employeeSalary.Fields[(int)EmployeeSalaryFieldIndex.EncryptedNetSalary].ExpressionToApply =
new DbFunctionCall("encryptByPassPhrase", new object[] { "password", "textToEncrypt" });
adapter.UpdateEntitiesDirectly(employeeSalary, null);
LLBLGen generates the following SQL statement to encrypt the data:
exec sp_executesql
N'UPDATE [dbo].[EmployeeSalary] SET
[encryptedNetSalary]=encryptByPassPhrase(@LO6c85ac7f1, @LOa0b51cf82)',
N'@LO6c85ac7f1 nvarchar(7),
@LOa0b51cf82 nvarchar(9)',
@LO6c85ac7f1=N'password',
@LOa0b51cf82=N'textToEncrypt'
Reading the data on SQL Server
SELECT *,
netsalary = convert(nvarchar(10), decryptbypassphrase(N'password', encryptedNetSalary))
Writing the data on SQL Server
INSERT INTO EmployeeSalary (employeeId, encryptedNetSalary)
Values (100557, EncryptByPassPhrase(N'password', N'my salary'))
Always use NVARCHAR for all operations on SQL Server. I had some problems with that because for the encryption functions it matters if you use nvarchar or varchar.
Have fun!
Cheers.