-2

I have the following connectionString string:

"DataSource=R7gIrRzkARUJsQBUYXKCPpH8MdqtQ5Sd+lt4kyBEZBY=; userid=weRGgY7PERBTg2WPPzVerIlMP93kmQbTSuKsJKyDHFU=; password=qcMxEGU75lJ1VD5OaaujnLzleR/7ZQDco3kddfwTOvI=;"

After I pass the string to a decrypt function, I will use it in the following:

iDB2Connection.ConnectionString = connectionString;

My question is, what's the best way to decrypt the values in the connectionString string?

One possibility was to convert the string to a DataSet and call the following code:

String connection = dsConnection.Tables[0].Rows[0]["connectionstring"].ToString();
SqlConnectionStringBuilder DBConfig = new SqlConnectionStringBuilder(connection);
string ConnectionString =
                "Data Source=" + Decrypt(DBConfig.DataSource)
                + ";Initial Catalog=" + Decrypt(DBConfig.InitialCatalog)
                + ";User ID=" + Decrypt(DBConfig.UserID)
                + ";Password=" + Decrypt(DBConfig.Password);

but I haven't figured out how to do that successfully.

Any help is appreciated.

user1666620
  • 4,800
  • 18
  • 27
HappyCoding
  • 641
  • 16
  • 36
  • Possible duplicate of [Encrypt and decrypt a string](http://stackoverflow.com/questions/202011/encrypt-and-decrypt-a-string) – kayess Feb 02 '16 at 16:08
  • What doesn't work? What errors or invalid results do you get? Is the problem on the decryption or the parsing of the encrypted connection string? – D Stanley Feb 02 '16 at 16:27
  • @DStanley If I were to move forward with the `DastSet` method, I am unsure how to parse the connectionString correctly into the `DataSet` format I am looking for. – HappyCoding Feb 02 '16 at 16:33
  • What does adding the string to a `DataSet` buy you? Seems like you just pull the string back out to pass it to `SqlConnectionStringBuilder`. – D Stanley Feb 02 '16 at 16:38
  • @DStanley I agree. Thus we can skip the `DataSet` step. My question would then be, how do I go from `string` to `SqlConnectionStringBuilder` correctly? When I call the following line: SqlConnectionStringBuilder DBConfig = new SqlConnectionStringBuilder("DataSource=R7gIrRzkARUJsQBUYXKCPpH8MdqtQ5Sd+lt4kyBEZBY=; userid=weRGgY7PERBTg2WPPzVerIlMP93kmQbTSuKsJKyDHFU=; password=qcMxEGU75lJ1VD5OaaujnLzleR/7ZQDco3kddfwTOvI=;"); I get an error of "Keyword not supported: 'datasource'." – HappyCoding Feb 02 '16 at 16:45

1 Answers1

2

The problem is your source is an OleDb connection sting, not a SQL Server connection string. I would recommend using an OleDbConnectionStringBuilder instead:

var connectionString = "DataSource=R7gIrRzkARUJsQBUYXKCPpH8MdqtQ5Sd+lt4kyBEZBY=; userid=weRGgY7PERBTg2WPPzVerIlMP93kmQbTSuKsJKyDHFU=; password=qcMxEGU75lJ1VD5OaaujnLzleR/7ZQDco3kddfwTOvI=;";

var DBConfig = new OleDbConnectionStringBuilder(connectionString);

string ConnectionString =
            "DataSource=" + Decrypt(DBConfig["datasource"].ToString())
            //+ ";Initial Catalog=" + Decrypt(DBConfig.InitialCatalog)
            + ";UserID=" + Decrypt(DBConfig["userid"].ToString())
            + ";Password=" + Decrypt(DBConfig["password"].ToString());

You could try to map the decrypted values from the source string to a Sql Server connection string, but I don't see a database key that would normally be present if you were connecting to a SQL server through OleDb.

HappyCoding
  • 641
  • 16
  • 36
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you I believe this is going to work. Now I need to complete the Decrypt function. Regarding your answer, from a learning perspective, how did you know that; how did you learn that? – HappyCoding Feb 02 '16 at 17:13
  • 1
    I looked up the documentation for [`SqlConnectionStringBuilder`](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx), realized that your source string was for Ole Db, not SQL, and searched for [`OleDbConnectionBuilder`](https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnectionstringbuilder(v=vs.110).aspx) as an alternative. – D Stanley Feb 02 '16 at 17:21