ASSIGNMENT 5 TIPS AND TRICKS

Similar documents
CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Fundamentals of Programming (C)

Chapter 8. Characters and Strings

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Fundamentals of Programming

Chapter 2 Bits, Data Types, and Operations

Chapter 2 Bits, Data Types, and Operations

Data Representation and Binary Arithmetic. Lecture 2

Chapter 3. Information Representation

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Number Representations

Chapter 2 Bits, Data Types, and Operations

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Do not start the test until instructed to do so!

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Chapter 2 Number System

Oberon Data Types. Matteo Corti. December 5, 2001

Do not start the test until instructed to do so!

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Do not start the test until instructed to do so!

Lecture (09) x86 programming 8

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Chapter 2 Bits, Data Types, and Operations

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Project #2: Linear Feedback Shift Register

The Binary Number System

Chapter 2 Bits, Data Types, and Operations

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Number System (Different Ways To Say How Many) Fall 2016

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Positional Number System

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Simple Data Types in C. Alan L. Cox

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Number Systems Base r

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

3.1. Unit 3. Binary Representation

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

User s Manual. Xi3000 Scanner. Table of Contents

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Variables and data types

Fundamental Data Types

COS 226 Algorithms and Data Structures Fall Final

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Final Exam Practice Questions

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

Xi2000-BT Series Configuration Guide

Configuration Manual PULSAR C CCD SCANNER. Table of Contents

CSE 30 Fall 2013 Final Exam

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Source coding and compression

S-Series Sensor ASCII Protocol v8.1.0

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

CSE 30 Spring 2006 Final Exam

CS 112 Introduction to Programming

Chemistry Hour Exam 2

FD-011WU. 2D Barcode Reader User Guide V1.6CC

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

EE 109 Unit 2. Binary Representation Systems

Hardware. ( Not so hard really )

Universal Asynchronous Receiver Transmitter Communication

The following are the data types used in the C programming language:

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

CSE 30 Winter 2014 Final Exam

CSE 30 Winter 2009 Final Exam

Connecting UniOP to Datalogic Barcode Readers

CSE 30 Fall 2007 Final Exam

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

CSE 30 Fall 2012 Final Exam

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

Introduction. Chapter 1. Hardware. Introduction. Creators of Software. Hardware. Introduction to Computers and Programming (Fall 2015, CSUS)

RS-232 Digital Relay I/O

^BC Code 128 Bar Code (Subsets A, B, and C)

marson MT8200S 2D Handheld Scanner User Manual V / 6 / 25 - I -

Characters Lesson Outline

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Final CSE 131 Fall 2014

Question Points Score Total: 100

8.2 User Defined Protocol Communication

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

SECURITY PROTECTION OF SOFTWARE PROGRAMS BY INFORMATION SHARING AND AUTHENTICATION TECHNIQUES USING INVISIBLE ASCII CONTROL CODES*

Transcription:

ASSIGNMENT 5 TIPS AND TRICKS linear-feedback shift registers Java implementation a simple encryption scheme http://princeton.edu/~cos26 Last updated on /26/7 : PM

Goals OOP: implement a data type; write a client program to use it. LFSR: learn about a simple machine and encryption scheme. 2

ASSIGNMENT 5 TIPS AND TRICKS linear-feedback shift registers Java implementation a simple encryption scheme

Linear-feedback shift register Bit: or. Cell: storage element that holds one bit. Register: sequence of cells. Seed: initial sequence of bits. Feedback: Compute xor of two bits and put result at right. Tap: bit positions used for xor (one is always leftmost bit). Shift register: when clock ticks, bits propagate one position to left. an -bit LFSR ^ 9 8 7 6 5 4 3 2 4

Linear-feedback shift register simulation 5 ^ step ^ ^ 2 ^ 3 ^ 4 5 history of register contents a pseudo-random bit sequence!

LFSR quiz Which are the next two bits that the LFSR outputs? A. B. C. ^ D. ^ 6

ASSIGNMENT 5 TIPS AND TRICKS linear-feedback shift registers Java implementation a simple encryption scheme

Applications programming interface API. Specifies the set of operations. public class public LFSR LFSR(String seed, int tap) creates an LFSR with specified seed and tap public int length() returns the length of the LFSR public int bitat(int i) returns bit i as or public String tostring() returns a string representation of this LFSR public int step() simulates one step; return next bit as or public int generate(int k) simulates k steps; return next k bits as k-bit integer public static void main(string[] args) tests every method in this class 8

LFSR.java template public class LFSR { // Define instance variables here. // Creates an LFSR with the specified seed and tap. public LFSR(String seed, int tap) // Returns the length of the LFSR. public int length() // Returns bit i of this LFSR as or. public int bitat(int i) // Returns a string representation of this LFSR. public String tostring() // Simulates one step of this LFSR; returns next bit as or. public int step() // Simulates k steps of this LFSR; returns next k bits as a k-bit integer. public int generate(int k) } // Tests every method in this class. public static void main(string[] args) 9

Testing Develop program incrementally, one method at a time.. Define instance variables. 2. Implement construtor. 3. Implement length(). 4. Implement bitat(). 5. Implement tostring(). 6. Implement step(). 7. Implement generate(). Tip. Testing iteratively is the key to success.

LFSR representation Q. How to represent the LFSR? A. Several viable approaches. 9 8 7 6 5 4 3 2 Approach. Integer array of length n in forward order: reg[i] = bit (i + ). Approach 2. Integer array of length n+ in forward order: reg[i] = bit i. Approach 3. Integer array of length n in reverse order: reg[i] = bit n i. Approach 4. Boolean array of length n or n+, in forward or reverse order. Approach 5. String of length n: reg.charat(i) = character corresponding to bit n i. Key point. The client doesn t know (or care) how you represent the data type. LFSR indices go from right to left, starting at

Applications programming interface API. Specifies the set of operations. public class public LFSR LFSR(String seed, int tap) creates an LFSR with specified seed and tap public int length() returns the length of the LFSR public int bitat(int i) returns bit i as or public String tostring() returns a string representation of this LFSR public int step() simulates one step; return next bit as or public int generate(int k) simulates k steps; return next k bits as k-bit integer public static void main(string[] args) tests every method in this class 2

String representation Java s tostring() method. Every Java class has a tostring() method; by default, it returns the memory address of the object. Defining a custom tostring() method overrides the default one. Java calls the tostring() method automatically with string concatenation and StdOut.println(). LFSR lfsr = new LFSR("", 9); StdOut.println(lfsr.toString()); // print string representation StdOut.println(lfsr); // better style Best practice. Override the tostring() method to facilitate debugging. Tip. If you use an array representation, concatenate array elements (in proper order). 3

Applications programming interface API. Specifies the set of operations. public class public LFSR LFSR(String seed, int tap) creates an LFSR with specified seed and tap public int length() returns the length of the LFSR public int bitat(int i) returns bit i as or public String tostring() returns a string representation of this LFSR public int step() simulates one step; return next bit as or public int generate(int k) simulates k steps; return next k bits as k-bit integer public static void main(string[] args) tests every method in this class 4

Generating a k-bit integer Q. Suppose next 5 bits are. How to convert into a 5-bit integer? A. Binary-to-decimal conversion: 2 4 + 2 3 + 2 2 + 2 + 2 = 25. A2. Horner s method: 2 ( 2 ( 2 (2 ( ) + ) + ) + ) + = 25. Horner s method. Initialize a variable sum to. For each bit, from left to right double sum and add bit sum bits 3 6 2 25 Tip. To implement generate(k), don t simulate LFSR from scratch; instead, make k calls to step(). 5

Applications programming interface API. Specifies the set of operations. public class public LFSR LFSR(String seed, int tap) creates an LFSR with specified seed and tap public int length() returns the length of the LFSR public int bitat(int i) returns bit i as or public String tostring() returns a string representation of this LFSR public int step() simulates one step; return next bit as or public int generate(int k) simulates k steps; return next k bits as k-bit integer public static void main(string[] args) tests every method in this class 6

Java characters A char in Java is a 6-bit unsigned integer. Unicode. Characters are encoded using Unicode. 'A' is 65. '' is 48. '' is 49. 'á' is 225. ' ' is 9775. Best practices. Don t write code that depends on internal representation. Good: if (c == ''). Bad: if (c == 48). 2 3 4 5 6 7 8 9 A B C D E F NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 2 SP! # $ % & ( ) * +, -. / 3 2 3 4 5 6 7 8 9 : ; < = >? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h i j k l m n o 7 p q r s t u v w x y z { } ~ DEL Hexadecimal to ASCII conversion table 7

ASSIGNMENT 5 TIPS AND TRICKS linear-feedback shift registers Java implementation a simple encryption scheme

A simple encryption scheme For each pixel in column-major order: Read (red, green, blue) values of pixel. Get 8 bits from LFSR and bitwise xor those with red. Get 8 bits from LFSR and bitwise xor those with green. Get 8 bits from LFSR and bitwise xor those with blue. Write (red, green, blue) values of resulting pixel. pixel (col, row) column-major order original picture transformed picture 9

A simple decryption scheme For each pixel in column-major order: Read (red, green, blue) values of pixel. Get 8 bits from LFSR and bitwise xor those with red. Get 8 bits from LFSR and bitwise xor those with green. Get 8 bits from LFSR and bitwise xor those with blue. Write (red, green, blue) values of resulting pixel. pixel (col, row) column-major order transformed picture transformed transformed picture 2

Why does it work? Key identity. (x ^ y) ^ y = x ^ (y ^ y) = x ^ = x. Important requirements. Must use the same initial LFSR to encrypt and decrypt. Must traverse the pixels in the same order. transform transform 2

Photomagic.java template Tip. See ColorSeparation.java from precept. must create and return a copy of picture, (do not mutate argument picture) } 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) % java-introcs PhotoMagic pipe.png 6 name of image description of LFSR (seed and tap) 22