Decrypt via T-SQL data that was encrypted via C# - c#

I have a SQL Server table with 1 column containing encrypted data.
The data was encrypted using the answer provided by #CraigTP to this question... Encrypting & Decrypting a String in C#
I need to retrieve & decrypt this data using only T-SQL. Is that possible? How?

Related

Symmetric Key Encryption in SQl Server and decryption in C#

hope everyone will be fine here.
I have created a column with encrypted data using SQL Server Symmetric Key encryption feature. What I have done is given below:
CREATE MASTER KEY ENCRYPTION
BY PASSWORD = 'Test1'
CREATE CERTIFICATE EncryptTestCert
WITH SUBJECT = 'Test1'
CREATE SYMMETRIC KEY TestTableKey
WITH ALGORITHM = TRIPLE_DES ENCRYPTION
BY CERTIFICATE EncryptTestCert
CREATE SYMMETRIC KEY Test1
WITH ALGORITHM = TRIPLE_DES ENCRYPTION
BY CERTIFICATE EncryptTestCert
OPEN SYMMETRIC KEY TestTableKey DECRYPTION
BY CERTIFICATE EncryptTestCert
UPDATE [Security].[User]
SET EncryptedPhone = ENCRYPTBYKEY(KEY_GUID('TestTableKey'),Phone)
It has successfully Updated EncryptedPhone column with encrypted data (which is in varbinary).
Now the problem is that, I am using LINQToSQL on application and I am using inline Linq To SQL query (not stored procedure or sql query), I need to decrypt this data to view on the page. As you can see from the above database code, I am using TripleDES to create encrypted data. However I am unable to understand how can I achieve the decrypted data on C# which was encrypted through SQL query.

We. config encryption when multiple datasets are present

I am trying web.config encryption for server deployment.
I am using the aspnet_regiis.exe for encryption of the connection string.
In this project there are multiple datasets for the database calls.
how can I set decryption for all these datasets from one place as I cannot set decryption for each of the datasets nor can I keep the encrypted file during development.
after encryption the
<connectionstring></connectionstring>
gets converted to
<encrypteddata></encrypteddata>
so application crashes at the
((global::System.Data.SqlClient.SqlCommand)(this._commandCollection[0])).Connection = new global::System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString)
in the typed dataset as it is not able to ApplicationServices as now it is encrypted in the encrypted data

Encryption/ Decryption using AES128 between Objective c and C#

I am using encryption/decryption between Objective c(iPad end) and C#(.net, server site). I am using code from this link:
iPhone/C# AES Encryption
We are encrypting a XML on server end and sending it to iPad end using web services. At the iPad end when i am trying to decrypt, it is creating decrypted data but is not able to convert that data into the string. There is some problem coming in converting data into string using NSUTF8StringEncoding.
Before sending from server to iPad we are converting it into base64 String.
The same XML file when encrypted and decrypted at iPad end works fine.
I believe there are some special characters in the encrypted data coming from from server end ,therefore it is not decrypted at iPad end.
Can anybody give me any suggestion on this.
Thank you!
You can not send encrypted string over the network.
you need to convert encrypted NSString into Base64 String.
Then send this Base 64 string to ipad.

Query encrypted column in MS SQL using LINQ

I need to encrypt some columns in my MS SQL database (name, ssn ...) and I have been looking at column encryption as described by a few sites such as:
Encrypting Column Level Data in SQL Server
and
Introduction to SQL Server Encryption and Symmetric Key Encryption Tutorial.
I've been able to use an Trigger on insert to encrypt a column and I can decrypt the column within SQL Studio using:
OPEN SYMMETRIC KEY TestTableKey
DECRYPTION BY PASSWORD = 'Pa$$w0rd'
CONVERT(VARCHAR(50),DECRYPTBYKEY(EncryptSecondCol)) AS DecryptSecondCol
FROM TestTable
But how do I access the data from my application? I still want to be able to search for names. The only thing I can think of is using a Stored Procedure and sending the decryption password as a parameter in the LINQ statement so the stored procedure can decrypt the data and then execute the query.
Am I on the right track?
I'm new to this so I welcome other useful suggestions.
Yes, I have seen stored procedures to decrypt encrypted text. I've also seen stored procedures to encrypt text, which I prefer to Triggers because I don't like Triggers - see these answers.
However, I prefer to put encryption logic in my application layer - using, for example, the Enterprise Library Encryption Code. The passphrase and salt are easily encrypted in the config file using the Enterprise Library console.
Is there a specific reason for doing this work in the database? If you must do it that way, you could use EL to protect your passphrase and pass that into the stored procedure you've written.
If you are using MS SQL Server Enterprise or Developer editions, you can use TDE:
TDE

How to Encrypt a file for transport in C#?

I am trying to encrypt a file when I save it to disk and I have looked at the Crypto namespace in C#, but am unsure how I should do it. Basically I need the ability for my program to be able to both encrypt and decrypt a file. The file is just an xml file that is serialized by my program, but it can contain sensitive data like connection strings for SQL servers. My clients want the ability to email these profiles to others and open them in our application to apply the settings to their system.
I tried the AES classes in the Crypto namespace, but I don't know where to store the IV and the key so that my program on another machine will be able to decrypt it.
In a typical scenario, the flow for handling something like this would go:
The IV is static and known to the client
The end machine generates an RSA keypair, and gives the public key only to the party sending the data (the XML file)
Your AES key is generated, and encrypted using the RSA public key and sent to the client, now only the client is able to obtain that AES key using the private key it generated previously.
You encrypt the data using the AES key you securely sent to the client earlier
This means that even if someone captured the complete data stream, they wouldn't be able to decrypt your data because they don't have the private key required to obtain the AES key.

Categories