You are on page 1of 38

C++ program to encrypt and decrypt the

string
In this example, you will learn simple C++ program to encrypt and decrypt the
string using two different encryption algorithms i.e. Caesar Cypher and RSA.

Encryption/Decryption using Caesar Cypher Algorithm

Encryption/Decryption using RSA Algorithm

Encryption basically means encoding a particular message or information so that it can’t


be read by other person and decryption is the process of decoding that message to
make it readable.

Method 1: C++ program to encrypt and decrypt the


string using Caesar Cypher Algorithm.

We have used a simple method of adding and subtracting a key value


for encryption and decryption.
For encrypting a string, key-value ‘2’ is added to the ASCII value of the characters in the
string.

Similarly, for decrypting a string, key-value ‘2’ is subtracted from the ASCII value of the
characters.

Let’s take a deeper look at the source code:

//Simple C++ program to encrypt and decrypt a string

#include <iostream>

using namespace std;

int main()

int i, x;

char str[100];

cout << "Please enter a string:\t";

cin >> str;

cout << "\nPlease choose following options:\n";

cout << "1 = Encrypt the string.\n";

cout << "2 = Decrypt the string.\n";

cin >> x;
//using switch case statements

switch(x)

//first case for encrypting a string

case 1:

for(i = 0; (i < 100 && str[i] != '\0'); i++)

str[i] = str[i] + 2; //the key for encryption is 3 that is added to ASCII


value

cout << "\nEncrypted string: " << str << endl;

break;

//second case for decrypting a string

case 2:

for(i = 0; (i < 100 && str[i] != '\0'); i++)

str[i] = str[i] - 2; //the key for encryption is 3 that is subtracted to


ASCII value

cout << "\nDecrypted string: " << str << endl;

break;
default:

cout << "\nInvalid Input !!!\n";

return 0;

Output

#Encrypting
#Decrypting

Explanation

In the above program, if you change the value of key then the encrypted value will be
different.

If the user enters other value than 1 or 2 it will show Invalid Input.

Method 2: C++ program to encrypt and decrypt


the string using RSA algorithm.

RSA algorithm is bit complex than Ceaser Cypher. It involves the use of public and
private key, where the public key is known to all and used for encryption.

On the other hand, Private key is only used to decrypt the encrypted message.

Here is the deeper look at the steps of encryption algorithm:


1: Creating Keys

 Select two large prime numbers x and y


 Compute n = x * y
where n is the modulus of private and the public key
 Calculate totient function, ø (n) = (x − 1)(y − 1)
 Choose an integer e such that e is coprime to ø(n) and 1 < e < ø(n).
e is the public key exponent used for encryption
 Now choose  d, so that d · e mod ø (n) = 1, i.e., >code>d is the multiplicative
inverse of e in mod ø (n)

2: Encrypting Message

Messages are encrypted using the Public key generated and is known to all.

The public key is the function of both e and n i.e. {e,n}.

If M is the message(plain text), then ciphertext

C = M ^ n( mod n )

3: Decrypting Message

The private key is the function of both d and n i.e {d,n}.

If C is the encrypted ciphertext, then the plain decrypted text M is

M = C ^ d ( mod n )

You might be wondering how to write a source code for this program.

So let’s look at the program.

//C++ program for encryption and decryption

#include<iostream>

#include<stdlib.h>

#include<math.h>

#include<string.h>

using namespace std;


int x, y, n, t, i, flag;

long int e[50], d[50], temp[50], j;

char en[50], m[50];

char msg[100];

int prime(long int); //function to check for prime number

void encryption_key();

long int cd(long int);

void encrypt();

void decrypt();

int main()

cout << "\nENTER FIRST PRIME NUMBER\n";

cin >> x;

//checking whether input is prime or not

flag = prime(x);

if(flag == 0)

cout << "\nINVALID INPUT\n";

exit(0);
}

cout << "\nENTER SECOND PRIME NUMBER\n";

cin >> y;

flag = prime(y);

if(flag == 0 || x == y)

cout << "\nINVALID INPUT\n";

exit(0);

cout << "\nENTER MESSAGE OR STRING TO ENCRYPT\n";

cin >> msg;

for(i = 0; msg[i] != NULL; i++)

m[i] = msg[i];

n = x * y;

t = (x - 1) * (y - 1);

encryption_key();
cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";

for(i = 0; i < j - 1; i++)

cout << "\n" << e[i] << "\t" << d[i];

encrypt();

decrypt();

return 0;

} //end of the main program

int prime(long int pr)

int i;

j = sqrt(pr);

for(i = 2; i <= j; i++)

if(pr % i == 0)

return 0;

return 1;

}
//function to generate encryption key

void encryption_key()

int k;

k = 0;

for(i = 2; i < t; i++)

if(t % i == 0)

continue;

flag = prime(i);

if(flag == 1 && i != x && i != y)

e[k] = i;

flag = cd(e[k]);

if(flag > 0)

d[k] = flag;

k++;

if(k == 99)
break;

long int cd(long int a)

long int k = 1;

while(1)

k = k + t;

if(k % a == 0)

return(k/a);

//function to encrypt the message

void encrypt()

long int pt, ct, key = e[0], k, len;

i = 0;
len = strlen(msg);

while(i != len)

pt = m[i];

pt = pt - 96;

k = 1;

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

k = k * pt;

k = k % n;

temp[i] = k;

ct= k + 96;

en[i] = ct;

i++;

en[i] = -1;

cout << "\n\nTHE ENCRYPTED MESSAGE IS\n";

for(i=0; en[i] != -1; i++)

cout << en[i];


}

//function to decrypt the message

void decrypt()

long int pt, ct, key = d[0], k;

i = 0;

while(en[i] != -1)

ct = temp[i];

k = 1;

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

k = k * ct;

k = k % n;

pt = k + 96;

m[i] = pt;

i++;

m[i] = -1;
cout << "\n\nTHE DECRYPTED MESSAGE IS\n";

for(i = 0; m[i] != -1; i++)

cout << m[i];

cout << endl;

Output
Caesar Cipher in Cryptography
 Difficulty Level : Easy
  Last Updated : 12 Aug, 2019
The Caesar Cipher technique is one of the earliest and simplest method of encryption
technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is
replaced by a letter some fixed number of positions down the alphabet. For example with
a shift of 1, A would be replaced by B, B would become C, and so on. The method is
apparently named after Julius Caesar, who apparently used it to communicate with his
officials.
Thus to cipher a given text we need an integer value, known as shift which indicates the
number of position each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming the
letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a
letter by a shift n can be described mathematically as.

(Encryption Phase with shift n)

(Decryption Phase with shift n)

Examples :

Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW

Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
Algorithm for Caesar Cipher:
Input:
1. A String of lower case letters, called Text.
2. An Integer between 0-25 denoting the required shift.
Procedure:
 Traverse the given text one character at a time .
 For each character, transform the given character as per the rule, depending on
whether we’re encrypting or decrypting the text.
 Return the new string generated.
Program that receives a Text (string) and Shift value( integer) and returns the encrypted
text.

 C++
 Java
 Python
 C#
 PHP
filter_none
edit
play_arrow
brightness_4

// A C++ program to illustrate Caesar Cipher Technique

#include <iostream>

using namespace std;

  

// This function receives text and shift and

// returns the encrypted text

string encrypt(string text, int s)

    string result = "";

  

    // traverse text

    for (int i=0;i<text.length();i++)


    {

        // apply transformation to each character

        // Encrypt Uppercase letters

        if (isupper(text[i]))

            result += char(int(text[i]+s-65)%26 +65);

  

    // Encrypt Lowercase letters

    else

        result += char(int(text[i]+s-97)%26 +97);

    }

  

    // Return the resulting string

    return result;

  

// Driver program to test the above function

int main()

    string text="ATTACKATONCE";
    int s = 4;

    cout << "Text : " << text;

    cout << "\nShift: " << s;

    cout << "\nCipher: " << encrypt(text, s);

    return 0;

Output:
Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
How to decrypt?
We can either write another function decrypt similar to encrypt, that’ll apply the given
shift in the opposite direction to decrypt the original text. However we can use the cyclic
property of the cipher under modulo , hence we can simply observe
Cipher(n) = De-cipher(26-n)
Hence, we can use the same function to decrypt, instead we’ll modify the shift value such
that shift = 26-shift (Refer this for a sample run in C++).
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would
like to contribute, you can also write an article and mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory
concepts for SDE interviews with the CS Theory Course at a student-friendly price and
become industry ready.
Caesar Cipher Algorithm Program in C/C++

BY JAZIB

 ON MAR 24, 2020


 IN  CRYPTOGRAPHY

In cryptography, a cipher (or cypher) is an algorithm for


performing encryption or decryption—a series of well-defined steps that can be
followed as a procedure. An alternative, less common term is encipherment. To
encipher or encode is to convert information into cipher or code. In common
parlance, “cipher” is synonymous with “code“, as they are both a set of steps that
encrypt a message; however, the concepts are distinct in cryptography,
especially classical cryptography.

Codes generally substitute different length strings of character in the output,


while ciphers generally substitute the same number of characters as are input.
There are exceptions and some cipher systems may use slightly more, or fewer,
characters when output versus the number that was input.

In this post, we will discuss the Caesar Cipher Algorithm and also write a


program for the Caesar Cipher algorithm. Caesar Cipher is one of the simplest
and most widely known encryption techniques. It is a type of substitution cipher
in which each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet. For example, with a left shift of 3, D would be
replaced by A, E would become B, and so on. The method is named after Julius
Caesar, who used it in his private correspondence.

We will use C++ to write this algorithm due to the standard template library
support. Hence, we will write the program of Caesar Cipher algorithm in C++,
although, it’s very similar to C.

Encryption
INPUT:
line 1: key (key)
line 2: message (s)

OUTPUT:
line 1: Encrypted message (t)

The following is the Caesar Cipher encryption algorithm program in C++.


#include<iostream>
#include<string>
using namespace std;
int main(){
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message\n";
cin>>s;
for(i=0;i<s.size();i++){
t+=(s[i]-'A'+key)%26+'A';
}
cout<<"\n\nEncrypted message is "<<t<<'\n';
return 0;
}
OUTPUT:
Enter the key
4
Enter the message
HELLOWORLD

Encrypted message is LIPPSASVPH

Decryption
INPUT:
line 1: key (key)
line 2: message (s)

OUTPUT:
line 1: decrypted message (t)

The following is the Caesar Cipher decryption algorithm program in C++.


#include<iostream>
#include<string>
using namespace std;
int main(){
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message to decrypt\n";
cin>>s;
for(i=0;i<s.size();i++){
t+=(s[i]-'A'-key+26)%26+'A';
}
cout<<"\n\nDecrypted message is "<<t<<'\n';
return 0;
}
OUTPUT:
Enter the key
4
Enter the message to decrypt
LIPPSASVPH

Decrypted message is HELLOWORLD

C++ Program to Implement Caesar Cypher


« Prev
This is a C++ Program to implement Caesar Cipher Encryption algorithm. This is the
simplest of all, where every character of the message is replaced by its next 3rd character.
Here is source code of the C++ Program to Implement Caesar Cypher. The C++ program is
successfully compiled and run on a Linux system. The program output is also shown below.

1.#include <iostream>
2.#include <string>
3.using namespace std;
4.char caesar(char);
5.int main()
6.{
7. string input;
8. do
9. {
10. cout << "Enter cipertext and press enter to continue."
<< endl;
11. cout << "Enter blank line to quit." << endl;
12. getline(cin, input);
13. string output = "";
14. for (int x = 0; x < input.length(); x++)
15. {
16. output += caesar(input[x]);
17. }
18. cout << output << endl;
19. }
20. while (!input.length() == 0);
21. } //end main
22.  
23. char caesar(char c)
24. {
25. if (isalpha(c))
26. {
27. c = toupper(c); //use upper to keep from having to use
two seperate for A..Z a..z
28. c = (((c - 65) + 13) % 26) + 65;
29. }
30. //if c isn't alpha, just send it back.
31. return c;
32. }

Output:

$ g++ CaesarCipher.cpp
$ a.out
 
Enter cipertext and press enter to continue.
Enter blank line to quit.
Sanfoundry
FNASBHAQEL
Enter cipertext and press enter to continue.
Enter blank line to quit.
 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.


Here’s the list of Best Reference Books in C++ Programming, Data Structures and
Algorithms.
Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our
social networks below and stay updated with latest contests, videos, internships and jobs!
C++ Program to Implement Caesar
Cypher
C++Server Side ProgrammingProgramming

It is a mono-alphabetic cipher wherein each letter of the plaintext is substituted by


another letter to form the ciphertext. It is a simplest form of substitution cipher scheme.
This cryptosystem is generally referred to as the Shift Cipher. The concept is to replace
each alphabet by another alphabet which is ‘shifted’ by some fixed number between 0
and 25.
For this type of scheme, both sender and receiver agree on a ‘secret shift number’ for
shifting the alphabet. This number which is between 0 and 25 becomes the key of
encryption.
The name ‘Caesar Cipher’ is occasionally used to describe the Shift Cipher when the
‘shift of three’ is used.

Process
 In order to encrypt a plaintext letter, the sender positions the sliding ruler
underneath the first set of plaintext letters and slides it to LEFT by the number of
positions of the secret shift.
 The plaintext letter is then encrypted to the ciphertext letter on the sliding ruler
underneath. The result of this process is depicted in the following illustration for
an agreed shift of three positions. In this case, the plaintext ‘tutorial’ is encrypted
to the ciphertext ‘wxwruldo’. Here is the ciphertext alphabet for a Shift of 3 −

 On receiving the ciphertext, the receiver who also knows the secret shift,
positions his sliding ruler underneath the ciphertext alphabet and slides it to
RIGHT by the agreed shift number, 3 in this case.
 He then replaces the ciphertext letter by the plaintext letter on the sliding ruler
underneath. Hence the ciphertext ‘wxwruldo’ is decrypted to ‘tutorial’. To decrypt
a message encoded with a Shift of 3, generate the plaintext alphabet using a
shift of ‘-3’ as shown below −

Here is the implementation of above process in C++.


Steps and pseudocodes
Take the message and key as input −
For encryption

 Input: tutorial.
 Output: wxwruldo

For decryption

 Input: wxwruldo
 Output: tutorial

For encryption
Begin
   For i = 0 to msg[i] != '\0'
      ch = msg[i]
   //encrypt for lowercase letter
      If (ch >= 'a' and ch <= 'z')
         ch = ch + key
         if (ch > 'z')
            ch = ch - 'z' + 'a' - 1
         done
         msg[i] = ch
   //encrypt for uppercase letter
      else if (ch >= 'A' and ch <= 'Z')
         ch = ch + key
         if (ch > 'Z')
            ch = ch - 'Z' + 'A' - 1
         done
         msg[i] = ch
      done
   done
   Print Encrypted message
End
For decryption
Begin
   For i = 0 to msg[i] != '\0'
      ch = msg[i]
   //decrypt for lowercase letter
      if(ch >= 'a' and ch <= 'z')
         ch = ch - key
         if (ch < 'a')
            ch = ch +'z' - 'a' + 1
      done
      msg[i] = ch
   //decrypt for uppercase letter
      else if (ch >= 'A' and ch <= 'Z')
         ch = ch + key
         if (ch < 'A')
            ch = ch + 'Z' - 'A' + 1
         done
         msg[i] = ch
      done
   done
   Print decrypted message
End
Example
#include<iostream>

#include<string.h>

using namespace std;

int main() {

   cout<<"Enter the message:\n";

   char msg[100];

   cin.getline(msg,100); //take the message as input

   int i, j, length,choice,key;

   cout << "Enter key: ";

   cin >> key; //take the key as input

   length = strlen(msg);

   cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";

   cin>>choice;

   if (choice==1) //for encryption{

      char ch;

      for(int i = 0; msg[i] != '\0'; ++i) {

         ch = msg[i];

         //encrypt for lowercase letter

         If (ch >= 'a' && ch <= 'z'){


            ch = ch + key;

            if (ch > 'z') {

               ch = ch - 'z' + 'a' - 1;

            }  

            msg[i] = ch;

         }

         //encrypt for uppercase letter

         else if (ch >= 'A' && ch <= 'Z'){

            ch = ch + key;

            if (ch > 'Z'){

               ch = ch - 'Z' + 'A' - 1;

            }

            msg[i] = ch;

         }

      }

      printf("Encrypted message: %s", msg);

   }

   else if (choice == 2) { //for decryption

      char ch;

      for(int i = 0; msg[i] != '\0'; ++i) {

         ch = msg[i];

         //decrypt for lowercase letter

         if(ch >= 'a' && ch <= 'z') {

            ch = ch - key;

            if(ch < 'a'){

               ch = ch + 'z' - 'a' + 1;


            }

            msg[i] = ch;

         }

         //decrypt for uppercase letter

         else if(ch >= 'A' && ch <= 'Z') {

            ch = ch - key;

            if(ch < 'A') {

               ch = ch + 'Z' - 'A' + 1;

            }

            msg[i] = ch;

         }

      }

      cout << "Decrypted message: " << msg;

   }

Output
For encryption:
Enter the message:
tutorial
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
1
Encrypted message: wxwruldo

For decryption:
Enter the message:
wxwruldo
Enter key: 3
Enter your choice
1. Encryption
2. Decryption
2
Decrypted message: tutorial
Caesar Cipher in C and C++ [Encryption &
Decryption]
Get program for caesar cipher in C and C++ for encryption and decryption.

What is Caesar Cipher?

It is one of the simplest encryption technique in which each character in plain text is
replaced by a character some fixed number of positions down to it.

For example, if key is 3 then we have to replace character by another character that is
3 position down to it. Like A will be replaced by D, C will be replaced by F and so on.

For decryption just follow the reverse of encryption process.

Below I have shared program to implement caesar cipher in C and C++.

Also Read: Vigenere Cipher in C and C++

Program for Caesar Cipher in C

Encryption
1 #include<stdio.h>

2  

3 int main()

4 {

5 char message[100], ch;

6 int i, key;

8 printf("Enter a message to encrypt: ");


9 gets(message);
10 printf("Enter key: ");
11 scanf("%d", &key);
12

13 for(i = 0; message[i] != '\0'; ++i){


14 ch = message[i];
15

16 if(ch >= 'a' && ch <= 'z'){


17 ch = ch + key;
18

19
if(ch > 'z'){
20
ch = ch - 'z' + 'a' - 1;
21
}
22

23
message[i] = ch;
24
}
25
else if(ch >= 'A' && ch <= 'Z'){
26
ch = ch + key;
27
28 if(ch > 'Z'){

29 ch = ch - 'Z' + 'A' - 1;

30 }

31

32 message[i] = ch;

33 }

34 }

35

36 printf("Encrypted message: %s", message);


37

38 return 0;
39 }

Output
Enter a message to encrypt: axzd
Enter key: 4
Encrypted message: ebdh

Decryption

1 #include<stdio.h>

2  

3 int main()

4 {

5 char message[100], ch;

6 int i, key;

8 printf("Enter a message to decrypt: ");


9 gets(message);
10 printf("Enter key: ");
scanf("%d", &key);
11

12

13 for(i = 0; message[i] != '\0'; ++i){

14 ch = message[i];

15

16 if(ch >= 'a' && ch <= 'z'){

17 ch = ch - key;

18

19 if(ch < 'a'){

20 ch = ch + 'z' - 'a' + 1;

21 }

22

23 message[i] = ch;

24 }

25 else if(ch >= 'A' && ch <= 'Z'){

26 ch = ch - key;

27

28 if(ch < 'A'){


29 ch = ch + 'Z' - 'A' + 1;
30 }
31

32 message[i] = ch;
33 }
34 }
35

36 printf("Decrypted message: %s", message);


37

38
return 0;
39
}

Output
Enter a message to decrypt: ebdh
Enter key: 4
Decrypted message: axzd

Program for Caesar Cipher in C++

Encryption

1 #include<iostream>

2  

3 using namespace std;

4  

5 int main()

6 {

7 char message[100], ch;

8 int i, key;

10 cout << "Enter a message to encrypt: ";


11 cin.getline(message, 100);
12 cout << "Enter key: ";
13 cin >> key;
14

15 for(i = 0; message[i] != '\0'; ++i){


16 ch = message[i];
17

18 if(ch >= 'a' && ch <= 'z'){


19 ch = ch + key;
20

21
if(ch > 'z'){
22 ch = ch - 'z' + 'a' - 1;

23 }

24

25 message[i] = ch;

26 }

27 else if(ch >= 'A' && ch <= 'Z'){

28 ch = ch + key;

29

30 if(ch > 'Z'){

31 ch = ch - 'Z' + 'A' - 1;

32 }

33

34 message[i] = ch;
35 }
36 }
37

38 cout << "Encrypted message: " << message;


39

40 return 0;
41 }

Output
Enter a message to encrypt: asd zf
Enter key: 3
Encrypted message: dvg ci

Decryption

1 #include<iostream>

2  
3 using namespace std;

4  

5 int main()

6 {

7 char message[100], ch;

8 int i, key;

10 cout << "Enter a message to decrypt: ";


11 cin.getline(message, 100);
12 cout << "Enter key: ";
13 cin >> key;
14

15 for(i = 0; message[i] != '\0'; ++i){


16 ch = message[i];
17

18 if(ch >= 'a' && ch <= 'z'){


19 ch = ch - key;
20

21
if(ch < 'a'){
22
ch = ch + 'z' - 'a' + 1;
23
}
24

25
message[i] = ch;
26
}
27
else if(ch >= 'A' && ch <= 'Z'){
28
ch = ch - key;
29

30
if(ch > 'a'){
31
ch = ch + 'Z' - 'A' + 1;
32
}
33

34 message[i] = ch;

35 }

36 }

37

38 cout << "Decrypted message: " << message;


39

40 return 0;
41 }

Output
Enter a message to decrypt: az GjK
Enter key: 2
Decrypted message: yx EhI

C++ Caesar Cipher File encryption and decryption


program source code
C++ program for encrypting and decrypting any file using Caesar cipher and any key
entered by the user.

You may even use this as an assignment or mini project in B. Tech. or network security
subject by adding little gui and improving the source code.  Feel free to use, modify and share
the code... Knowledge is always free !!!

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Caesar
{
public: void encrypt(char *inp,char *out,int key);
void decrypt(char *inp,char *out,int key);
void readText(char *inp);
};
void Caesar::encrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key;
buf%=26;
buf+='A';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::decrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key;
buf%=26;
buf+='a';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::readText(char *inp)
{
ifstream input;
char buf;
input.open(inp);
cout<<"\n\n <--- "<<inp<<" --->\n";
buf=input.get();
while(!input.eof())
{
cout<<buf;
buf=input.get();
}
input.close();
}
void main()
{
Caesar a;
int choice,key;
char inp[30],out[30];
clrscr();
cout<<"\n Enter input file: ";
cin>>inp;
cout<<"\n Enter output file: ";
cin>>out;
cout<<"\n Enter key: ";
cin>>key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): ";
cin>>choice;
if(choice==1)
a.encrypt(inp,out,key);
else if(choice==2)
a.decrypt(inp,out,key);
else cout<<"\n\n Unknown choice";
getch();
}

You might also like