Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
ECE 353 Computer Systems Laboratory I
Lab 1: Encryption and Decryption
Due: October 3, 2016
In this lab, you will code in C an algorithm for encryption and decryption.
This is a symmetric algorithm, meaning that the same operations are used for both encryption
and decryption. The algorithm consists of generating a key stream starting from an initial key of
length up to 256 bytes. This key stream is bit-wise ex-ored with the stream of bytes representing
the message to be encrypted. (These bytes are in the form of chars; recall that a char variable
is one byte long.)
Decryption consists of generating the same key stream and bit-wise ex-oring the key stream
with the encrypted bytes. The second ex-or operation reverses the result of the first.
The algorithm for generating the key stream is as follows. Assume the key is in array key[]
of length kLength. Byte arrays S[] and T[], each of length 256 entries are set up.
1. Initialize by setting up array S[i]=i and T[i] = key[i mod kLength].
2. Do an initial permutation.
j=0;
for (i=0; i<256; i++) {
j = (j+S[i]+T[i]) mod 256
swap S[i] and S[j]
}
3. Now, we are ready to generate a stream based on S[]. We start by setting i=j=0 for
integers i and j. Then, we call a function to generate the next key byte (you’ll have to
translate this pseudo-code into proper C):
char generateKeyByte(char s[]) {
static int i=0
static int j=0
i = (i+1) mod 256
j = (j+S[i]) mod 256
Swap S[i] and S[j]
t = (S[i]+S[j]) mod 256
return keyByte = S[t]
}
You should do the following:
1. Assume that a key of length up to 256 bytes has been written into a keyfile that appears as
the first argument (i.e., pointed to by argv[1]) of the program at runtime.
1
2. Write C code for key generation based on this key. To do so, you will first have to read the
keyfile and count the length of the key. Since the key length is limited to 256 bytes (i.e., 256
characters), if the key file contains more than 256 characters, only the first 256 should be
considered; the others should be ignored.
3. Assume that the input file to be processed is placed in a file which appears as the second
argument of the program at runtime. Write C code to do the following:
(a) Read the input file character by character.
(b) For each character read,
• Generate the next element of the key stream.
• Ex-or the key byte with the byte that was just read to form the encrypted output.
• Write the resulting byte (character) into an output file, whose name is specified as
the third argument of the program at runtime.
4. Since this is a symmetric cipher, applying the same encryption procedure to the encrypted
file reverses the process and returns the plaintext. This is a good way to test your code.
Environment: Please remember that unlike Java, C is not always portable. It is not necessary that
a program that works in one environment will work in another. (For something as simple as this
program, this is not a major problem: some minor tweaks are all that are likely to be needed for
portability.) For this reason, any C project must specify the environment as well. The specified C
environment to be used is the gcc compiler in quark.ecs.umass.edu. This is the machine
that your code will be tested on; we will NOT be using any C99 or other flags in the compilation.
Only C should be used; no C++ extensions will be allowed. Your code should be in a file called
encryption.c; it should correctly compile with no warnings, using the following command on
quark:
gcc -o a.out encryption.c -lm .
The object file will then be run by issuing the command ./a.out keyFile inputFile
outputFile where keyFile, inputFile and outputFile are placeholders for the actual key file,
input file, and output file names. Use argv[] to specify these in the program.
Submission must be by means of a shell-script to be run from your quark account; follow
the detailed submission instructions posted elsewhere on the Lab 1 page of this website.
Please note that a program which compiles and runs in some other environment but
not in the quark gcc environment will be given no credit for doing so. So, even if you
develop your code on a different platform, be sure to test and modify it appropriately so
that it runs correctly in the specified environment.
Please be sure to include the full names of ALL group members as comments at the
top of your code.
2