You are on page 1of 6

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<conio.h>

using namespace std;

class Substitution

char str[500];

public:

void sgetstring();

void sencryption();

void sdecryption();

};

void Substitution::sgetstring()

int size = 500;

cout << "\n Enter the Input String: ";

cin.getline(str, size, '\n');

void Substitution::sencryption()

int key, i, t1;

char c;

cout << "\n Enter Key: ";

cin >> key;

cout << "\n Encryption : ";


//Encryption

for (i = 0; i<strlen(str); i++)

c = str[i];

if (c == -1)

break;

if (isupper(c))

t1 = (int)c - (int) 'A';

t1 = (t1 + key) % 26;

t1 = t1 + (int) 'A';

cout << (char)t1;

else if (islower(c))

t1 = (int)c - (int) 'a';

t1 = (t1 + key) % 26;

t1 = t1 + (int) 'a';

cout << (char)t1;

else{

cout << c;

void Substitution::sdecryption()

int key, i, t1;


char c;

cout << "\n Decryption : \n";

for (key = 0; key<26; key++)

for (i = 0; i<strlen(str); i++)

c = str[i];

if (c == -1)

break;

if (isupper(c))

t1 = (int)c - (int) 'A';

t1 = (t1 - key + 26) % 26;

t1 = t1 + (int) 'A';

cout << (char)t1;

else if (islower(c))

t1 = (int)c - (int) 'a';

t1 = (t1 - key + 26) % 26;

t1 = t1 + (int) 'a';

cout << (char)t1;

else{

cout << c;

cout << "\n";


}

int main()

int choice;

Substitution s;

cout << "\nUsing Substitution Cipher Method\n";

s.sgetstring();

cout << "\n What do you want to perform: \n";

cout << "\n 1. Encryption \n 2. Decryption \n Enter: ";

cin >> choice;

if (choice == 1)

s.sencryption();

else

s.sdecryption();

cout << "\n\n\n Done";

getch();

return (0);
}

You might also like