[an error occurred while processing this directive]
CIS 110

LFSR and Encryption in TOY
Programming Assignment


(US Army photo from ARL Technical Library)

Table of Contents

Goals

Submission

Submit encryption.toy and a completed readme_encryption_toy.txt file using the submission link in the sidebar.

Background

Dilbert-19920908.

In the old days of Computer Science, high-level langugages like Java did not exist yet. Programming started with bits. You are probably thinking, 0s and 1s, but did you know that they didn't even have 1s in the beginning? Punch cards were used to write and compile a computer program, in binary! The next higher level from binary is hexadecimal, which is a base-16 number system. TOY is a mock-up machine language with hexadecimal syntax and Assembly language in the comments. Assembly languages are associated with the chip architecture on a computer or phone and used by low-level system programmers. Only after that do you arrive at high-level human readable programming languages like Java. However, computers cannot read high-level languages, and they have to be translated down to machine code.

Practicing to write programs in TOY will help you understand what goes on under the hood of software programs, which usually compile into machine code for execution. The syntax in TOY is very close to machine code that the computer actually reads.

In this assignment, you will be doing conceptually the same thing as the previous homework, however, now in TOY instead of Java.

Before You Begin

Development Environment

Your Program

You will extend on the skeleton program (provided) encryption.toy to: Conceptually, the provided ImageToTOY and TOYToImage serve the same purpose as the ImageData class from Homework 4, and you are writing a TOY program encryption.toy that combines the functionality of LFSR init(), step(), generate(), and PhotoMagic.transform() into one program.

At the end of this page, you will find a list of procedure steps to develop a TOY program, some debugging tips, and a collection of FAQs.

Warning Debugging machine language is extremely tricky, no matter how much experience you have. It is imperative to be able to carefully proofread your code numerous times, which means working in shorter stints over a period of days. So start early, and don't wait to come to TA office hours!

Part I: Reading in the Header

Programming and debugging in TOY is even more finicky than Java, so it is essential to write and test your code in very small steps. The first step is to understand the input file format, read in everything but the actual data, and print it back out. This part is the equivalent of LFSR.init().

Required Files

Input Format

The TOY simulator represents data in text format. Each TOY word is 4 hex digits, with white space separating successive words. Your program should read standard input in the following format (you do not need to do any error checking):

Output Format

Your program should output data in exactly the same format as the input. Print out the number of bits, seed, tap position, and following two words unchanged. In later steps you will print out all the encrypted data and the final FFFF. So far, your program's output should stop before the encrypted data.

Recall that you access StdIn by loading from mem[FF] and you access StdOut by storing to mem[FF].

For example, the input file zeros.in is a valid input to encryption.toy specifying the 11-bit (000B in hex) seed 01101000010 (0342 in hex), tap bit 8, two arbitrary words (we picked DEAD and BEEF in hex for this file, see this page for an explanation), and a series of 8 0-bits.:

000B
0342
0008
DEAD
BEEF
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
FFFF
Your program should ultimately produce a file zeros.out with the following contents. For now, you should only be printing the first six words.
000B
0342
0008
DEAD
BEEF
0001
0001
0000
0000
0001
0000
0000
0001
0000
0000
0001
0001
0001
0001
0000
0001
FFFF
Using the command (in the Terminal/Command Prompt, not in DrJava):
% java TOY encryption.toy < zeros.in > zeros.out

Observe that the output of encryption.toy is itself valid input to encryption.toy. This means that you can run the encrypted ouput back through encryption.toy to decrypt it. You can feed the output of one program into another program as input using a pipe in the terminal. Like input and output redirection, pipes do not work in DrJava:

% java TOY encryption.toy < zeros.in | java TOY encryption.toy > zeros.out2
uses the file zeros.in as the standard input to encryption.toy, and feeds the output into another run of encryption.toy. The output of the second run is saved to the file zeros.out2, which should be indentical to zeros.in if your program works correctly. The pipe character | is the same vertical bar that Java uses for the or operator (||).

Part II: Reading and Encrypting One Bit

Once you are successfully reading in and printout out the header (number of bits in seed, seed, tap, and two unencrypted words), read in and encrypt a single bit:

Part III: Encrypting All Bits

The final step of your TOY program is reading in and encrypting all bits. This is roughly equivalent to PhotoMagic.transform().

Part IV: Encrypting Images

Required Files

If you're lucky, you will not need to write or modify any code at all for this part. Your existing TOY program should "just work" for images. You will, however, be testing your program much more intensively, and may discover bugs along the way.

Unlike Java, TOY does not come with a facility for reading in Images. You could write a program to do so, but it would be unpleasant. Instead, we provide the Java programs ImageToTOY and TOYToImage to convert image files to input for encryption.toy and convert the output back into an image. The two TOY words that are passed from input to output without modification are used to store the image width and height.

Using pipe operators and the supplied ImageToTOY and TOYToImage programs, you will be able to encrypt and decrypt images. (In the end, you should be able to encrypt images using PhotoMagic, then decrypt them with you TOY program, and vice versa.)

For example,

% java ImageToTOY white-10x10.png 01101000010 8 > white.in
creates a valid input file to encryption.toy from the 10x10 white square white-10x10.png and specifies the seed 01101000010 with tap bit 8.
% java TOYToImage < white.in
reads the contents of white.in from standard input, converts it back to an image, and displays it.

You can chain these together with the actual encryption using pipes:

% java ImageToTOY white-10x10.png 01101000010 8 | java TOY encryption.toy | java TOYToImage
This converts the image white-10x10.png along with the seed and tap into an appropriate input file, runs it through encryption.toy, and converts the output into an encrypted image. Although this works with any image, keep in mind that it will be very slow normal-size images. It is best to test with very small images like white-10x10.png and artifical test files like zeros.in.

Similarly, you should be able to produce the pipe image with the command:

% java ImageToTOY TOYXpipe.png 01101000010 8 | java TOY encryption.toy | java TOYToImage

Procedure for Developing a TOY Program

To simulate the execution of your program on a TOY machine, use either the bare-bones TOY.java or the full-blown Visual X-TOY simulator. To get started, download the sample TOY program multiply.toy.

Visual X-TOY simulator. The Visual X-TOY simulator from lecture provides lots of useful development features. There are three modes (edit, debug, and machine). Download it from the link on the assignment page.

Using Debug Mode in X-TOY Unlike in Java, it is not simple to print out the values of variables in TOY to see what is going on. Instead you will need to use the debug mode in X-TOY. You can access this from the Mode menu, or by clicking the computer icon on the toolbar. (Switch back to edit mode to modify your code.) In debug mode you can step through your program one instruction at a time, and inspect the contents of every register and memory location as you go. You can load a file to use as StdIn on the StdIn pane. Stepping through your program and methodically scrutinizing these values is how you will discover errors in your instructions. Keep in mind the "Sim Mode" is much cooler than debug mode but is much less useful.

Example Program X-TOY contains many sample programs that you can access through "Open Example Program" in the File menu. You may wish to look at "Fast Multiply" and "Multiply with stdin/stdout" in particular since these use many of the same instructions that you will need for encryption.toy.

Command-line TOY simulator. Download the TOY simulator using the link on the assignment page.

Debugging

Here are some debugging hints that may help you out.

Frequently Asked Questions

I can't launch the Java Web Start version of the Visual X-TOY simulator on my laptop. Any ideas? The program requires something called Web Start. When you installed Java initially, this was included. However, Windows security patches have been know to break it. If this happens to you, go to java.com and click the Get It Now button.

I can't launch the Java Web Start version of the Visual X-TOY simulator in the Moore 100 labs. Any ideas? The link in this assignment will not work on the Linux machines in the Moore 100 labs. Instead, select the Terminal from the start menu, and type "xtoy". The link in the assignment does work on the Windows machines, but there is also an icon for X-TOY in the Start Menu under the CIS 110 section of the Course Specific Software menu.

How do I format the TOY output? You can't. Just print one integer per line (0000 or 0001).

My program prints out the right answer, but then I get java.lang.NumberFormatException: null. What could be wrong? You are probably trying to read from standard input, even though it is empty. Be sure that your TOY program terminates when it encounters FFFF.

How do I write/comment a .toy file? To initialize a location in memory to have a value, write (on its own line) the index of the memory location (two hex digits), followed by a colon, followed by a space, followed by the value (four hex digits). Anything else on the line is ignored by the TOY simulator and treated as a comment. Any line that does not follow this format is also interpreted as a comment, but you should begin comment lines with "//" as a matter of good style.

TOY.java doesn't read in some of my instructions. Any ideas? Be sure that you follow the required format XX: XXXX. Check for duplicated line numbers and using the letter O instead of the number 0.

How much do I need to comment my TOY code? At a minimum, you should:


This assignment was created by Benedict Brown.
Copyright © 2012.