0% found this document useful (0 votes)
4 views3 pages

Encryption & Decryption (Source Code)

The document contains C++ code that implements a simple file encryption and decryption program. Users can choose to encrypt or decrypt a file by providing a file name and a key, with the option to specify a file type for the output during decryption. The program reads the input file character by character, modifies each character based on the key, and writes the result to a new output file.

Uploaded by

Piyush Raj
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)
4 views3 pages

Encryption & Decryption (Source Code)

The document contains C++ code that implements a simple file encryption and decryption program. Users can choose to encrypt or decrypt a file by providing a file name and a key, with the option to specify a file type for the output during decryption. The program reads the input file character by character, modifies each character based on the key, and writes the result to a new output file.

Uploaded by

Piyush Raj
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/ 3

#include <iostream>

#include <fstream>
#include <conio.h>

using namespace std;

int main ()
{
int ans, key;
string in;
cout<<"Press 1 to Encrypt file.\nPress 2 to Decrypt file."<<endl;
cin>>ans;
cout<<"Enter File Name -"<<endl;
cin>>in;
cout<<"Enter Key -"<<endl;
cin>>key;
if (ans == 1)
{
char c;
ifstream fin;
ofstream fout;
fin.open(in.c_str(), ios::binary);
in = "output";
fout.open(in.c_str(), ios::binary);
while (!fin.eof())
{
fin>>noskipws>>c;
int temp = (c + key);
fout<<(char)temp;
}
fin.close();
fout.close();
}
else if (ans == 2)
{
char c;
ifstream fin;
ofstream fout;
fin.open(in.c_str(), ios::binary);
in = "qutput";
fout.open(in.c_str(), ios::binary);
while (!fin.eof())
{
fin>>noskipws>>c;
int temp = (c - key);
fout<<(char)temp;
}
fin.close();
fout.close();
}
getch();//If you want to remove this you can. Then you can also remove conio.h header.
}

Moded to ask file type -

#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

int main ()
{
int ans, key;
string in;
cout<<"Press 1 to Encrypt file.\nPress 2 to Decrypt file."<<endl;
cin>>ans;
cout<<"Enter File Name -"<<endl;
cin>>in;
cout<<"Enter Key -"<<endl;
cin>>key;
if (ans == 1)
{
char c;
ifstream fin;
ofstream fout;
fin.open(in.c_str(), ios::binary);
in = "output";
fout.open(in.c_str(), ios::binary);
while (!fin.eof())
{
fin>>noskipws>>c;
int temp = (c + key);
fout<<(char)temp;
}
fin.close();
fout.close();
}
else if (ans == 2)
{
string ft;
cout<<"Enter File Type -"<<endl;
cin>>ft;//assumed that they have entered type with '.' in front.
char c;
ifstream fin;
ofstream fout;
fin.open(in.c_str(), ios::binary);
in = "qutput" + ft;
fout.open(in.c_str(), ios::binary);
while (!fin.eof())
{
fin>>noskipws>>c;
int temp = (c - key);
fout<<(char)temp;
}
fin.close();
fout.close();
}
getch();//If you want to remove this you can. Then you can also remove conio.h header.
}

You might also like