Tuesday, April 21, 2015

Rail fence cipher program in C++

#include<iostream>
#include<string.h>
using namespace std;
class RailFence
{
char plain[55],cipher[55],matrix[9][9];
int pl,kl,i,j,k,l,n,m,temp,key[11];

public:
RailFence(){}
void GetPlain()
{
cout<<endl<<"\t\t ***ENTER PLAIN TEXT***"<<endl;
gets(plain);
pl=strlen(plain);
}

void GetKey()
{
cout<<endl<<"\t\t ***ENTER KEY SIZE***"<<endl;
cin>>kl;
cout<<endl<<"\t\t ***ENTER KEY***"<<endl;

for(i=0;i<kl;i++)
{
cin>>key[i];
}
cout<<"KEY IS::";
for(i=0;i<kl;i++)
{
cout<<"\t"<<key[i];
}
cout<<endl;
}

void PlainManipulation()
{
if(pl%kl!=0)
{
temp=((pl/kl)+1)*kl;
for(j=pl;j<temp;j++)
{
plain[j]='x';
}
plain[temp]='\0';
}
else
{
temp=pl;
}
}

void MakeMatrix()
{
k=0;
cout<<"\t";
for(i=0;i<(temp/kl);i++)
{
for(j=0;j<kl;j++,k++)
{
matrix[i][j]=plain[k];
cout<<"\t"<<matrix[i][j];
}
cout<<endl;
cout<<"\t";
}
}

void Encryption()
{
k=1;
m=0;
for(i=0;i<kl;i++)
{
for(j=0;j<kl;j++)
{
if(key[j]==k)
{
k++;
for(l=0;l<temp/kl;l++)
{
for(n=0;n<kl;n++)
{
if(j==n)
{
cipher[m]=matrix[l][n];
m++;
}
}
}
}
}
}
}

void DisplayCipher()
{
cout<<endl<<"\t\t ***CIPHER TEXT IS***"<<endl<<endl<<"\t\t";
for(i=0;i<temp;i++)
{
cout<<cipher[i];
}
cout<<endl<<endl<<"\t\t ***END***"<<endl<<endl;
}

};

int main()
{
RailFence R1;
R1.GetPlain();
R1.GetKey();
R1.PlainManipulation();
R1.MakeMatrix();
R1.Encryption();
R1.DisplayCipher();

return 0;
}

Thursday, April 9, 2015

Vigenere Cipher C++ code

Code
/*
Vigenere Cipher C++ code
Here we assume that plain text length is equal or  more than key length
*/

#include<iostream>
#include<string.h>

using namespace std;

class VigenereCipher
{
char plain[33],cipher[33],key[11];
int i,j,pl,kl;
public:
VigenereCipher(){ }
void GetPlain()
{
cout<<"\t\t\t ***ENTER PLAIN TEXT***"<<endl;
cin>>plain;
pl=strlen(plain);
}
void GetKey()
{
cout<<endl<<"\t\t\t ***ENTER KEY***"<<endl;
cin>>key;
kl=strlen(key);
}
void KeyManip()
{
for(i=0;i<pl;i++)
{
key[i]=key[i%kl];
}
}
void Encryption()
{
for(i=0;i<pl;i++)
{
cipher[i]=(((plain[i]-97)+(key[i]-97))%26)+97;
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t\t ***CIPHER TEXT IS***"<<endl;
for(j=0;j<pl;j++)
{
cout<<cipher[j];
}
cout<<endl<<endl<<"\t\t\t+++END OF PROGRAM+++";
}
};

int main()
{
VigenereCipher v1;
v1.GetPlain();
v1.GetKey();
v1.KeyManip();
v1.Encryption();
v1.DisplayCipher();
return 0;
}

Thursday, April 2, 2015

Bitwise Operation in C and C++

To manipulation of binary (bit) value, we generally perform various operation. Some of them are supported by C and C++ programming language. There are basically six BITWISE  operation available for C and C++ are;


  1. & for Bitwise AND
  2. | for Bitwise OR
  3. ^ for Bitwise Exclusive XOR
  4. << for left shift
  5. >> for right shift
  6. ~ for bitwise NOT
Here is the C++ code example for Bitwise operation

#include<iostream>
using namespace std;

class Bitwise
{
int a,b,r;
public:
Bitwise()
{ }

void GetNumber()
{
cout<<"\t\t ENTER TWO DECIMAL NUMBERS"<<endl;
cin>>a>>b;
}

void BitAnd()
{
r=a & b;
cout<<"\t\t BITWISE AND RESULT"<<endl<<"\t\t"<<r<<endl;
}

void BitOR()
{
r=a | b;
cout<<"\t\t BITWISE OR RESULT"<<endl<<"\t\t"<<r<<endl;
}

void BitXOR()
{
r=a ^ b;
cout<<"\t\t BITWISE XOR RESULT"<<endl<<"\t\t"<<r<<endl;
}

};


int main()
{
Bitwise b1;
b1.GetNumber();
b1.BitAnd();
b1.BitOR();
b1.BitXOR();
return 0;

}

Output is (ALL INPUT AND RESULT ARE IN DECIMAL)

Wednesday, April 1, 2015

lab:5 Simulation of continuous system


Her -> represent forward
<- represent backward
^ represent left or right

code

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

class ContinuousSystem
{
int fc,bc,lrc,i,j,RandNum,x,y;

public:
ContinuousSystem()
{
fc=0;
bc=0;
lrc=0;
x=20;
y=90;
}

void RandGen()
{
for(i=0;i<100;i++)
{
RandNum=rand()%100;

if(RandNum<50)
{
fc++;
cout<<"->\t";
}
else if(RandNum>49 && RandNum<75)
{
bc++;
cout<<"<-\t";
}
else
{
lrc++;
cout<<"^\t";
}
}
}

void Display()
{
cout<<endl<<"fc=="<<fc<<endl;
cout<<"bc=="<<bc<<endl;
cout<<"lrc=="<<lrc<<endl;
}

void CheckDist()
{
if(fc-bc==50)
cout<<"\t\t\t *** YOU ARE IN DISTINATION ***"<<endl;
else
cout<<"\t\t\t *** YOU ARE NOT IN DISTINATION ***"<<endl;
}

};

int main()
{
ContinuousSystem c1;
c1.RandGen();
c1.Display();
c1.CheckDist();


return 0;
}