1 / 7

Ass#2

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

kael
Download Presentation

Ass#2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ass#2 Encryption Program

  2. Required Functions • bool checkExistingFile(string inFile); • unsigned char encChar (unsigned char c, int j, int flag); • void encFile (string inFile, string outFile, int flag);

  3. 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.

  4. 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.

  5. checkExistingFile Function boolcheckFile(string fileName) { • Declare the stream • Open the stream • if fail to open return false elseclose the file and return true }

  6. 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); }

  7. 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 }

More Related