views:

123

answers:

3

I am doing connection string encryption. we use our own encryption key with AES algorithm to do this. during the process, we need to convert string to byte array and then convert byte array back to string. I found the encoding play an important role on those conversions.

So I need to know the encoding C# is using to get above conversion right. Any idea how to get current encoding programmably?

+1  A: 
Richard
+1  A: 

If you're looking to store an encrypted string in a config (or other plain text) file, you're better off storing the encrypted bytes as a base64-encoded string.

byte[] encrypted = // encrypt your data

string encryptedString = Convert.ToBase64String(encrypted);

Likewise:

string encryptedString = // read the config value

byte[] encrypted = Convert.FromBase64String(encryptedString);

You can use the first code block to obtain a base64-encoded string representation of your encrypted data, then save that string in the config file. Use the second block to convert that same string from the file back into an encrypted byte array.

Adam Robinson
but I still need to know the encoding to convert the original unencrypted string from config file to byte array before I use your suggestion to convert the encypted bytes array to Base64String to write back to config file during the encryption. The same in the decryption process, I need to convert the decrypted byte array to normal string.
5YrsLaterDBA
@5YrsLaterDBA: The configuration system should give you the connection string as a string. You can use whatever encoding you like to perform the string-to-byte-array encoding and decoding, as long as you use the same on both sides. UTF-8 and UTF-16 are common choices, though I'd recommend UTF-8 since you're likely only to encounter 7-bit ASCII values anyway.
Adam Robinson
+1  A: 

Just in case if you don't know, but .NET have built-in support for Encrypting Configuration Information Using Protected Configuration. The encryption will be transparent for the application but operation system will guarantee that only accounts allowed to decipher that configuration (will have have access to key container) will be able to decrypt it.

I don't know your security model, but I guess you still need to get/store a key in order to decrypt connection string. So effectively connection string are as safe as the key which will be used to decrypt them. If it isn't as safe as in OS's key container I would recommend to reconsider your design.

Regent
We have investigated that built-in support but our expert want to use our own encryption key which is machine specific. That key will not store anywhere on the disk but generated when appliation is running.
5YrsLaterDBA
@5YrsLaterDBA: Well, machine-dependent key is not that bad, but, still, .NET built in is both machine- and user-dependent, what is just more secure anyway. Also if the person trying to decrypt data will get the binaries as well it could be just easier to him to brute-force data used for machine key creation rather that the key itself.
Regent
@5YrsLaterDBA: Anyway, you can build your own Protected Configuration Provider (http://msdn.microsoft.com/en-us/library/wfc2t3az.aspx) which will data with the algorithm and key you want.
Regent