PDA

View Full Version : سوال: مفهوم این کدها در رمزنگاری متقارن AES چیست؟



ghazal_73
دوشنبه 26 مهر 1395, 07:53 صبح
سلام دوستان
این قسمت از کد که فرستادم توی رمزنگاری چ معنی ای میده؟
مثلا Salt، Throw و ....

ممنون



private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");


/// <summary>
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
/// </summary>
/// <param name="plainText">The text to encrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for encryption.</param>
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");


string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.


try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);


// Create a RijndaelManaged object
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);


// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);


// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
// prepend the IV
msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Le ngth), 0, sizeof(int));
msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
catch (Exception ex)
{


Label l1 = new Label();
l1.ForeColor = Color.Red;
l1.Text = "???? ????? ?? ???? ????";
l1.Show();
Form1 f = new Form1();
f.Controls.Add(l1);
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}


// Return the encrypted bytes from the memory stream.
return outStr;
}


/// <summary>
/// Decrypt the given string. Assumes the string was encrypted using
/// EncryptStringAES(), using an identical sharedSecret.
/// </summary>
/// <param name="cipherText">The text to decrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for decryption.</param>
public static string DecryptStringAES(string cipherText, string sharedSecret)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");


// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;


// Declare the string used to hold
// the decrypted text.
string plaintext = null;


try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);


// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
// Get the initialization vector from the encrypted stream
aesAlg.IV = ReadByteArray(msDecrypt);
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))


// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
catch (Exception ex)
{
Label l = new Label();
l.ForeColor = Color.Red;
l.Text = "???? ????? ?? ???? ????";
l.Show();
Form1 f = new Form1();
f.Controls.Add(l);


}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}


return plaintext;
}


private static byte[] ReadByteArray(Stream s)
{
byte[] rawLength = new byte[sizeof(int)];
if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
{
throw new SystemException("????? ???? ???? ???? ???? ??? ????");
}


byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new SystemException("????? ???? ?? ????? ?????? ???? ???");
}


return buffer;
}

mrprestige
دوشنبه 26 مهر 1395, 15:50 عصر
عرض سلام و وقت بخیر خدمت شما . اگه منظورتون از Throw همون throw new ArgumentNullException و یا throw new SystemException به ترتیب خطاهای مربوط به null بودن و اون یکی خطاهای سیستمی رو برمیگردونه و همینطور منظور از salt_ ( خط شماره 1 ) یه متغیر استاتیک از نوع Byte هستش که میاد پارامتر ورودی خودش رو بصورت کدهای Ascii تبدیل میکنه .

ghazal_73
دوشنبه 26 مهر 1395, 17:26 عصر
عرض سلام و وقت بخیر خدمت شما . اگه منظورتون از Throw همون throw new ArgumentNullException و یا throw new SystemException به ترتیب خطاهای مربوط به null بودن و اون یکی خطاهای سیستمی رو برمیگردونه و همینطور منظور از salt_ ( خط شماره 1 ) یه متغیر استاتیک از نوع Byte هستش که میاد پارامتر ورودی خودش رو بصورت کدهای Ascii تبدیل میکنه .



سلام مممنون از پاسختون
میشه یکم کاملتر توضیح بدین که این قسمت ها کلا تو رمزنگاری چکاری انجام میده؟
یکم واضح تر
سپاس

mrprestige
دوشنبه 26 مهر 1395, 18:22 عصر
در کل اگه شما میخواید بطور خودکار روی خطاهاتون مدریت بشه از Try Catch Finally باید استفاده کنید این دستور میاد خطا هارو بطور خودکار در System Runtime (به زبان ساده تر زمانی که پروژه شما شروع به کار کردن میکنه ) از این دستور استفاده میکنید که این روش رو بنده پیشنهاد میکنم ولی اگر شما خواستید این روند بطور دستی انجام بشه باید از این کلمه کلیدی throw استفاده کنید ببینید مثلا دستور های مورد نظر از خط 12 تا 15 در نظر بگیرید در این خط گفته شده اگه ورودی تابع مون (Plain text و shared secret) که از نوع رشته هستن برابر تهی (null) یا اینکه رشته ای در اون ها ثبت نشده بود به کاربر خطای مربوط به null بودن ورودی تابع رو نشون بده . همینطور از خط 157 تا 160 رو ببینید میگه اگه خطاای در روند عملیات که مربوط به خوندن اطلاعات از buffer ( یا در کل همون حافظه ) رخ بده خطای سیستمی رو بهمون شون بده . ولی بازم عرض میکنم شما داخل try catch میتونید این ها رو بهتر مدیریت کنید . امیدوارم مطالب براتون تا حد امکان گیرا بوده باشه

ghazal_73
دوشنبه 26 مهر 1395, 18:48 عصر
در کل اگه شما میخواید بطور خودکار روی خطاهاتون مدریت بشه از Try Catch Finally باید استفاده کنید این دستور میاد خطا هارو بطور خودکار در System Runtime (به زبان ساده تر زمانی که پروژه شما شروع به کار کردن میکنه ) از این دستور استفاده میکنید که این روش رو بنده پیشنهاد میکنم ولی اگر شما خواستید این روند بطور دستی انجام بشه باید از این کلمه کلیدی throw استفاده کنید ببینید مثلا دستور های مورد نظر از خط 12 تا 15 در نظر بگیرید در این خط گفته شده اگه ورودی تابع مون (Plain text و shared secret) که از نوع رشته هستن برابر تهی (null) یا اینکه رشته ای در اون ها ثبت نشده بود به کاربر خطای مربوط به null بودن ورودی تابع رو نشون بده . همینطور از خط 157 تا 160 رو ببینید میگه اگه خطاای در روند عملیات که مربوط به خوندن اطلاعات از buffer ( یا در کل همون حافظه ) رخ بده خطای سیستمی رو بهمون شون بده . ولی بازم عرض میکنم شما داخل try catch میتونید این ها رو بهتر مدیریت کنید . امیدوارم مطالب براتون تا حد امکان گیرا بوده باشه


خیلی ممنون از توضیحاتتون.. متوجه شدم