Thursday, 25 July 2013

Cryptography Tool (Cipher Wheel)



A cipher is a set of rules for converting between plain text and cipher-text. These rules often use a secret key.
  

A Virtual Cipher Wheel  : http://invpy.com/cipherwheel 

How to Encrypt with the Cipher Wheel
First, write out your message in English on paper. For this example we will encrypt the message, “The secret password is Rosebud.” Next, spin the inner wheel around until its letters match up with letters in the outer wheel. Notice in the outer wheel there is a dot next to the letter A. Look at the number in the inner wheel next to the dot in the outer wheel. This number is known the encryption key.
The encryption key is the secret to encrypting or decrypting the message


So the steps to encrypt a letter are:
1. Decide on a key from 1 to 25. Keep this key secret!
2. Find the plain-text letter’s number.
3. Add the key to the plain-text letter’s number.
4. If this number is larger than 26, subtract 26.
5. Find the letter for the number you’ve calculated. This is the cipher-text letter.
6. Repeat steps 2 to 5 for every letter in the plain-text message.


To decrypt, subtract the key instead of adding it. For the cipher-text letter B, the number is 1. Subtract 1 – 13 to get -12. Like our “subtract 26” rule for encrypting, when we are decrypting and the result is less than 0, we have an “add 26” rule. -12 + 26 is 14. So the cipher-text letter B decrypts back to letter O.

Code:
#include<iostream>
#include<cstring>
//#include<ctype>
char * encript(int k,char *text);
char * decript(int k,char *text);
using namespace std;

int main()
{
char text[50];
int key,x;
cout<<"Enter the text:";
cin>>text;
cout<<"Enter encryption key:";
cin>>key;
cout<<"Press 1 for Encryption\nPress 2 for Decryption\n";
cin>>x;
if(x==1)
cout<<"Encripted text:"<<encript(key,text)<<endl;
else
cout<<"Decripted text:"<<decript(key,text)<<endl;

return(0);
}


char * encript(int key,char *text)
{
int ti;
for(int i=0;i<strlen(text);i++)
{
  if(isalpha(text[i])&&(isupper(text[i])))
   {
        ti=(int)text[i];
        ti+=key;
       if(ti>90)
        ti-=26;
       text[i]=(char)ti;

   }

 if(isalpha(text[i])&&(islower(text[i])))
   {
        ti=(int)text[i];
        ti+=key;
       if(ti>122)
        ti-=26;
       text[i]=(char)ti;

   }



}
return(text);
}

char * decript(int key,char *text)
{

int ti;
for(int i=0;i<strlen(text);i++)
{
  if(isalpha(text[i])&&(isupper(text[i])))
   {
        ti=(int)text[i];
        ti-=key;
       if(ti<65)
        ti+=26;
       text[i]=(char)ti;

   }

 if(isalpha(text[i])&&(islower(text[i])))
   {
        ti=(int)text[i];
        ti-=key;
       if(ti<97)
        ti+=26;
       text[i]=(char)ti;

   }



}
return(text);

}





 

Tuesday, 25 June 2013

A matchstick game



Q.Matchstick game being played between the computer and a user. Your program should ensure that the
computer always wins. Rules for the game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game. 


   #include<stdio.h>
   #include<conio.h>
void main()
{
int ms=21,ply,sys=0;
clrscr();
fflush(stdin);
printf("Select between 1-4\n otherwise computer wins\n");
while(ms>=1)
{
   if(ms==1)
    {
       printf("Computer wins\n");
       break;
    }
printf("Avilable Matchsticks = %d\n",ms);
printf("Player= ");
scanf("%d",&ply);
printf("\nComputer= %d\n",sys=5-ply);
ms=ms-sys-ply;
}
getch();
}

Monday, 24 June 2013

How to shuffle the array elements..?

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int size,i;

cout<<"Enter the size of array:";
cin>>size;

int a[20];
cout<<"\nEnter the elements:";

for(i=0;i<size;i++)
cin>>a[i];

srand(time(0));
int k,t;


for(i=0;i<size;i++)
{
k=rand()%size;
t=a[i];        //a[i]=a[i]*a[k];
a[i]=a[k];     //a[k]=a[i]/a[k];
a[k]=t;        //a[i]=a[i]/a[k];
}

cout<<"\nShuffeled array:";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
cout<<endl;
return 0;
}

Wednesday, 8 May 2013

Sort elements by frequency

Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}.

**************************************
*         C++ implementation:        *
**************************************
#include<iostream>
#include<stdlib.h>
using namespace std;
struct tree
{
int freq,info;
struct tree *lchild,*rchild;
};
typedef struct tree* TREE;
TREE creat(TREE,int);
void sort();
void inorder(TREE);

int e[20],c[20],l=0;

int main()
{
cout<<"Enter the number of elements to be inserted:";
int n;cin>>n;
TREE root=NULL;

cout<<"Enter the elements:";
for(int i=0;i<n;i++)
{
  int ele;cin>>ele;
  root=creat(root,ele);
}

inorder(root);
sort();
cout<<"Sorted elements:";
for(int i=0;i<=l;i++)
{
  for(int j=0;j<c[i];j++)
  cout<<e[i]<<"\t";
}
cout<<endl;
return(0);
}


void inorder(TREE root)
{
  if(root!=NULL)
  {
    inorder(root->lchild);
    e[++l]=root->info;
    c[l]=root->freq;
    inorder(root->rchild);
  }
}

void sort()
{
int temp;
for(int i=0;i<l-1;i++)
for(int j=0;j<l-i-1;j++)
{
  if(c[j]<c[j+1])
   {
      temp=c[j];
      c[j]=c[j+1];
      c[j+1]=temp;
      temp=e[j];
      e[j]=e[j+1];
      e[j+1]=temp;
   }
}

}

TREE creat(TREE node,int key)
{
TREE nnode,x,p;
nnode=(TREE)malloc(sizeof(struct tree));
nnode->lchild=NULL;
nnode->rchild=NULL;
nnode->info=key;
nnode->freq=1;

if(node==NULL)
return(nnode);

x=node;
while(x!=NULL)
{
   p=x;
   if(key>x->info)
   x=x->rchild;
   else if(key<x->info)
   x=x->lchild;
   else
   {
        x->freq++;
        return(node);
   }
}
if(key>p->info)
p->rchild=nnode;
else
p->lchild=nnode;
return(node);
}