Tuesday, 27 November 2012

Additive Cipher (C++)


Encryption using Additive Cipher in C++


#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();
};
void AddCiph :: gettext()
{
  cout<<"\nEnter the plain string: ";
  cin>>str;
}
void AddCiph :: getkey()
{
  cout<<"\nEnter the additive cipher key: ";
  cin>>key;
}
void AddCiph :: 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();
  AddCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download code from here


Decryption using Additive Cipher in C++

#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();
};
void AddCiph :: gettext()
{
  cout<<"\nEnter the decrypted string: ";
  cin>>str;
}
void AddCiph :: getkey()
{
  cout<<"\nEnter the additive decipher key: ";
  cin>>key;
}
void AddCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    int dkey=(((toascii(str[i])-97)-key))%26;
    if(dkey<0)
      dkey+=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 code from here

0 comments:

Post a Comment