Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COS 126 Programming Assignment: LFSR COS 126 Linear Feedback Shift Register Programming Assignment Write a program that produces pseudo-random bits by simulating a linear feedback shift register, and then use it to implement a simple form of encryption for digital pictures. LFSR review.  A linear feedback shift register (LFSR) is a register of bits that performs discrete step operations that shifts the bits one position to the left and replaces the vacated bit by the exclusive or of the bit shifted off and the bit previously at a given tap position in the register. A LFSR has three parameters that characterize the sequence of bits it produces: the number of bits N, the initial seed (the sequence of bits that initializes the register), and the the tap position tap. As in the example in Lecture 0, the following illustrates one step of an 11-bit LFSR with initial seed 01101000010 and tap position 8. LFSR data type.  Your first task is to write a data type that simulates the operation of a LFSR by implementing the following API: public class LFSR { public LFSR(String seed, int tap) // creates an LFSR with the specified seed and tap public int step() // simulates one step and return the new bit (as 0 or 1) public int generate(int k) // simulates k steps and return the k bits as a k-bit integer public String toString() // returns a string representation of this LFSR public static void main(String[] args) // unit tests this class } To do so, you need to choose the internal representation (instance variables), implement the constructor, and implement the three instance methods. These are interrelated activities and there are several viable approaches. Constructor. The constructor takes the initial seed as a String argument comprised of a sequence of the characters '0' and '1'. The length of the register is the length of the seed. We will generate each new bit by xoring the leftmost bit and the tap bit. The index of the tap bit is specified as the second constructor argument. For example, the following code creates the LFSR described above. LFSR lfsr = new LFSR("01101000010", 8); String representation. The toString() method returns a string representation of the LFSR by concatenating the values in the registers. For example, LFSR lfsr = new LFSR("01101000010", 8); StdOut.println(lfsr); outputs 01101000010 Simulate one step. The step() method simulates one step of the LFSR and returns the rightmost bit as an integer (0 or 1). For example, LFSR lfsr = new LFSR("01101000010", 8); StdOut.println(lfsr); for (int i = 0; i < 10; i++) { int bit = lfsr.step(); StdOut.println(lfsr + " " + bit); } outputs 01101000010 11010000101 1 10100001011 1 01000010110 0 10000101100 0 00001011001 1 00010110010 0 00101100100 0 01011001001 1 10110010010 0 01100100100 0 Extracting multiple bits.  The method generate() takes a positive integer k as an argument and returns a k-bit integer obtained by simulating k steps of the LFSR. This task is easy to accomplish with a little arithmetic: initialize a variable to zero and, for each bit extracted, double the variable and add the bit returned by step(). For example, extracting (from left to right) each bit from the sequence 1 1 0 0 1, the variable takes on the values 1, 3, 6, 12, and 25, ending with the binary representation of the bit sequence. For example, LFSR lfsr = new LFSR("01101000010", 8); StdOut.println(lfsr); for (int i = 0; i < 10; i++) { int r = lfsr.generate(5); StdOut.println(lfsr + " " + r); } outputs 01101000010 00001011001 25 01100100100 4 10010011110 30 01111011011 27 01101110010 18 11001011010 26 01101011100 28 01110011000 24 01100010111 23 01011111101 29 Implement the generate() method by calling the step() method k times and performing the necessary arithmetic. Corner cases. You may assume that the arguments to the constructor and instance methods are valid. For example, you may assume that constructor argument seed is a string comprised of only the characters '0' and '1' and that tap is an integer between 0 and seed.length() - 1. You may also assume that the argument k to generate() is a positive integer. Testing. Your main() must contain code that tests all of the methods. To make development easiest, each time you write a new method or chunk of code, add a test to main() to show that the new code works properly and you didn't break any existing functionality. You should experiment with developing your own test cases; in addition, you can use the tests that we give you on this page and the checklist. A client to encrypt and decrypt images.  Your final task is write a LFSR client PhotoMagic.java that can encrypt and decrypt pictures, by implementing the following API: public class PhotoMagic { // Returns a transformed copy of the specified picture, using the specified LFSR. public static Picture transform(Picture picture, LFSR lfsr) // Takes the name of an image file and // a description of an LFSR as command-line arguments; // displays a copy of the image that is "encrypted" using the LFSR. public static void main(String[] args) } Here are a few more details about the API. Manipulating pictures.  The Picture data type is part of stdlib.jar; the Color data type is a Java library. The transform method.  The transform() method takes a Picture and an LFSR as arguments and returns a new Picture object that is the result of transforming the argument picture using the LFSR as follows: For each pixel (col, row), in column-major order—(0, 0), (0, 1), (0, 2), ... —extract the red, green, and blue components of the color (each component is an integer between 0 and 255). Then, xor the red component with a newly-generated 8-bit integer. Do the same for the green (using another new 8-bit integer) and, finally, the blue. Create a new Color object using the result of the xor operations, and set the pixel in the new picture to that color. Main method. The main() method takes three command-line arguments: the name of an image file, a binary password (the initial LFSR seed), and an integer (the tap number). It must display the transformed picture on the screen, using the show() method in the Picture data type. For example, the command % java-introcs PhotoMagic pipe.png 01101000010100010000 16 takes as input the picture pipe.png (left) and displays as output the transformed picture Xpipe.png (right).    Now, here's the magic: running the same program with the same binary password and tap on the transformed picture recovers the original picture! For example, typing % java-introcs PhotoMagic Xpipe.png 01101000010100010000 16 takes as input the tranformed picture Xpipe.png (left) and displays as output the original picture pipe.png (right).    Anyone knowing this password and tap can recover the original picture, but another password won't work. If you're not convinced, try it. Thus, for example, you can post a transformed picture on the web, but only friends who have the password (and your program) can see the original. Corner cases. You may assume that the arguments to transform() are not null. Files for this assignment. The file lfsr.zip contains a number of sample PNG files, including the ones used above; an optional template for getting started with LFSR.java; and this week's readme.txt template. Style.   When implementing a class, include a comment next to each instance variable indicating its purpose, and one above each instance method documenting what it does. Submission.   Submit LFSR.java, PhotoMagic.java, and a completed readme.txt file. Challenge for the bored 1.  Using a short binary password is weak protection and using a long one is inconvenient. Write a client with the same API as PhotoMagic.java, but use a short alphanumeric password instead of a long binary one. For example, if your password works over a 64-character alphabet String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; then a length n alphanumeric will provide as much security as a length-6n binary one. Challenge for the bored 2.  Write a program that takes the filename of an encrypted picture and the number of bits N in the password as command-line arguments, tries all possible binary passwords of length N and all possible taps, and decrypts the picture. All but the correct seed and tap will produce pseudo-random colors, so you can track the frequencies of each 8-bit value and pick the seed and tap that results in the frequencies that deviate the most from 128. This assignment was created by Robert Sedgewick. Copyright © 2008.