Encrypt and Decrypt of Text file(C++ Language)

0 ian ona · March 20, 2015
heyya guys i guys need help on how should i create a simple encrypt and decrypt text file with a substitution table of ASCII characters.? 

/* Program Description: the program should be able to encrypt and decrypt messages for security purposes. */

using namespace std;
char first[50];

void encrypt()
FILE *plainfile;
FILE *inputf;
FILE *subTab;
FILE *encrypt;
int a, b, c;
char inputen[20];
char sub;

if ((plainfile = fopen("plainfile.txt", "r")) == NULL)
cout << "Error! plainfile.txt cannot be found\n";
if ((plainfile = fopen("plainfile.txt", "r")) == NULL)
cout << "Error! plaintext.txt cannot be found\n";

if ((subTab = fopen("subtable.txt", "r")) == NULL)
cout << "Error! (subtable.txt) cannot be found\n" << endl;
inputf = fopen("plainfile.txt", "w");
while ((a = fgetc(plainfile)) != EOF)
fputc(a, inputf);

cout << "Enter File to be encryted: ";
cin >> inputen;

if ((encrypt = fopen(inputen, "w")) == NULL)
cout << "Error! (encrypted.txt) cannot be found\n";


void decrypt()

int main(void)
int menu;

cout << "Welcome To  Encryption/Decryption Program!....\n";
cout << "\n[1]-Data Encryption\n";
cout << "[2]-Data Decryption\n";
cout << "[3]-Exit\n";
cout << "Enter Choice: ";
cin >> menu;
if (menu == 1)
if (menu == 2)
if (menu == 4)


this is what I have made. but it doesn't decrypt. i also have a text file of substitution table of ascii characters on it. thanks a lot.

Post a Reply


Oldest  Newest  Rating
0 Dol Lod · March 20, 2015
Are you sure you are supposed to be doing  system("pause");system("cls"); when you call decrypt? 

If you are given freedom for this program a very basic cipher is character substitution, i.e. replace all a's with o's, o's with z's ,...

If you have a function called decrypt, at least make sure it actually decrypts. 

Anyway main ideas for encryption:
1) Read file to be encrypted and store the contents in a buffer. 
2) Provide a cipher or have the program itself use a cipher. 
3) Use the cipher to operate over the buffer encrypting it as needed. 
4) Write out the buffer to a file. 

This is just something to consider, but is there a chance for aliasing, encrypted file and inputted file have the same name. 

I'm assuming you are doing a very basic encryption with simple character substitutions. That means you can just use the cipher to generate an stl map going from a character to a character.

Here is a link for using maps:http://www.cplusplus.com/reference/map/map/operator[]

Then the map can do simple character replacements in the buffer. 
0 c student · March 20, 2015
If all you're after is a substitution cipher, then there is a really simple way to approach this.
What you'll need is two arrays: one for encrypting and the other for decrypting.  Here is an example:
#define 26 ALPHABET

char perm[ALPHABET] = "qwertyuiopasdfghjklzxcvbnm";
char orig[ALPHABET] = "abcdefghijklmnopqrstuvwxyz";

What your algorithm needs to do is read in a set of characters and then simply point to the corresponding cipher text within the array like such:
cipheredText = perm[plaintext-'a'];

An example of how this works: let's take cipheredText as the letter 'g'.  perm['g'-'a'] is perm[103-97] = perm[6] = 'u'.  As you might realize, you might need a separate uppercase array.

As for deciphering with this approach, it might be kinda brute force but it works fine since it's not that complex.  What you need to do is to match the cipher text's position in the perm array with the orig array, for example:
Let's take the cipher text 'u'.  'u' is perm[5], so orig[5] is 'g'.
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator