Tuesday, 27 November 2012

Multiplicative Cipher (C++)

Multiplicative Cipher : Encryption

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class MulCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
};
void MulCiph :: gettext()
{
  cout<<"\nEnter the string: ";
  cin>>str;
}
void MulCiph :: getkey()
{
  cout<<"\nEnter the multiplicative cipher key: ";
  cin>>key;
}
void MulCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    char c=(((toascii(str[i])-97)*key)%26)+97;
    cout<<str[i]<<"\t"<<((toascii(str[i])-97)*key)%26<<"\t"<<c<<endl;
  }
}
void main()
{
  clrscr();
  MulCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download the code from here

Multiplicative Cipher: Decryption

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class AddCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
  int inverse(int);
};
void AddCiph :: gettext()
{
  cout<<"\nEnter the string: ";
  cin>>str;
}
void AddCiph :: getkey()
{
  cout<<"\nEnter the multiplicative cipher key: ";
  cin>>key;
}
int AddCiph :: inverse(int key)
{
  int r1,r2,r,q,t1,t2,t;
  r1=26;r2=key;
  t1=0;t2=1;
  while(r2>0)
  {
    q=r1/r2;
    r=r1-q*r2;
    r1=r2;r2=r;
    t=t1-q*t2;
    t1=t2;t2=t;
  }
  if(r1==1)
  {
    if(t1<0)
      t1+=26;
    return t1;
  }
}

void AddCiph :: convert()
{
  int h=strlen(str);
  int inv=inverse(key);
  for(int i=0;i<h;i++)
  {
    int dkey=(((toascii(str[i])-97)*inv))%26;
    char ch=dkey+97;
    cout<<str[i]<<"\t"<<dkey<<"\t"<<ch<<endl;
  }
}
void main()
{
  clrscr();
  AddCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download the code from here

0 comments:

Post a Comment