Professional Documents
Culture Documents
C# الجلسة العملية الثانية أمن معلومات
C# الجلسة العملية الثانية أمن معلومات
}
for (i = 0; i < 25; i++)
{
if (key.Contains(alphabets[i]))
continue;
else
key = key + alphabets[i];
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
switch (i)
{
case 0: Key[i, j] = key[j].ToString(); break;
case 1: Key[i, j] = key[j + 5].ToString(); break;
case 2: Key[i, j] = key[j + 10].ToString(); break;
case 3: Key[i, j] = key[j + 15].ToString(); break;
case 4: Key[i, j] = key[j + 20].ToString(); break;
}
}
}
Console.WriteLine("The Matrix of Cipher Key ");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
Console.Write(Key[i, j] + "\t");
}
Console.WriteLine();
}
// كود التشفير
for (i = 0; i < plaintext.Length; i += 2)
{
if (alphabets.Contains(plaintext[i]))
{
// " فحص وجود عدد أحرف فردي يقوم بإضافة حرفX "
if (plaintext.Length % 2 != 0 || !alphabets.Contains(plaintext[i + 1]))
{
plaintext = plaintext.Insert(i + 1, "X");
}
جامعة إدلب
أحمد المصري.م أمن المعلومات والشبكات المعهد التقاني للحاسوب
السنة الثانية
//" فحص وجود حرفين متماثلين متتاليين وبالتالي يقومك بوضع بين الحرفين المتشابهينX"
if (plaintext[i] == plaintext[i + 1])
{
plaintext = plaintext.Insert(i + 1, "X");
//تشفير األحرف
int RowChar1 = 0;
int RowChar2 = 0;
int ColumnChar1 = 0;
int ColumnChar2 = 0;
for (int R = 0; R < 5; R++)
{
for (int C = 0; C < 5; C++)
{
if (Key[R, C] == Convert.ToString(plaintext[i]))
{
RowChar1 = R;
ColumnChar1 = C;
}
if (Key[R, C] == Convert.ToString(plaintext[i + 1]))
{
RowChar2 = R;
ColumnChar2 = C;
}
}
}
if (RowChar1 == RowChar2)
{
ciphertext += Key[RowChar1, ((ColumnChar1 + 1) % 5)];
ciphertext += Key[RowChar1, ((ColumnChar2 + 1) % 5)];
}
else if (ColumnChar1 == ColumnChar2)
{
ciphertext += Key[((RowChar1 + 1) % 5), ColumnChar1];
ciphertext += Key[((RowChar2 + 1) % 5), ColumnChar1];
}
else
{
ciphertext += Key[RowChar1, ColumnChar2];
ciphertext += Key[RowChar2, ColumnChar1];
}
}
else
{
جامعة إدلب
أحمد المصري.م أمن المعلومات والشبكات المعهد التقاني للحاسوب
السنة الثانية
ciphertext += plaintext[i];
i--;
}
}
Console.Write("Cipher Text :" + ciphertext); }; break;
case 2:
{
Console.WriteLine("Enter the Cipher text Please : ");
Console.Write("Cipher Text : ");
ciphertext = Console.ReadLine().ToUpper();
ciphertext = ciphertext.Replace("J", "I");
Console.WriteLine("Enter Key of Decryption Please : ");
Console.Write("Key : ");
key = Console.ReadLine().ToUpper();
key = key.Replace("J", "I");
// إعداد جدول التشفير
// إزالة األحرف المكررة في المفتاح
for (i = 0; i < key.Count(); i++)
{
for (j = i + 1; j < key.Count(); j++)
{
if (key[i] == key[j])
{
key = key.Remove(j, 1);
}
}
}
for (i = 0; i < 25; i++)
{
if (key.Contains(alphabets[i]))
continue;
else
key = key + alphabets[i];
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
switch (i)
{
case 0: Key[i, j] = key[j].ToString(); break;
case 1: Key[i, j] = key[j + 5].ToString(); break;
case 2: Key[i, j] = key[j + 10].ToString(); break;
case 3: Key[i, j] = key[j + 15].ToString(); break;
جامعة إدلب
أحمد المصري.م أمن المعلومات والشبكات المعهد التقاني للحاسوب
السنة الثانية
{
plaintext += Key[(((RowChar1 - 1) + 5) % 5), ColumnChar1];
plaintext += Key[(((RowChar2 - 1) + 5) % 5), ColumnChar1];
}
else
{
plaintext += Key[RowChar1, ColumnChar2];
plaintext += Key[RowChar2, ColumnChar1];
}
}
else
{
plaintext += ciphertext[i];
i--;
}
}
Console.Write("Plain Text :" + plaintext); }; break;
default:
{
Console.WriteLine("Your Select is Unknown ...
Are you Want To Try Again [Y/N]");
ans = Convert.ToChar(Console.ReadLine());
if (ans == 'Y' || ans == 'y')
goto start;
else
{
Console.WriteLine("Thank You .... Program is Finished");
goto end;
}
}
}
end: ;
Console.ReadKey();
}
}
}
Hill لتطبيق خوارزميةC# اكتب كود بلغة: التمرين الثاني
انتهت الجلسة