Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

‫جامعة إدلب‬

‫ أحمد المصري‬.‫م‬ ‫أمن المعلومات والشبكات‬ ‫المعهد التقاني للحاسوب‬


‫السنة الثانية‬

‫الجلسة العملية الثانية‬


:‫التمرين األول‬
C# ‫ برمجيا ً باستخدام لغة‬Playfair ‫تطبيق شيفرة‬
using System;
namespace Playfair_cipher
{
class Program
{
static void Main()
{
int i, j, select;
char ans;
string[,] Key = new string[5, 5];
string alphabets = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
string plaintext = "", key, ciphertext = "";
Console.WriteLine("Welcome To Playfair Algorithm ");
start:
Console.WriteLine("Please, Select What are you want to do [1 - 2 ]:");
Console.WriteLine("1- Encryption Text\n2- Decryption Text");
select = Convert.ToInt32(Console.ReadLine());
switch (select)
{
case 1:
{
Console.WriteLine("Enter the plain text Please : ");
Console.Write("Plain Text : ");
plaintext = Console.ReadLine().ToUpper();
plaintext = plaintext.Replace("J", "I");
Console.WriteLine("Enter Key of Encryption or 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;
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;
‫جامعة إدلب‬
‫ أحمد المصري‬.‫م‬ ‫أمن المعلومات والشبكات‬ ‫المعهد التقاني للحاسوب‬
‫السنة الثانية‬

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 < ciphertext.Length; i += 2)
{
if (alphabets.Contains(ciphertext[i]))
{
//‫تشفير األحرف‬
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(ciphertext[i]))
{
RowChar1 = R;
ColumnChar1 = C;
}

if (Key[R, C] == Convert.ToString(ciphertext[i + 1]))


{
RowChar2 = R;
ColumnChar2 = C;
}
}
}
if (RowChar1 == RowChar2)
{
plaintext += Key[RowChar1, (((ColumnChar1 - 1) + 5) % 5)];
plaintext += Key[RowChar1, (((ColumnChar2 - 1) + 5) % 5)];
}
else if (ColumnChar1 == ColumnChar2)
‫جامعة إدلب‬
‫ أحمد المصري‬.‫م‬ ‫أمن المعلومات والشبكات‬ ‫المعهد التقاني للحاسوب‬
‫السنة الثانية‬

{
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# ‫ اكتب كود بلغة‬: ‫التمرين الثاني‬

‫انتهت الجلسة‬

You might also like