بدي كود لخوارزمية التشفير playfairو rail fence طبعا كل وحدة لحالها رجاء الي بيقدر يساعدني يعطيني الحل وشكرا الكم
بدي كود لخوارزمية التشفير playfairو rail fence طبعا كل وحدة لحالها رجاء الي بيقدر يساعدني يعطيني الحل وشكرا الكم
كن صديقا ولا تنتظر ان يكون لك صديق
الكود بالسي شارب لل playfair
الكود بالسي شارب rail fenceكود PHP:using System;
using System.Text;
class Test
{
static void Main()
{
string originalText = "Defend the east wall of the castle.";
Console.WriteLine(originalText);
string plainText = Playfair.Prepare(originalText);
Console.WriteLine(plainText);
string key = "cdefghiklmnopqrstuvwxyzab";
string cipherText = Playfair.Encipher(key, plainText);
Console.WriteLine(cipherText);
plainText = Playfair.Decipher(key, cipherText);
Console.WriteLine(plainText);
Console.WriteLine();
originalText = "Hide the gold in the tree stump.";
Console.WriteLine(originalText);
plainText = Playfair.Prepare(originalText);
Console.WriteLine(plainText);
key = "playfirexmbcdghjknostuvwz";
cipherText = Playfair.Encipher(key, plainText);
Console.WriteLine(cipherText);
plainText = Playfair.Decipher(key, cipherText);
Console.WriteLine(plainText);
Console.ReadLine();
}
}
public class Playfair
{
/*
'Prepare' removes all characters that are not letters i.e. all numbers, punctuation,
spaces etc. are removed (uppercase is also converted to lowercase).
If the seond letter of a pair is the same as the first letter, an 'x' is inserted.
Also, if the length of the string is odd, an 'x' is appended to make it an even length
as Playfair can only encrypt even length strings.
If you want numbers, punctuation etc. you must spell it out e.g.
'stop' for period, 'one', 'two' etc.
*/
public static string Prepare(string originalText)
{
int length = originalText.Length;
originalText = originalText.ToLower();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++)
{
char c = originalText***91;i***93;;
if (c >= 97 && c <= 122)
{
// If the second letter of a pair is the same as the first, insert an 'x'
if (sb.Length % 2 == 1 && sb***91;sb.Length - 1***93; == c)
{
sb.Append('x');
}
sb.Append(c);
}
}
// If the string is an odd length, append an 'x'
if (sb.Length % 2 == 1)
{
sb.Append('x');
}
return sb.ToString();
}
/*
'Encipher' uses the Playfair cipher to encipher some text.
The key is a string containing all 26 letters in the alphabet, except one'.
*/
public static string Encipher(string key, string plainText)
{
int length = plainText.Length;
char a,b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i+=2)
{
a = plainText***91;i***93;;
b = plainText***91;i+1***93;;
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if(a_row == b_row)
{
if(a_col == 4)
{
sb.Append(key***91;a_ind - 4***93;);
sb.Append(key***91;b_ind + 1***93;);
}
else if(b_col == 4)
{
sb.Append(key***91;a_ind + 1***93;);
sb.Append(key***91;b_ind - 4***93;);
}
else
{
sb.Append(key***91;a_ind + 1***93;);
sb.Append(key***91;b_ind + 1***93;);
}
}
else if(a_col == b_col)
{
if(a_row == 4)
{
sb.Append(key***91;a_ind - 20***93;);
sb.Append(key***91;b_ind + 5***93;);
}
else if(b_row == 4)
{
sb.Append(key***91;a_ind + 5***93;);
sb.Append(key***91;b_ind - 20***93;);
}
else
{
sb.Append(key***91;a_ind + 5***93;);
sb.Append(key***91;b_ind + 5***93;);
}
}
else
{
sb.Append(key***91;5*a_row + b_col***93;);
sb.Append(key***91;5*b_row + a_col***93;);
}
}
return sb.ToString();
}
/*
'Decipher' uses the Playfair cipher to decipher some text.
The key is a string containing all 26 letters of the alphabet, except one.
*/
public static string Decipher(string key, string cipherText)
{
int length = cipherText.Length;
char a,b;
int a_ind, b_ind, a_row, b_row, a_col, b_col;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i+=2)
{
a = cipherText***91;i***93;;
b = cipherText***91;i+1***93;;
a_ind = key.IndexOf(a);
b_ind = key.IndexOf(b);
a_row = a_ind / 5;
b_row = b_ind / 5;
a_col = a_ind % 5;
b_col = b_ind % 5;
if(a_row == b_row)
{
if(a_col == 0)
{
sb.Append(key***91;a_ind + 4***93;);
sb.Append(key***91;b_ind - 1***93;);
}
else if(b_col == 0)
{
sb.Append(key***91;a_ind - 1***93;);
sb.Append(key***91;b_ind + 4***93;);
}
else
{
sb.Append(key***91;a_ind - 1***93;);
sb.Append(key***91;b_ind - 1***93;);
}
}
else if(a_col == b_col)
{
if(a_row == 0)
{
sb.Append(key***91;a_ind + 20***93;);
sb.Append(key***91;b_ind - 5***93;);
}
else if(b_row == 0)
{
sb.Append(key***91;a_ind - 5***93;);
sb.Append(key***91;b_ind + 20***93;);
}
else
{
sb.Append(key***91;a_ind - 5***93;);
sb.Append(key***91;b_ind - 5***93;);
}
}
else
{
sb.Append(key***91;5*a_row + b_col***93;);
sb.Append(key***91;5*b_row + a_col***93;);
}
}
return sb.ToString();
}
}
ملاحظة : الكود من النت و مش برمجتي ,, يرجى فحصه و عمل Test قبل تسليمة للأستاذ حمدي العمريكود PHP:/// <summary>
/// method for demonstrating the ZigZag Cipher
/// (Also known as the Rail Fence Cipher)
/// </summary>
/// <param name="str">string we wish to apply the cipher to</param>
/// <returns>the ciphered string</returns>
public static string ZigZagCipher(string str)
{
//create zip & zag (we're only using
//2 rails so this represents top and bottom rails)
string zig = "";
string zag = "";
//convert the input string to a char array
char***91;***93; chrArray = str.ToCharArray();
//this lets us know if we're dealing with the
//top or bottom rail
bool isTopRow = true;
//loop through the char array
foreach (char c in chrArray)
{
//if we're dealing with the top rail add
//current char to zig
if (isTopRow == true)
zig += c.ToString();
else
//otherwise add to zag
zag += c.ToString();
//change status
isTopRow = !isTopRow;
}
//concantenate the 2 and return
return zig + zag;
}
/// <summary>
/// method for undoing the zigzag cipher
/// </summary>
/// <param name="str">the string we're working with</param>
/// <returns>the converted string</returns>
public static string DecipherZigZag(string str)
{
//create zig & zag which both hold 1/2 the original string
string zig = str.Substring(0, str.Length / 2);
string zag = str.Substring(str.Length / 2);
//top & bottom counter to hold our position
int top = 0;
int bottom = 0;
//hold the deciphered string
string decyphered = "";
bool isTopRow = true;
//loop through the string, making sure we havent gone too far
while (decyphered.Length != str.Length)
{
//if we're dealing with the top rail then add to
//deciphered from the zig char array
if (isTopRow == true)
{
decyphered += zig.Substring(top, 1);
top++;
}
else
{
//otherwise add from the zag char array
decyphered += zag.Substring(bottom, 1);
bottom++;
}
isTopRow = !isTopRow;
}
return decyphered;
}
ملاحظة ثانية : الرمز 93*** الي ظاهر بالكود هو رمز المصفوفة المربعة [ و ] لكن لخطأ معين بالعرض بالمنتدى ما ظهر
التعديل الأخير تم بواسطة Sc®ipt ; 03-01-2011 الساعة 12:01 AM
شكرا كتييييييييييييييييييير ع المساعدة غلبتك معي![]()
كن صديقا ولا تنتظر ان يكون لك صديق
العفو ,, نورتي![]()
الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)
مواقع النشر (المفضلة)