After following the answer of this question, I was able to recreate the DecryptByPassPhrase function of SQL. Now I am trying to do the EncryptByPassPhrase function.
We have different SQL version on different server which cannot be upgrade to the same version at the same time, so we want to create our c# function (with SQL CLR).
I need to encode with SHA1 (like a SQL 2012) since the SQL on the other server might not be upgraded to 2017 yet
Here is my c#, the function compile, but I can't decrypt it with my other c# function (which is exactly the function of the answer of the other question)
int keySize = 16;
var cryptoAlgo = TripleDES.Create();
cryptoAlgo.Padding = PaddingMode.PKCS7;
cryptoAlgo.Mode = CipherMode.CBC;
cryptoAlgo.GenerateIV(); //cryptoAlgo.IV = StringToByteArray(value.Substring(8, 16));
//cryptoAlgo.IV = StringToByteArray("7854E155CEE338D5");
var valueInByte = Encoding.Unicode.GetBytes(value); //UTF8Encoding.UTF8.GetBytes(value); //encrypted = StringToByteArray(value.Substring(24));
byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
var hashAlgo = SHA1.Create();
hashAlgo.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length);
cryptoAlgo.Key = hashAlgo.Hash.Take(keySize).ToArray();
byte[] encrypted = cryptoAlgo.CreateEncryptor().TransformFinalBlock(valueInByte, 0, valueInByte.Length);
//byte[] encryptedData = encrypted.Skip(8).ToArray();
//bool isUtf16 = (Array.IndexOf(encryptedData, (byte)0) != -1);
//string encryptedText = (isUtf16 ? Encoding.Unicode.GetString(encryptedData) : Encoding.UTF8.GetString(encryptedData));
return new SqlString(encryptedText);
//return new SqlString(encryptedText);
//return Convert.ToBase64String(encrypted, 0, encrypted.Length);
//return Convert.ToBase64String(encryptedData, 0, encryptedData.Length);
//return "0x01000000" + Convert.ToBase64String(cryptoAlgo.IV, 0, cryptoAlgo.IV.Length) + Convert.ToBase64String(encrypted, 0, encrypted.Length);
//return "0x01000000" + Convert.ToBase64String(encryptedData, 0, encryptedData.Length);
there a lot of different return that i've tried (they are in comment). I noticed that SQL return "0x01000000" and I've guess that the next 16 character are the IV so i've tried to add them, with no luck