80 likes | 254 Views
Ass#2. Encryption Program. Required Functions. bool checkExistingFile (string inFile ); unsigned char encChar (unsigned char c , int j , int flag ); void encFile (string inFile , string outFile , int flag );. Before the main function. Include the required libraries
E N D
Ass#2 Encryption Program
Required Functions • bool checkExistingFile(string inFile); • unsigned char encChar (unsigned char c, int j, int flag); • void encFile (string inFile, string outFile, int flag);
Before the main function • Include the required libraries • Global variables: • string key = "5002,22enuJdeW"; //or any key • int n = key.length(); • If you allow the user to enter his own key then you have to find the length of the key and it should be valid (should not exceed 32 characters). • Prototypes of your defined functions.
main() Function Algorithm • Get from the user the input file name • Call the functioncheckExistingFileand if the file does not exist then loop to get an existing file name. • Get from the user the output file name • Ask the user to encrypt or decrypt and set the flag. • Call the function encFile.
checkExistingFile Function boolcheckFile(string fileName) { • Declare the stream • Open the stream • if fail to open return false elseclose the file and return true }
encChar Function unsigned char encChar (unsigned char c, int j, int flag) { • Find the index of the key iusing j • Initial value of j is 0 and should be incremented after each reading of a character from the file (initially j = 0 and i = 0) • i = j % n (key index iranges from 0 up to n-1) • Example: if j = n (n = key length) then the index i = n % n = 0 • if j = n+1 and then the index i = 1, if j = n+2 and then the index i = 2, … • if j = n+n and then the index i = 0 and so on… • Find the distance: d = int (key.at(i)) - 32 • According to the flag value, decide either to decrypt or to encrypt • return the result (character); }
encFile Function void encfile (string infile, string outfile, int flag) { • Open the streams • Initialize j to 0 • Read a character from the file use the get function; • Loop while the read character is noteof { • Encrypt / decrypt the character • Write it to the output file • Update j • Read another character from the file } • Close the files }