Project UltiMatrix Tarun Pondicherry Michael Navara Katrina Pizarro Alex Rodriguez Aaron Schaff
Overview Requirements Responsibilities Schedule Encryption Algorithm C+ + Function Overview Function Algorithms Final Product UltiMatrix 2
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 other class? UltiMatrix 3
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 UltiMatrix 4
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 UltiMatrix 5
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 UltiMatrix 6
Implementing Matrices in C+ + Consider the matrix: 1 2 3 A[0] = 1 2 3 = A 4 5 6 4 5 6 A[1] = 7 8 9 7 8 9 A[2] = char A[3][3] = { 1, 2, 3, In effect, this is any array of arrays where the first 4, 5, 6, subscript refers to another 7, 8, 9} ; array UltiMatrix 7
Implementing Matrices in C+ + A different perspective of memory: 1 2 3 4 5 6 7 8 9 [0] [1] [2] [0] [1] [2] [0] [1] [2] A[0] A[1] A[2] Why not implement this as a single subscripted array? 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 [0] [1] [2] [0] [1] [2] [0] [1] [2] [0] [1] [2] [3] [4] [5] [6] [7] [8] A[0] A[1] A[2] UltiMatrix 8
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 UltiMatrix 9
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 typedef struct { short* values; Oh no! A pointer! char rows; The number of rows Fear not, as far as we are char cols; concerned this is an array The number of columns containing the values } Matrix; UltiMatrix 10
Implementing Matrices in C+ + Reconsider the matrix: Matrix A; 1 2 3 short vals[] = { 1, 2, 3, = 4, 5, 6, A 4 5 6 7, 8, 9} ; A.values = vals; 7 8 9 A.rows = 3; A.cols = 3; To virtually recreate the matrix from the array, use the formula: A ij = (i-1)* (columns) + (j-1) UltiMatrix 11
Implementing Matrices in C+ + A ij = (i-1)* (columns) + (j-1) This formula is the basis for Matrix access which is encapsulated in the functions: 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 UltiMatrix 12
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 UltiMatrix 13
Function Dependencies main MatrixMath.h Encrypt createKey bool I nverse Decrypt short Determinant short Cofactor void Minor void Adjoint void Transpose void ScalerMultiply void Multiply UltiMatrix 14
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 UltiMatrix 15
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 UltiMatrix 16
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 UltiMatrix 17
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 UltiMatrix 18
Example Message: Hello Computer A random key is generated: − 1 2 3 4 5 6 − 7 8 9 Determinant function checks if the key is invertible UltiMatrix 19
Example (cont’d) Message is converted into long and skew value is subtracted from the values This becomes the output array: 1 st value Puts delimiter 1 st value In encrypted 2nd value Key size 2nd value In key Skew value array , 3 , 94 , -1 , 2 … , Value , Value 0 1 2 3 4 5 6 7 … UltiMatrix 20
Example (cont’d) 1 st value Creates empty key matrix: 2 nd value In key Value , Value _ _ _ … _ _ _ Puts values into matrix _ _ _ and removes delimiter UltiMatrix 21
Example (cont’d) The key is inverted using matrixInverse: − − . 3875 . 175 . 0125 − . 325 . 05 . 075 − − . 0125 . 091 6 . 0541 6 Product Matrix is created: _ _ _ 0 _ _ _ 1 _ _ _ 2 0 1 2… UltiMatrix 22
Example (cont’d) Encrypted matrix is created: 1 st value 2 nd value _ _ _ Value , Value _ _ _ … _ _ _ 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! UltiMatrix 23
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. UltiMatrix 24
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 of the determinant and stored in the output matrix Allocated memory is given back to the system UltiMatrix 25
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) UltiMatrix 26
matrixCofactor Memory is dynamically allocated for a matrix that has one less row and column than the input matrix 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 UltiMatrix 27
matrixMinor Minor for Element: row= 2, column= 1 i= 1 i= 2 i= 3 j= 1 j= 2 j= 3 j= 1 j= 2 j= 3 j= 1 j= 2 j= 3 i= = Row? YES NO NO j= = Column? NO YES NO YES a a a 11 12 13 a a a a 12 x a x a a x = = = = = 12 12 13 13 = 12 13 B B B B B A a a a 21 22 23 a x a x x x a x x x 32 32 33 a a a 31 32 33 UltiMatrix 28
Recommend
More recommend