CS 101 - Introduction to Computing, Spring 2007

Spring 2007

Homework 3 - Encrypting and Decrypting a Sound File

Due: Monday, April 16, 2007 at 11:59 pm

Encrypting is just a fancy term for changing the format of data so other people can't read the data easily( in the best cases, they can't read it at all). So then decrypting is just restoring the changed data back to it original format.

X -->
Encrypting
Program
--> X' -->
Decrypting
Program
--> X

Where X is the original data and X' is the changed (or encrypted) data.

Depending on how good the algorithm used by the Encrypting/Decrypting program pair, to more (or less) secure the encrypted data is. We will use an algorithm that give us "OK" security but nothing you would want to use if you really wanted to make an unbreakable code.

To make things a little more easier (and a bit less secure), we will use an algorithm in which uses the same program for both the Encrypting program and the Decrypting program. Thus:

X -->
Encryption
Program
--> X' -->
Encryption
Program
--> X

We will do two changes to the sound data to encrypt it. We will

  1. swap (or exchange) two blocks of data
  2. reverse the swapped blocks of data

The blocks of data will consist of some number of consectutive samples from the sound object. This number will be called the block size This number will be specified by the user when encrypting or decrypting the sound object. So in order to properly decrypt the sound object, the block size used must be the same as the block size used when the sound was encrypted. A good value for the block size would be between 1000 to 15000. Since a sound object normally has 22,050 sample per second, a block size of 2205 would give us 10 blocks per second (or have each block be 0.1 second long). A block size of 11,025 would give us 2 blocks per second.

At some point we will need to translate block numbers into sample indexes. The following table should help us figure out how to do this translation. The table assumes that you are using the JES function of getSampleValueAt() or setSampleValueAt() when accessing the samples from a sound object.

Block Size
1000 2205 5000 11025 b
Block Number Starting
Sample
index
Ending
Sample
Index
Starting
Sample
index
Ending
Sample
Index
Starting
Sample
index
Ending
Sample
Index
Starting
Sample
index
Ending
Sample
Index
Starting
Sample
index
Ending
Sample
Index
11100012205150001110251b
210012000220644105001100001102622050 b+12*b
3200130004411661510001150002205133075 (2*b)+13*b
4300140006615882015001200003307644100 (3*b)+14*b
i         ((i-1)*b)+1i*b

How Will We Decide Which Blocks to Swap?

We will need to create a sequence of numbers that will tell us which blocks to switch. There are many sequences that we could use, but we will pick the Fibonacci sequence. The first two values in the Fibonacci sequence are 1 and 1. For the rest of the values in the Fibonacci sequence, we add the two previous values together, so the third value is 1 + 1 or 2. The fourth value is 1 + 2 or 3. The fifth value is 2 + 3 or 5. Refer to the following table:
Fib(1)=  1
Fib(2)=  1
Fib(3)=Fib(1) + Fib(2)=2
Fib(4)=Fib(2) + Fib(3)=3
Fib(5)=Fib(3) + Fib(4)=5
Fib(6)=Fib(4) + Fib(5)=8
Fib(7)=Fib(5) + Fib(6)=13
Fib(8)=Fib(6) + Fib(7)=21
Fib(9)=Fib(7) + Fib(8)=44
Fib(10)=Fib(8) + Fib(9)=65
...
Fib(n)=Fib(n-2) + Fib(n-1)

For our encryption algorithm, we will swap the (Fib(n))-th block with the (2*(Fib(n))-th block. Thus we will swap the following blocks:

Swap 1:Swap block 1 with block 2
Swap 2:Swap block 1 with block 2
Swap 3:Swap block 2 with block 4
Swap 4:Swap block 3 with block 6
Swap 5:Swap block 5 with block 10
Swap 6:Swap block 8 with block 16
Swap 7:Swap block 13 with block 26
Swap 8:Swap block 21 with block 42
Swap n:Swap block Fib(n) with block 2*Fib(n)
Note that the first two swaps do the same thing. So the second swap undoes the first swap. We will continue to swap values as long as the value of 2*Fib(n) is less than the number of blocks in the sound object.

Needed Functions

You must have the following two functions in your code.

Recommened Functions

These functions are ideas of functions you can write that encrypt() will call while it is encrypting/decrypting the sound object. While you don't have to use these function, it is strongly suggested that you do. Using these should make your life easier while writing this assignment.

How do We swap Values?

Swapping or exchanging two values is a common thing to do, but it requires the use of a third storage spot to hold one value while the other is moved. For example, assume we have two variables, val1 and val2. To swap the values in these two variables, we need a third variable (assume it is valled val3). First we move the value in val1 to val3. Then we move the value in val2 to val1. Finally we move the value in val3 to val1.
StepCode Value in val1 Value in val2 Value in val3
0.1val1 = 88  
0.2val1 = 585 
1val3 = val1 858
2val1 = val2 558
3val2 = val3 588

How Do We Create Code for the Fibonacci Sequence?

There are a number of ways to write code to produce the Fibonacci Sequence. We will use the following to produce the n-th value from the Fibonacci sequence. I will leave the actual code to you to create.

Submitting Your Project

Your code must be commented and written in good programming style. You are to submit your final program to the digital drop box on the CS 101 Blackboard site.