Operating Systems Homework assignment #1 In this assignment, you will have to build two command line utilities for file encryption and decryption. The Windows file encryption utility called file_encrypt.exe will accept three arguments: path to a file to be encrypted, path to a file which contain encryption key and path to store resulting encrypted file. Encryption will be performed by bitwise XOR-ing the content of the input file and the key file. Please notice following constrains: 1. Input/output and key files could be relatively large (up to 1Gb), so you are not allowed to keep in memory(RAM) more than 4K data from any of these files. 2. If output file already exists you should overwrite it 3. Key file and input file does not have to be of the same size. In case the key file is larger than the input file, only part of the key file will be used. 4. If the input file is longer than the key file, you should reuse bits of the key file by restarting reading from the beginning of the key file Create Linux file_decript utility for file decryption. The utility will accept three command line arguments: path to an encrypted file, path to a key file and path to store decrypted file content. Decryption will be performed by bitwise XOR of the content of the encrypted file and the key file. Same constrains as in encryption utility should be considered. Test your solution by encrypting/decrypting several diferent files (try different sizes) and make sure that you get the original file after encrypt/decrypt operation. Background Here we will provide some information on the system calls that you will need to use. The information is not comprehensive; on Linux, consult the manual page (e.g., use man 2 open; the number 2 is the section of the manual that describes system calls; section 3 describes the C library, and other sections describe utilities, files, and other things). On Windows, consult MSDN (on the internet). We’ll start with Linux. #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); The flags include the options O_RDONLY, O_WRONLY, O_RDWR to indicate whether you want to read or write the file, O_CREAT to indicate that you want to create the file if it does not exist, and O_EXCL to indicate that you want to create the file only if it does not exist. There are also flags that allow you to indicate that you want to append data to an existing file, or to truncate a file (remove its existing contents). There are a few more flags that are not relevant to this exercise. The mode argument allows you to specify the access permissions of new files. Here you can use S_IRUSR | S_IWUSR, which will give you (the creator of the file) read and write permissions; others will not be able to access the file. When you open a file, you get an integer that’s called a file descriptor. You use it later to access the file. Here and in most POSIX system calls, an error is indicated with a return value of -1. The actual error code is stored in a global variable called errno. The function perror is a good way to print information about what went wrong. When you are done with a file, you need to close it with close. Read and write from/to the file using read and write; their interface is simple (see the man pages for details). To find out the size of a file, you’ll need to use fstat (or one of its sisters, lstat and stat): #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf); The member st_size of the stat structure indicates the file size. On Windows platform you should consider using following system calls (API calls) to work with files o o o o CreateFile for opening and creating new files WriteFile/ReadFile for writing into and reading from file CloseHandle for closing file handle GetFileSize for retrieving file size Consult MSDN pages for exact arguments for these functions. Below are some points to keep in mind while working with files o o o o o o Don’t forget to free system resources by closing every handle using CloseHandle function Use correct create flags OPEN_EXISTING/CREATE_ALWAYS in order to open existing file or create a new one Use FILE_ATTRIBUTE_NORMAL file attribute Request correct access rights (GENERIC_READ/GENERIC_WRITE) depending on what you are going to do with the file (write to it or read data from it) To be friendly to the environment specify what other programs can do with the file while you are working with it (FILE_SHARE_READ/FILE_SHARE_WRITE) Provide default NULL value for security attributes and template file arguments of CreateFile function