// APS101, Winter 2009: Assignment 2 (Spy class) // Examples of using some of the methods and their expected outputs > Spy.getAllCodeNumbers() "" > Spy.removeCodeNumber(1234) false > Spy spy1 = new Spy(1234) > spy1.getCodeNumber() 1234 > Spy.getAllCodeNumbers() "1234" > Spy spy2 = new Spy(9999) > Spy.getAllCodeNumbers() "9999:1234" > Spy spy3 = new Spy(1235) > Spy.getAllCodeNumbers() "9999:1235:1234" > Spy.removeCodeNumber(9999) true > Spy.getAllCodeNumbers() "1235:1234" > Spy.removeCodeNumber(9999) false > Spy spy4 = new Spy(1234) This code number already exists in the record! > Spy.charToBinary('a') "01100001" > Spy.charToBinary('~') "01111110" > Spy.charToBinary(a) Static Error: Undefined name 'a' > Spy.charToBinary(97) Static Error: No method in Spy with name 'charToBinary' accepts arguments (int) // these are the ONLY 4 inputs & outputs for exclusiveOr > Spy.exclusiveOr(1, 1) 0 > Spy.exclusiveOr(0, 1) 1 > Spy.exclusiveOr(1, 0) 1 > Spy.exclusiveOr(0, 0) 0 > Spy.stretch("1001", 3) "1001" > Spy.stretch("1001", 4) "1001" > Spy.stretch("1001", 5) "100111" > Spy.stretch("1001", 8) "100111011" // what's the problem here? (an ODD number of bits was produced) // so, if you wanted to do: Spy.stretch("1001", 10), this would be a problem, // because the method would try to split a 9-bit String into two EVEN halves. // This is a flaw of the method, but it's OK, because for our purposes this // sort of situation won't happen. Why? Because in the encrypt method, you'll // be stretching 128-bit keys. Let's see how that happens... // 1st iteration: 128 / 2 = 64. Then 128 + 64 = 192 <- even number of bits! // 2nd iteration: 192 / 2 = 96. Then 192 + 96 = 288 <- even number of bits! // 3rd iteration: 288 / 2 = 144. Then 288 + 144 = 432 <- even number of bits! // 4th iteration: 432 / 2 = 216. Then 432 + 216 = 648 <- even number of bits! // 5th iteration: 648 / 2 = 324. Then 648 + 324 = 972 <- even number of bits! // 6th iteration: 972 / 2 = 486. Then 972 + 486 = 1458 <- even number of bits! // 7th iteration: 1458 / 2 = 729. Then 1458 + 729 = 2187. <- odd number of bits! (finally) // What this means for you is the following: // Since each character is represented by an 8-bit String, you must only test method encrypt // on messages with 273 characters or less (because 8 * 273 would give us a 2184-bit String, which is OK). // If the message is 274 characters or more (i.e., 8 * 274 = 2192 bits long), then we'd have to run // method stretch one more time, for an 8th iteration, which would give us problems. // In other words... > String msg = "This message has 273 characters. This is the maximum length of the messages you're allowed to test. If you test your encrypt method on longer messages, there will be a problem with the stretch method. I ran out of things to say so I'm just writing useless things.... :)))))"; > msg.length() 273 > Spy.encrypt("", 1234) "" > Spy.encrypt("lol.", 1234) "01101000101111010110100011111100" > Spy.encrypt("lol.", spy1.getCodeNumber()) "01101000101111010110100011111100" > String msg = "This is a secret message!"; > String encrypted = Spy.encrypt(msg, spy1.getCodeNumber()) > encrypted "01010000101110100110110110100001001001001011101101110111111100100110010111110010011101111011011101100111101000000110000110100110001000000110110101100101011100110111001101100001011001110110010100100001" > encrypted.length() == 8 * msg.length() true > String decrypted = Spy.decrypt(encrypted, 1234) > decrypted "This is a secret message!" > Spy spy2 = new Spy(1234) This code number already exists in the record! > spy2.getCodeNumber() 1234 > spy2.decrypt(encrypted, spy2.getCodeNumber()) "This is a secret message!" > spy2.decrypt(encrypted, 1235) "Tiir hs!a!sdcseu message!" > spy2.decrypt(encrypted, 1230) "Ttio us