PDA

View Full Version : آموزش: Encrypt کردن connectionStrings در app.config



Sp|R|T
جمعه 06 آبان 1390, 17:36 عصر
سلام

در این تاپیک قصد دارم نحوه Encrypting کردن مقدارهایی که در app.config قرار داره رو آموزش بدم ( اولین باره دارم میگم آموزش بدم :لبخند::لبخند::کف: )

من خودم برنامه ای رو نوشته بودم که به دیتابیس وصل میشد اونم دیتابیس سایتم :-ss و خب قصدم این بود که برنامه رو بزارم واسه دانلود و همه ازش استفاده کنن ( مثلآ برای ثبت نام و و و)

خب حالا این برنامه داخلش یوز و پسورد دیتابیس سایتم ست شده و اگه کسی یکم باهاش ور بره میتونه یوزر و پسورد رو بیرون بکشه که دیگه ............... :|

ولی این روشی که امروز میگم باعث تمامیه اطلاعات داخل app.config رو کد میکنه ( مطمئن باش 100 سال دیگه میشه این کد رو شکست :دی )

و اما آموزش:

داخل app.config :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<clear/>
<add name="MyDBConnectionString" connectionString="Server=127.0.0.1;Port=3306;Database=test;User ID=root;Password=;CharSet=utf8" />
</connectionStrings>
</configuration>

که اینجا خیلی راحت یوزر و پسود نشون داده ! ( شاید کسی بجا app.config از خوده فورم استفاده کنه و واسه این هم باید بگم که عزیزم اونم میشه بازش کرد و یوزر و پسورد رو دید :چشمک: مثل همین برنامه هایی که کرک میکنن )

خب الان در فرممون اینو قرار میدیم:


private static void ProtectConnectionString()
{
ToggleConnectionStringProtection(System.Windows.Fo rms.Application.ExecutablePath, true);
}

private static void ToggleConnectionStringProtection(string pathName, bool protect)
{
// Define the Dpapi provider name.
string strProvider = "DataProtectionConfigurationProvider";
// string strProvider = "RSAProtectedConfigurationProvider";

System.Configuration.Configuration oConfiguration = null;
System.Configuration.ConnectionStringsSection oSection = null;

try
{
// Open the configuration file and retrieve the connectionStrings section.

// For Web!
// oConfiguration = System.Web.Configuration.WebConfigurationManager.O penWebConfiguration("~");

// For Windows!
// Takes the executable file name without the config extension.
oConfiguration = System.Configuration.ConfigurationManager.OpenExeC onfiguration(pathName);

if (oConfiguration != null)
{
bool blnChanged = false;
oSection = oConfiguration.GetSection("connectionStrings") as System.Configuration.ConnectionStringsSection;

if (oSection != null)
{
if ((!(oSection.ElementInformation.IsLocked)) && (!(oSection.SectionInformation.IsLocked)))
{
if (protect)
{
if (!(oSection.SectionInformation.IsProtected))
{
blnChanged = true;

// Encrypt the section.
oSection.SectionInformation.ProtectSection(strProv ider);
}
}
else
{
if (oSection.SectionInformation.IsProtected)
{
blnChanged = true;

// Remove encryption.
oSection.SectionInformation.UnprotectSection();
}
}
}

if (blnChanged)
{
// Indicates whether the associated configuration section will be saved even if it has not been modified.
oSection.SectionInformation.ForceSave = true;

// Save the current configuration.
oConfiguration.Save();
}
}
}
}
catch (System.Exception ex)
{
throw (ex);
}
finally
{
}
}


و برای استفاده :

private void button1_Click(object sender, RoutedEventArgs e)
{

try
{
ProtectConnectionString();

}
catch (MySqlException ex)
{
MessageBox.Show("Error", ex.Message);
}
string con = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = con;
conn.Open();
MessageBox.Show("Open");
}

خب حالا با روی کلیک بروی دکمه به دیتابیس وصل میشه

و بعدش هم برید app.config رو چک کنید که شده این :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAKkAsC90km0aCbahELx+mRAQAAAACAAAAAAAQZgAAA AEAACAAAADjvQc1Y5iV1faduuFhhDYb6T9iDO+sJqbIc2LihOm EBwAAAAAOgAAAAAIAACAAAABfRVU83z9YvZxsWWNTvM/34V9gjRILMoPLEq1V/m9395ABAADbOIO3P17ZqPCUEHEMMBgtZXBrtgoMqCu/jXRVDjn/NZs6bBaHfzpgf2SDK2PoW9eZlwhub9eP94SRTkybd4L1XmMTYH Ly8pEGHM1D16CxRtToSgtRkrLt0itseWKDO52cMMjtGoAv5Vm4 znPjIXK0bqTrYqvty0jwMkSNUeVseSMD02z0VQ7Fc3g6zRhbaU kaiYsSIqmgzPc+aBXzuNxJ4Z9Zjwhjyho/aixqQSOqD7PupNytA4IHfJ9RknxCPfNruwrWH4+O335uRsbRZd RnM1hbSiAWIfKDM5YvM+gKqbb63z6wbk7MDTorykpPyEttfqrN rb2vapYklY7iKoXaVLE/NIgIKLGXefmCBXxA2tFFLH15IjqqHsY352yLlH97Yb41l+jG7t nd91jNP4C+53VD3gWbi0h8hzQjn+wNp0GzKHowoeOSk0Yw5N86 JI3R4kfrc6jYZXwFfAjaPLpdstr4y26upjNervqkmXz3akwkFa hJ5gaS4+uGTDAXzHwT7FIRHCOGNUVJ63OMkoO1QAAAANq2x6OS SyCpOpfgmWvnApA9noTAxGariG9hLKTV7vP6eAxAS/NlfW1cPTAmLRtfwDhkXamU677Dngx//qYq+Ig=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
</configuration>



کی میتونه کرک کنه اطلاعات رو ؟ :لبخند::لبخند::متفکر:

HAMID484
سه شنبه 17 آبان 1390, 16:58 عصر
خیلی ممنون از کدتون . بعدا به دردم میخوره چون ایده خوب و جالبی دارید. اما همه جاش رو با سوادی که الان دارم متوجه نمیشم .:خجالت: ایشالله به مرور:بامزه:
ی سوال داشتم. من با سبک کد نویسی این app.config آشنایی ندارم
من ی نرم افزار نوشتم و میخوام اطلاعات رشته اتصال رو به صورت کد شده ( نه هش شده ) در دیتابیس ذخیره کنم و زمانی که لازم بود این اطلاعات رو دیکد کنم و داخل یک متغییر رشته ای بریزم و این متغییر رو در داخل app.config استفاده کنم.
به زبان ساده . آقا چطوری ی متغییر رشته ای رو داخل app.config استفاده کنم:چشمک:

HAMID484
چهارشنبه 18 آبان 1390, 11:49 صبح
جواب سوالم رو اینجا پیدا کردم
http://barnamenevis.org/showthread.php?220322