I was just testing a DB connection on a C# project I'm working on. I was wondering if there is any protection to my credentials being exposed to a possible decompiling.
public int connect_DB()
{
try
{
string server = "localhost";
string db = "myDB";
string username = "myUsername";
string password = "passwordDB";
string constring = $"SERVER={server};DATABASE={db};UID={username};PASSWORD={password};";
conn = new MySqlConnection(constring);
conn.Open();
return 1;
}
catch (Exception)
{
MessageBox.Show("Please contact to an administrator", "DataBase connection failed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}
}
This project will be build as a .exe and provided to companies. If someone decompiles the code, the DB credentials will be shown as plain text and that can lead to some obvious problems.
Thanks!
Decompile the build (.exe) in the environment .NET is not difficult. To do this, there are decompilers like dnSpy, JustDecompile and others. To protect your code from decompilation, you need to use an obfuscator. The obfuscator makes your source code difficult to read during decompilation, but at the same time retains its functionality. There are also a huge number of obfuscators, both paid and free. I would recommend using .NET Reactor. But it's also worth remembering that for almost any obfuscator, there are also deobfuscators that decrypt assemblies processed by the obfuscator. For example , to deobfuscate the assembly .NET Reactor there is a NETReactorSlayer project.
As a result, I would advise you: