using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//WHile making this program we assume that there is no j in the english alphabet
// while searching we consider both i and j as same
namespace ConsoleApplication5
{
class Program
{
static int rs=0, cs = 0;
static string encrypt="";
static char[,] arr = new char[5, 5];
static void Main(string[] args)
{
string key="",text="",text2="";
int i,l,r=0,c=-1;
Console.WriteLine("Enter a key");
key=Console.ReadLine();
key = key.Trim();
key = key.ToUpper();
l=key.Length;
for (i = 0; i < l; i++)
{
if ((key[i] == 'J'&& (key.IndexOf('I')>=0))||(key[i]==' '))
{
}
else
{
search( key[i], out rs, out cs);
if(cs==-1)
put( ref r, ref c, key[i]);
}
}
fill(r,c);
display(); // this step will complete the process of creating the key into the array
Console.WriteLine("Enter a text to cipher");
text = Console.ReadLine();
text = text.ToUpper();
text = text.Trim();
for (i = 0; i < text.Length; i++)
{
if(text[i]!=' ')
text2 += text[i];
}
Console.WriteLine(text2);
if(text2.Length%2==1)
text2+="X";
for(i=0;i<text2.Length;i+=2)
{
encrypt+=cipher(text2[i],text2[i + 1]);
}
Console.ReadKey();
}
static string cipher(char a,char b)
{
int r, c,r2,c2,tmp ;
string ret = "";
search(a,out r,out c);
search(b, out r2, out c2);
if (r == r2)
{
c2 = (c2 + 1) % 5;
c = (c + 1) % 5;
}
else if (c == c2)
{
r2 = (r2 + 1) % 5;
r = (r + 1) % 5;
}
else
{
tmp = c;
c = c2;
c2 = tmp;
}
ret = arr[r, c].ToString() + arr[r2, c2].ToString() + " ";
Console.WriteLine(ret);
return ret;
}
static void put(ref int r,ref int c,char k)
{
c++;
if (c >= 5)
{
c = 0; r += 1;
}
// Console.WriteLine(r + " " + c);
arr[r, c] = k;
}
static void search(char c1,out int rs,out int cs )
{
int r, c;
rs = -1; cs = -1;
if ((c1=='I')||(c1=='J'))
{
for (r = 0; r < 5; r++)
for (c = 0; c < 5;c++ )
if ((arr[r, c] == 'J')||(arr[r,c]=='I'))
{ rs=r;
cs=c;
}
}
else
{for (r = 0; r < 5; r++)
for (c = 0; c < 5;c++ )
if (arr[r, c] == c1)
{ rs=r;
cs=c;
}}
}
static void display()
{ int r, c;
for (r = 0; r < 5; r++)
{
for (c = 0; c < 5; c++)
Console.Write(arr[r, c]);
Console.WriteLine();
}
}
static void fill(int r,int c)
{
int i=65,r1,c1,r2,c2,r3,c3;
for (i = 65; i <= 90; i++)
{search( 'J',out r1,out c1 );
search('I',out r2,out c2 );
search((char)i, out r3, out c3);
if (((char)i == 'I' && c1 >= 0)||((char)i == 'J' && c2 >= 0))
{ }
else if (c3 < 0)
put(ref r,ref c, (char)i);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
//WHile making this program we assume that there is no j in the english alphabet
// while searching we consider both i and j as same
namespace ConsoleApplication5
{
class Program
{
static int rs=0, cs = 0;
static string encrypt="";
static char[,] arr = new char[5, 5];
static void Main(string[] args)
{
string key="",text="",text2="";
int i,l,r=0,c=-1;
Console.WriteLine("Enter a key");
key=Console.ReadLine();
key = key.Trim();
key = key.ToUpper();
l=key.Length;
for (i = 0; i < l; i++)
{
if ((key[i] == 'J'&& (key.IndexOf('I')>=0))||(key[i]==' '))
{
}
else
{
search( key[i], out rs, out cs);
if(cs==-1)
put( ref r, ref c, key[i]);
}
}
fill(r,c);
display(); // this step will complete the process of creating the key into the array
Console.WriteLine("Enter a text to cipher");
text = Console.ReadLine();
text = text.ToUpper();
text = text.Trim();
for (i = 0; i < text.Length; i++)
{
if(text[i]!=' ')
text2 += text[i];
}
Console.WriteLine(text2);
if(text2.Length%2==1)
text2+="X";
for(i=0;i<text2.Length;i+=2)
{
encrypt+=cipher(text2[i],text2[i + 1]);
}
Console.ReadKey();
}
static string cipher(char a,char b)
{
int r, c,r2,c2,tmp ;
string ret = "";
search(a,out r,out c);
search(b, out r2, out c2);
if (r == r2)
{
c2 = (c2 + 1) % 5;
c = (c + 1) % 5;
}
else if (c == c2)
{
r2 = (r2 + 1) % 5;
r = (r + 1) % 5;
}
else
{
tmp = c;
c = c2;
c2 = tmp;
}
ret = arr[r, c].ToString() + arr[r2, c2].ToString() + " ";
Console.WriteLine(ret);
return ret;
}
static void put(ref int r,ref int c,char k)
{
c++;
if (c >= 5)
{
c = 0; r += 1;
}
// Console.WriteLine(r + " " + c);
arr[r, c] = k;
}
static void search(char c1,out int rs,out int cs )
{
int r, c;
rs = -1; cs = -1;
if ((c1=='I')||(c1=='J'))
{
for (r = 0; r < 5; r++)
for (c = 0; c < 5;c++ )
if ((arr[r, c] == 'J')||(arr[r,c]=='I'))
{ rs=r;
cs=c;
}
}
else
{for (r = 0; r < 5; r++)
for (c = 0; c < 5;c++ )
if (arr[r, c] == c1)
{ rs=r;
cs=c;
}}
}
static void display()
{ int r, c;
for (r = 0; r < 5; r++)
{
for (c = 0; c < 5; c++)
Console.Write(arr[r, c]);
Console.WriteLine();
}
}
static void fill(int r,int c)
{
int i=65,r1,c1,r2,c2,r3,c3;
for (i = 65; i <= 90; i++)
{search( 'J',out r1,out c1 );
search('I',out r2,out c2 );
search((char)i, out r3, out c3);
if (((char)i == 'I' && c1 >= 0)||((char)i == 'J' && c2 >= 0))
{ }
else if (c3 < 0)
put(ref r,ref c, (char)i);
}
}
}
}
Comments