Project UltiMatrix
Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff
Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro - - PowerPoint PPT Presentation
Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff Overview Requirements Responsibilities Schedule Encryption Algorithm C+ + Function Overview Function Algorithms Final
Project UltiMatrix
Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff
2
UltiMatrix
Overview
Requirements Responsibilities Schedule Encryption Algorithm Function Overview Function Algorithms Final Product
C+ +
3
UltiMatrix
Requirements
WRI TE A C+ + PROGRAM TO ENCRYPT & DECRYPT A TEXT MESSAGE USI NG A 3x3 MATRI X AS A KEY TO ENCODE THE MESSAGE AND THE I NVERSE MATRI X TO DECODE THE MESSAGE
Hmmm…
I wonder if I can transfer to the
4
UltiMatrix
Responsibilities
Team Leader: Tarun Pondicherry
User Interface Functions
startApp – Tarun Pondicherry init – Tarun Pondicherry
Cryptography.h
Encrypt – Katrina Pizarro Decrypt – Katrina Pizarro createKey – Alex Rodriguez
5
UltiMatrix
Responsibilities
MatrixMath.h Functions
matrixInverse – Tarun Pondicherry matrixDeterminant – Aaron Schaff matrixCofactor –Tarun Pondicherry matrixMinor – Tarun Pondicherry matrixAdjoint – Michael Navera matrixTranspose – Aaron Schaff matrixMultiply – Alex Rodriguez matrixScalerMultiply – Michael Navera
6
UltiMatrix
Implementing Matrices in C+ +
The first method that comes to mind is to use
double subscripted arrays where the first subscript corresponds to the row and the second corresponds to the column
However, this poses two issues:
Arrays are zero based but matrices are one based Passing a double subscripted array to a function
without pointers poses limitations on expanding the program to handle larger matrices
7
UltiMatrix
Implementing Matrices in C+ +
Consider the matrix:
= 9 8 7 6 5 4 3 2 1 A
1 2 3 4 5 6 7 8 9
A[0] = A[1] = A[2] =
In effect, this is any array of arrays where the first subscript refers to another array
char A[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9} ;
8
UltiMatrix
Implementing Matrices in C+ +
A different perspective of memory: Why not implement this as a single subscripted array?
1 2 3 4 5 6 7 8 9
A[0] A[1] A[2] [0] [1] [2] [0] [1] [2] [0] [1] [2]
1 2 3 4 5 6 7 8 9
A[0] A[1] A[2] [0] [1] [2] [0] [1] [2] [0] [1] [2]
1 2 3 4 5 6 7 8 9
[0] [1] [2] [3] [4] [5] [6] [7] [8]
9
UltiMatrix
Implementing Matrices in C+ +
However, now we cannot distinguish between
matrices of different sizes (1x9, 3x3, 9x1)
Unless… we know the dimensions and can
virtually recreate the matrix
So this would mean passing the dimensions
to each and every matrix function
This can become extremely tedious and
unproductive
10
UltiMatrix
typedef struct { short* values; char rows; char cols; } Matrix;
Oh no! A pointer! Fear not, as far as we are concerned this is an array containing the values The number of rows The number of columns
The Matrix Structure
To make things easier to pass to functions,
the Matrix structure is used
It contains the array and the dimensions Now all that needs to be passed is the matrix
11
UltiMatrix
Implementing Matrices in C+ +
= 9 8 7 6 5 4 3 2 1 A
Reconsider the matrix:
Matrix A; short vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9} ; A.values = vals; A.rows = 3; A.cols = 3;
To virtually recreate the matrix from the array, use the formula:
Aij = (i-1)* (columns) + (j-1)
12
UltiMatrix
Implementing Matrices in C+ +
This formula is the basis for Matrix access which is encapsulated in the functions:
Aij = (i-1)* (columns) + (j-1)
short getElement(Matrix iMatrix, char i, char j) { return iMatrix.values[(i-1)* (iMatrix.cols) + (j-1)]; } void setElement(Matrix iMatrix, char i, char j, short newVal) { Matrix.values[(i-1)* (iMatrix.cols) + (j-1)] = newVal; }
These functions are overloaded for different data types
13
UltiMatrix
Implementing Matrices in C+ +
Now, we can pass Matrices on any size The functions handle the issue of altering
the zero base of arrays to the one of matrices
For the sacrifice of memory, this makes
coding matrix functions much easier, more functional and expandable
14
UltiMatrix
Function Dependencies
main
Encrypt Decrypt createKey
MatrixMath.h
bool I nverse short Determinant short Cofactor void Adjoint void Transpose void ScalerMultiply void Multiply void Minor
15
UltiMatrix
MainLooper.h
Contains function main Resizes window Sets title Calls init() Prints title Calls startApp() Asks “Would You Like to Continue?” Clears Screen and Loops
16
UltiMatrix
startApp
Asks user whether to encrypt or decrypt Encrypt
Asks for an output file Creates it if necessary Clears the new line in buffer (cin.ignore()) Asks for user input through cin.getline() Calls Encrypt and outputs to the file
Decrypt
Asks for input file Checks for existence Reads it line by line Calls Decrypt Displays results
17
UltiMatrix
Encrypt
Put input string into Matrix array Convert into long integers and skew Get random generated key (createKey) Multiply
key and Matrix array (matrixMultiply)
Place result, key, key size, delimiter and
skew into encrypted output array
18
UltiMatrix
Decrypt
Parse output array into key, key size,
data and skew
Find inverse of key Multiply inverse of key and encrypted
array
Convert result to ASCII (chars) and
correct skew
19
UltiMatrix
Example
Message: Hello Computer A random key is generated: Determinant function checks if the key is
invertible
− − 9 8 7 6 5 4 3 2 1
20
UltiMatrix
Example (cont’d)
0 1 2 3 4 5 6 7 … Puts delimiter
Key size
3 , 94
Skew value
1st value In key
2nd value
1st value In encrypted array 2nd value
is subtracted from the values
21
UltiMatrix
Example (cont’d)
Creates empty key matrix:
Value , Value
2nd value
1st value In key
Puts values into matrix and removes delimiter
22
UltiMatrix
Example (cont’d)
The key is inverted using matrixInverse: Product Matrix is created:
− − − − − 6 0541 . 6 091 . 0125 . 075 . 05 . 325 . 0125 . 175 . 3875 .
_ _ _ _ _ _ _ _ _
1 2 0 1 2…
23
UltiMatrix
Example (cont’d)
Encrypted matrix is created: Finally, the inverted key and encrypted matrix
are multiplied using matrixMultiply and the result is placed into the empty product matrix and is converted back to ascii!
_ _ _ _ _ _ _ _ _
Value , Value
2nd value 1st value
…
24
UltiMatrix
createKey
Since the number a rows and columns in the
input matrix is unknown, the function loops for the number of rows and columns in the input matrix.
The function then places a random value in
the matrix using the rand and srand functons.
After looping through the entire matrix the
function calls matrixDeterminant and if it returns zero it creates an new key. If it returns non-zero then the function returns the generated key.
25
UltiMatrix
matrixInverse
First,
the function ensures that the determinant is not zero
Memory is dynamically allocated to hold the
adjoint of the matrix
This matrix is populated via matrixAdjoint The adjoint matrix is multiplied by the inverse
matrix
Allocated memory is given back to the system
26
UltiMatrix
matrixDeterminant
First, the size of the matrix is checked If it is a 1 x 1 matrix, the determinant is the value of
the element
Otherwise, each element in the first row is multiplied
by its cofactor
The sum of these products is returned as the
determinant
This function calls cofactor, which calls determinant
so they are in effect recursive
The stop case is a 1 x 1 matrix (whose determinant is
the value)
27
UltiMatrix
matrixCofactor
Memory is dynamically allocated for a matrix that has
That matrix is then populated with the requested
minor via the matrixMinor function for element i,j
The determinant of that minor is found via the
matrixDeterminant function and signed appropriately based on parity
Allocated memory is given back to the system The appropriately signed determinant is returned
28
UltiMatrix
matrixMinor
=
33 32 31 23 22 21 13 12 11
a a a a a a a a a A
= x x x x B
Minor for Element: row= 2, column= 1
i= 1 j= 1
i= = Row? j= = Column?
j= 2 j= 3 i= 2 i= 3 j= 1 j= 2 j= 3 j= 1 j= 2 j= 3
NO YES
= x x x a12 B = x x a a
13 12
B = x a a a
32 13 12
B =
33 32 13 12
a a a a B
NO YES NO YES NO
29
UltiMatrix
matrixTranspose
=
33 32 31 23 22 21 13 12 11
a a a a a a a a a A
The function loops through the rows and columns
element j,i of the output matrix
= x x x x x x x x x B = x x a x x a x x a
13 12 11
B = x a a x a a x a a
23 13 22 12 21 11
B =
33 23 13 32 22 12 31 21 11
B a a a a a a a a a
30
UltiMatrix
matrixAdjoint
The function loops through the rows
and columns of the first matrix and places each element i,j in element j,i of the cofactor matrix.
31
UltiMatrix
matrixMultiply
= C c b a c b a c b a B c c c b b b a a a A *
2
3a
2
a
Is the number of columns in matrix A the same as the rows in Matrix B? YES This pattern continues until matrix C is complete, then function is complete. Example: + + = 3x3 3x3 3 x Matrix A’s columns Matrix B’s rows 3
2
a
2
a
32
UltiMatrix
matrixScalerMultiply
The
function loops through each element
the input matrix
Each element is multiplied by a
scaler and placed in the same position in the output matrix
33
UltiMatrix
Full Demonstration
34
UltiMatrix
35
UltiMatrix
36
UltiMatrix
Contents of message.txt
,4,0,0,2,-5,9,-3,-3,1,-6,-9,-5,1,6,9,-1,-8,8,-339,-74,-429,-569,
,4,0,1,2,6,-6,6,-1,9,-5,7,2,-10,0,-7,9,3,3,325,443,-366,280,729, 937,524,866,855,1079,-97,999,1276,1400,772,987,-269,
440,1105 ,4,0,9,-4,-8,-4,7,-5,3,4,8,0,2,-4,8,-8,8,-10,-300,-29,-580,-123,- 76,860,989,644,906,855,240,596,-164,824,1150,-328,-210,- 954,628,1352
37
UltiMatrix
38
UltiMatrix
Acknowledgments/Sources
Acknowledgements:
Mr. Paterno Canned Doit Software – Matrix
Calculator Sources:
C+ + book Mr. Paterno’s slide show on Matrix
Algebra and Cryptography