Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
117 views4 pages

Affine Cipher: Project Code

This document describes an affine cipher encryption/decryption program. The program begins by getting a message from the user and converting it to uppercase. It then converts each letter to a number based on its position in the alphabet. The program encrypts the message by applying the mathematical operation 5x+8 to each number, taking the result modulo 26. To decrypt, it reverses the process by applying 21(x-8) modulo 26. The encrypted/decrypted text is then displayed. The user can choose to encrypt/decrypt again or quit.

Uploaded by

Tariq Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views4 pages

Affine Cipher: Project Code

This document describes an affine cipher encryption/decryption program. The program begins by getting a message from the user and converting it to uppercase. It then converts each letter to a number based on its position in the alphabet. The program encrypts the message by applying the mathematical operation 5x+8 to each number, taking the result modulo 26. To decrypt, it reverses the process by applying 21(x-8) modulo 26. The encrypted/decrypted text is then displayed. The user can choose to encrypt/decrypt again or quit.

Uploaded by

Tariq Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

AFFINE CIPHER

Project code:
#include <iostream>//for input-output
#include <conio.h>//for screen pause
#include <string>//To use string
#include <iomanip>//for parametric manipulations-formatting outputs
#include <cctype>//Mandatory for strings
#include <algorithm>//For important algorithms
#include <cstdlib>//For clearing screen
#include <stdlib.h>//For clearing screen
#include <stdio.h>//For clear screen
using namespace std;
void main ()
{
string S=("ABCDEFGHIJKLMNOPQRSTUVWXYZ");//String containing alphabets
string s1;//Message to be written
//string s3;
int s2[100];//Integar array-used to convert alphabets into numericals by
comparison
int c=0;//USED TO CONTROL LOOP
char f=' ';//USED TO CONTROL LOOP
do
{
cout<<"Type Message: ";
getline(cin,s1);
s1.erase( remove( s1.begin(), s1.end(), ' ' ), s1.end() );//for removing spaces
int b=s1.length();//To determine the length of message to be encrypted
cout<<endl;
for(int i=0;i<=1;i++)//Just for formatting
{
for(int j=0;j<=78;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<setw(45)<<"ENCRYPTION"<<endl;
////////////////////////Conversion into uppercase/////////////////////
cout<<"Text :"<<setw(13);
for(int i=0;i<b;i++)
{
s1[i]=toupper(s1[i]);
cout<<s1[i]<<setw(5);
}
cout<<endl;
////////////////////////Conversion into uppercase/////////////////////
int d=S.length();//length of string S
//////////////////////////Conversion into numerics////////////////////
for(int i=0;i<b;i++)
{
for(int j=0;j<d;j++)
{
if(s1[i]==S[j])
{
s2[i]=j;
}//end of inner loop
}

}
cout<<"x :"<<setw(13);
for(int i=0;i<b;i++)
{
cout<<s2[i]<<setw(5);
}
cout<<endl;
//////////////////////////Conversion into numerics/////////////////////
/////////////////////////////////////5x+8//////////////////////////////
cout<<"5x+8 :"<<setw(13);
for(int i=0;i<b;i++)
{
s2[i]=(5*s2[i]+8);
cout<<s2[i]<<setw(5);
}
cout<<endl;
/////////////////////////////////////5x+8///////////////////////////////
////////////////////////////////////mod26///////////////////////////////
cout<<"mod26:"<<setw(13);
for(int i=0;i<b;i++)
{
if(s2[i]>26)
{
s2[i]=s2[i]%26;
cout<<s2[i]<<setw(5);
}

else
cout<<s2[i]<<setw(5);
}
cout<<endl;
////////////////////////////////////mod26///////////////////////////////
////////////////////////////ENCRYPTED TEXT//////////////////////////////
cout<<"Encrypted Text:"<<setw(4);
int c=0;
for(int i=0;i<b;i++)
{
c=s2[i];
cout<<S[c]<<setw(5);
}
cout<<endl;
cout<<endl;
////////////////////////////ENCRYPTED TEXT//////////////////////////////
cout<<setw(45)<<"DECRYPTION"<<endl;
cout<<"Encrypted Text:"<<setw(4);
for(int i=0;i<b;i++)
{
c=s2[i];
cout<<S[c]<<setw(5);
}
////////////////////////////ENCRYPTED TEXT//////////////////////////////
cout<<endl;
////////////////////////////////////Y///////////////////////////////////
cout<<"y :"<<setw(13);
for(int i=0;i<b;i++)
{
cout<<s2[i]<<setw(5);
}
////////////////////////////////////Y///////////////////////////////////
cout<<endl;
/////////////////////////////21*(Y-8)///////////////////////////////////
cout<<"21(y-8):"<<setw(11);
for(int i=0;i<b;i++)
{
s2[i]=21*(s2[i]-8);
cout<<s2[i]<<setw(5);
}
/////////////////////////////21*(Y-8)///////////////////////////////////
cout<<endl;
//////////////////////////////MOD 26////////////////////////////////////
cout<<"mod26:"<<setw(13);
for(int i=0;i<b;i++)
{
if(s2[i]>26)
{
s2[i]=s2[i]%26;
cout<<s2[i]<<setw(5);
}
else if(s2[i]<0)
{
s2[i]=s2[i]%26;
s2[i]+=26;
cout<<s2[i]<<setw(5);
}
else
cout<<s2[i]<<setw(5);
}
cout<<endl;
/////////////////////////////Decrypted text////////////////////////////////////
cout<<"Decrypted Text:"<<setw(4);
int k=0;
for(int i=0;i<b;i++)
{
k=s2[i];
cout<<S[k]<<setw(5);
}
/////////////////////////////Decrypted text////////////////////////////////////
cout<<endl;
cout<<endl;
for(int i=0;i<=1;i++)
{
for(int j=0;j<=78;j++)
{
cout<<"*";
}
cout<<endl;
}
cout<<endl;
cout<<"Press R if you want to encrypt/decrypt again, or press any other button to
quit"<<endl;
cin>>f;

if(f=='R' || f=='r')
{
c=0;
cin.ignore();
system("cls");
}
else
break;
}while(c<1);
cout<<"Have a nice day :-) "<<endl;
_getch();
}

You might also like