C++ Programming Code Examples C++ > File Operations Code Examples Programming Code to Encrypt Files Programming Code to Encrypt Files To encrypt and decrypt file's content in C++ programming, you have to ask to enter the file name with extension to encrypt and decrypt the content present inside the file. Now open that file using the function open() and start reading the file's content, character by character, at the time of reading make some algorithm to encrypt the content of the file and place the content in the temporary file then after encrypting all content of the file place the content in the original file and later use the same algorithm to decrypt that file's content Following C++ program ask to the user to enter file name to encrypt its content: /* C++ Program - Encrypt File */ #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> #include<stdlib.h> void main() { clrscr(); char fname[20], ch, choice; fstream fps, fpt; cout<<"Enter file name (with extension like file.txt) to encrypt : "; gets(fname); fps.open(fname); if(!fps) { cout<<"Error in opening file..!!"; cout<<"\nPress any key to exit..."; getch(); exit(1); } fpt.open("temp.txt") if(!fpt) { cout<<"Error in creating temp.txt file..!!"; fps.close(); cout<<"\nPress any key to exit..."; getch(); exit(2); } while(fps.eof()==0) { fps>>ch; ch=ch+100; fpt<<ch; } fps.close(); fpt.close(); fps.open(fname); if(!fps) { cout<<"Error in opening source file..!!"; cout<<"\nPress any key to exit..."; getch(); exit(3); } fpt.open("temp.txt"); if(!fpt) { cout<<"Error in opening temp.txt file...!!"; fps.close(); cout<<"\nPress any key to exit..."; getch(); exit(4); } while(fpt.eof()==0) { fpt>>ch; fps<<ch; } cout<<"File "<<fname<<" encrypted successfully..!!"; cout<<"\nPress any key to exit..."; fps.close(); fpt.close(); getch(); }