I know you can convert binary data to nvarchar and save in an nvarchar column, and then convert it back to binary when retrieving it. Will this work correctly all the time to give you your original binary data? Or could something get messed up in the translation?
I am asking because we have a nvarchar column that saves passwords in plain text. I would like this to change to saving the PWs in encrypted text. If I use the function EncryptByPassPhrase, it return varbinary. I am wondering if I can just convert the output from EncryptByPassPhrase to nvarchar and save in the same Password column. This would be easier than creating a new column of type varbinary to save the encrypted PW.
So I am proposing that I convert the current passwords like this:
UPDATE Users
SET Password = CONVERT(nvarchar(200), EncryptByPassPhrase('whatever', Password))
Then I would decrypt and retrieve the PW like:
SELECT CONVERT(nvarchar(200), DecryptByPassPhrase('whatever', Password)) AS PW
FROM Users
Will this always work correctly?
Thanks in advance!
Ignoring whether or not this is a good idea the following query seems to indicate that all two byte combinations round trip correctly.