LFSR - WordPress.com

BHSEC Queens
Object-Oriented Programming
Spring 2015
Project #2: Linear Feedback Shift Register
Introduction1
A register is a small amount of storage on a digital processor. In this project, we will
think of a register as a string of N ones and zeros stored in order.
A linear feedback shift register (LFSR) is a special type of register used primarily for
generating pseudo-random numbers. In this project, we will use the sequence of pseudorandom numbers generated from an LFSR to encrypt and decrypt an image.
→
encrypt
→
decrypt
Linear Feedback Shift Register Technical Details
Below is an example of an 11-bit LFSR. To go from one pseudo-random number to the
next, the LFSR performs a step, which consists of the following two operations:
1. Shift all bits one position to the left.
2. The new rightmost bit is the exclusive or of the old leftmost bit and a bit at a given
position, called the tap.
Submission
1 Project
adapted from www.princeton.edu/~cos126
1
On Dropbox, create a folder called LFSR. You will submit a total of three java files that
should be named XOR.java, LFSR.java, PhotoMagic.java.
Grading
To get a 60: Successfully implement and test XOR.java.
To get a 70: Define the LFSR class. Write the constructor and toString methods, and
test each one.
To get an 80: Correctly implement and test the step() method in the LFSR class.
To get a 90: Correctly implement and test the generate() method in the LFSR class.
To get a 100: Encrypt and decrypt images by correctly implementing the PhotoMagic
class.
Possible 10 point deduction: You must include a block comment at the start of each Java
file containing your name, date of last modification, and a sentence affirming that you did
not receive or share your code from anyone else, including via email. Amongst your three
Java files, you also must include a total of at least 25 inline comments, each of which gives
a brief, English description of one or more lines of code that is not obvious.
Academic Honesty Policy
It is academically dishonest to look at code written by anyone else. You are allowed to talk
to other students in class and share ideas. You may ask them if they have experienced particular compiler errors or how they approached a portion of the project. However, you are not
allowed to look at code, accept it, or send it to anyone else. The most common incidents of
academic dishonesty in computer science involve the electronic sending/sharing of code with
other students. Incidents of academic dishonest will be reported to the principal of the school.
2
APIs
XOR: This class is composed of static methods (i.e. methods that do calculation); therefore, it should not contain any instance variables. A starter XOR.java class has been provided
for you.
Method
Name
Input Type
Return
Type
Comments
xor
boolean,
boolean
boolean
Implements the exclusive OR function on
two bits of data.
String
Assumes that the two input strings are
composed only of 0s and 1s. Outputs the
bitwise exclusive OR of the two inputs.
Your function should work even when the
input strings are of different lengths.
int
Outputs the bitwise exclusive OR of two
base 10 integers. The integers should
be converted to binary and the final result should be converted back to base 10.
Make use of the decimalToBinary and
binaryToDecimal methods that have already been written.
String
Already coded for you. This takes an integer in base 10 and converts it to binary.
The input is an integer, and the output is
a String of 1s and 0s.
int
Already coded for you. This takes a binary number and converts it to an integer
in base 10. The input is a string of 1s and
0s, and the output is an integer.
String[]
This code has been started for you, and
you must finish it. It should test each
static method defined in the class. You
are expected to comment the main method
with expected outputs as is demonstrated
in the starter code.
xor
xor
decToBinary
binaryToDec
main
String, String
int, int
int
String
none
3
LFSR: This class is a template for an LFSR object. Your instance variables should all be
private, and your methods should all be public. Re-read the LFSR technical details to help
you decide what instance variables to use.
Method
Name
Input Type
Return
Type
Comments
Constructor
String, int
none
The constructor takes the initial seed as
a String of 0s and 1s. The length of the
register should be the same as the length
of the initial seed. The constructor also
takes the the position of the tap bit.
toString
none
String
Returns a string representation of the
LFSR, which is simply the sequence of bits
stored in the register.
int
Simulates one step (i.e. one shift) of the
LFSR. The method should modify the contents of the register and return the new bit
that was shifted in.
step
none
generate
int
int
Simulates k steps of the LFSR where k
is the input parameter. Returns the nonnegative integer (in base 10) that is represented by the 0s and 1s that have been
shifted into the register in those k steps.
main
none
String[]
Instantiates at least one LFSR object and
tests all methods of the class.
Here are some additional hints:
• Instantiating and printing an LFSR object
LFSR lfsr = new LFSR("01101000010", 2);
System.out.println(lfsr);
This should display:
01101000010
• The following code will help you determine if your step method is working.
LFSR lfsr = new LFSR("01101000010", 2);
for (int i = 0; i < 10; i++) {
int bit = lfsr.step();
System.out.println(lfsr + " " + bit);
}
This should display
4
11010000101
10100001011
01000010110
10000101100
00001011001
00010110010
00101100100
01011001001
10110010010
01100100100
1
1
0
0
1
0
0
1
0
0
• The generate method should use a String to keep track of what bits have been shifted
into the LFSR. It should take advantage of the binaryToDec static method in the
XOR class. To call a static method located in a different class, you write <class
name>.<method name>(<parameters>).
The following code will help you determine if your generate method is working.
LFSR lfsr = new LFSR("01101000010", 2);
for (int i = 0; i < 10; i++) {
int r = lfsr.generate(5);
System.out.println(lfsr + " " + r);
}
It should output
00001011001
01100100100
10010011110
01111011011
01101110010
11001011010
01101011100
01110011000
01100010111
01011111101
25
4
30
27
18
26
28
24
23
29
5
PhotoMagic: This class only contains static methods. It will use your LFSR and XOR
classes to encrypt or decrypt a Picture.
Method
Name
transform
main
Input Type
Picture,
LFSR
none
Return
Type
Comments
Picture
Returns a new picture that is the result
of transforming the input picture using
the input LFSR. How to use the LFSR to
transform the picture is described below.
String[]
Reads in three command line arguments
(input image name, initial seed, and the
tap number for the LFSR). Displays the resulting encrypted image. You should also
consider saving this encrypted image to
make sure that your code can successfully
decrypt it.
Here are a few additional details:
• Transforming the image. First of all, make sure to create a new Picture object. Do not
modify (i.e. overweight) the original image. Second, iterate through each pixel in the
original image. For each pixel, do the following:
1. Extract the red component of the original image. Generate an 8-bit number from
your LFSR. XOR the red component with the number from the LFSR. This is the
new red value in the encrypted image.
2. Extract the green component of the original image. Generate an 8-bit number from
your LFSR. XOR the green component with the number from the LFSR. This is
the new green value in the encrypted image.
3. Extract the blue component of the original image. Generate an 8-bit number from
your LFSR. XOR the blue component with the number from the LFSR. This is the
new blue value in the encrypted image.
• The final product. Note that the second and third arguments can effectively thought of
as a way to password protect your image.
java PhotoMagic pipe.png 01101000010100010000 3
Input
Should Display
Save the output image as Xpipe.png.
6
java PhotoMagic Xpipe.png 01101000010100010000 3
Input
Should Display
7