Encryption Client-Server

Assigned during fall 2005 for csci4061 — Introduction to Operating Systems, this is a client and server combination that encrypts files using a simple shift cipher. Obviously the practical applications for such a client and server are virtually non-existent, but it was an easy proof of concept of sorts, in network protocols, sockets, multithreading, and file I/O (in C). The README is pretty comprehensive on this project, so I won’t waste much more breath here, and just let you read it for yourself:

Spoiler
NAME
encrypt - encrypt files using a simple shift cipher

SYNOPSIS
encrypt-server num_threads port_number [-v]
encrypt-client host_name port_number [-v]

DESCRIPTION
Encrypt is a utility based in C for the encryption and decryption of 
files using a static byte offset shift cipher.

Encrypt is interactive, requiring user input in the form of encrypt
commands, each in the form of in_filename byte_offset.

The client is exited cleanly by sending an EOF (ctrl-d) character.

The encrypted files are automatically created as a separate file in
the working directory of the client, with the filename
in_filename.encrypt

If a byte_offset is known, decryption of a file can be accomplished
by 'encrypting' an in_filename.encrypt -byte_offset (a negative
byte_offset), which would save the decrypted file in
in_filename.encrypt.encrypt

All valid commands issued to the program are logged at the server,
indicating the thread that handled the request, the number of the
request being handled by the given thread, the socket number from which
the request was sent, and either the integer_add (byte shift), or a
description of an error encountered.

OPTIONS
There are two required options for encrypt-server: the number of
worker threads to spawn, and the port number on which to listen for
client requests. Both of these are integers, though their valid ranges
vary. The valid range for num_threads is [1,100] inclusive, and the 
valid range for port_number is [1,65535] inclusive (though ports below
1024 generally require root access to listen). Optionally, a third
switch can be passed to encrypt-server, which is a -v flag, resulting
in verbose debugging output to the terminal.


There are also two required options for encrypt-client, which are
host_name and port_number. The hostname option can contain either a 
hostname or fully qualified domain name (FQDN) resolvable by the
client system, or a standard dotted IP address. As with the server, the
valid range for ports is [1,65535] (root access is not required to
connect to a port number below 1024). The encryption client can also
accept an optional third flag, -v, to produce verbose debugging output
to the terminal.

NOTES
A makefile is provided for convenience. To compile encrypt, simply
run `make' from the directory containing encrypt-server.c and
encrypt-client.c.

encrypt_log is overwritten every time encrypt is called, as are any
in_filename.encrypt files that may already exist.

encrypt-client can be cleanly terminated by reaching an EoF or ctrl-d
on its standard input. encrypt-server must be terminated with ctrl-c.

BUGS
This program will not compile on Sun/Solaris machines due to the
absence of the thread-safe strerror_r function. Additionally, the
function char *strerror_r(int errnum, char *buf, size_t n) does
not function as documented, in that char *buf is not actually
used (but rather it returns the char *buf). This is contrary to the
function definition of int strerror_r() defined in the manpages.

There is a current limitation of 1024 unhandled requests in the
command buffer. This is due to the request buffer implementation as
a statically sized array rather than a linked list, as a conscious
decision due to the scope of the program's use. It seems unlikely that
the program will be called upon to encrypt over 1024 files at all, and
furthermore, will have time between requests for threads to process
these files, thus freeing up space in the request buffer (which will
wrap around to allow more than 1024 requests total, just not
simultaneous requests).

In addition to the problem with strerror_r on Sun/Solaris machines,
this program is not compatible to run a client and a server on machines
of different byte-ordering. This results in a workaround to enable
negative encryptionKeys, which were non-functional with the ntohs and
htons commands, as they return unsigned integers.

When compiled as currently coded on a linux machine under gcc 3.3.2,
there are no known bugs. Tests have been performed to check for
incorrectly formed commands, and present versus non-present files.

HOMEPAGE
http://www-users.itlabs.umn.edu/classes/Fall-2005/csci4061/assignments/asgn5/

AUTHOR
David R. Hedges  is the author of both this
documentation and the encrypt program.

2005-12-07T23:22-0600-drh

Below is the code from the encrypt client, which connects to the server, sends a file and shift key, and then saves the result it receives from the server (the encrypted file).

[syntax,encrypt/encrypt-client.c,C]