Submission instructions

Create a directory called hw1 containing the following: Create an archive by running the following command from the directory containing hw1:

        tar czvf hw1.tar.gz hw1

Then submit the archive using the submit script:

        submit 2008 spring cmsc 414 0101 1 hw1.tar.gz

You may find it useful to create an alias to the the submit script. Edit file ~/.aliases and add to it:

        alias submit "submit 2008 spring cmsc 414 0101"

This way, when you submit homework 1 you can just type:

        submit 1 hw1.tar.gz

For usage instructions of the submit script run:

        submit


General

1. I need a partner to work with on homework 1.
Email martin@cs. Put partner-414 in the subject line. First pair of people will be matched, next pair of people will be matched and so on. If you email me and I have not gotten back to you this means that you are the only person in the queue for the time being.
2. Will the homework be tested automatically?
The programming part of the homework will be tested automatically.
3. The HW1 FAQ asks that I submit the answers to the written questions in a text file. Can I submit my answers in a PDF file?
Yes you can. Please submit in the most convenient format for you. Name the file hw1.xxx where xxx is the extension of the format used.
4. The command tar czvf hw1.tar.gz hw1 does not work on grace
Run gtar czvf hw1.tar.gz hw1 instead

Question 1

1. How is OTPgen.java getting its argument?
On the command line.
2. What error should OTPenc.java return if the plaintext and key lengths do not match?
Print (quotes for clarity) "Plaintext and key lengths do not match.\n" in OTPctext.txt. Do not throw an exception.
3. What error should OTPdec.java return if the ciphertext and key lengths do not match?
Print (quotes for clarity) "Ciphertext and key lengths do not match.\n" in OTPdecrypt.txt. Do not throw an exception.
4. Is n the length of key/plaintext/ciphertex in bytes or in bits?
n is the length in bits. Since it will always be a multiple of 8, as stated by the question, the number of bytes can be obtained by n/8 .
5. Can you provide some sample input/output data?
Running:

        javac OTPgen.java
        java OTPgen 264

should result in OTPkey.txt looking like this.
Running next:

        javac OTPenc.java
        java OTPenc

with OTPmsg.txt in the current directory like this and OTPkey.txt as generated above, should generate output OTPctext.txt like this. Finally, running:

        javac OTPdec.java
        java OTPdec

with OTPctext.txt and OTPkey.txt in the current directory generated as above should produce OTPdecrypt.txt in the current dirctory that looks like this.

If the key above was generated with the command:

        java OTPgen 256

encryption and decryption should produce OTPctext.txt and OTPdecrypt.txt that look like this and this, respectively.

6. How do I tell java to view characters as bit-strings?
You can obtain the 8-bit ASCII code of a char by casting the char to a byte. For example:

        char ch = 'a';
        byte b = (byte)ch;

7. Do all error outputs really need to be to a file?
You should check for the erros mentioned in this FAQ and should print the corresponding messages to the corresponding files.
8. The way we coded up the reading in of the OTPmsg.txt file removes newlines. Do you want these included?
You should encrypt every character/byte in the file OTPmsg.txt no matter whether it is a whitespace or not.

Question 2

1. After I read the DES/AES key from key.txt, how do I pass it to the init() method of the Cipher class in order to initialize the Cipher object?
If you read the key into an array of bytes (byte[]), let's call it key, create an object of class javax.crypto.spec.SecretKeySpec. This class implements the SecretKey interface which is the type of the argument Cipher's init() expects. Don't forget to consult the Java API documentation and the JCA reference guide:

        http://java.sun.com/javase/6/docs/api/
        http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

2. The encryption schemes for CBC and CTR modes require an Initialization Vector (IV). How should we produce and/or store the IV?
You are supposed to figure this out for yourself.
3. What error message should we output to ctext.txt if the length of the message in msg.txt is not a multiple of the block length of the cipher being used?
Please output "Message needs to be padded.\n" to ctext.txt.
4. With how many bits is a character in msg.txt represented?
8 bits, its ASCII code.
5. The question does not specify what Decrypt.java should do with the recovered plaintext?
Please output the recovered message in decrypt.txt
6. Does question 2b ask for the frequency of 0 bits or the frequency of 0 in the hex representation?
It asks for the frequency of 0 bits.