07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ Cryptographic Math and Frequency Analysis 117

Solving Systems of Linear Equations

In this section, you will learn how to solve systems of linear equations using

Python. Linear equations can increase the level of difficulty in breaking cryptographic

schemes by using large matrices to encode and decode messages. The

first matrix is typically referred to as an encoding matrix. While we could start

writing programs from scratch, linear equations have been made much simpler

using the NumPy library. NumPy also provides methods that will allow

you to multiply matrices without having to worry about how the rows and

columns are being multiplied. NumPy also provides linear algebra libraries to

find inverses and determinates quickly. The inverse of a matrix is known as the

decoding matrix. For our example, we examine the following:

1a + 1b = 35

2a + 4b = 94

These two equations will look like the following. Notice that the values from

the first equation fill in the top row, while the values from the second fill the

second row:

11 a

A X B

24 b

35

94

When we write this in matrices form, it should be in the form of AX = B.

To solve this equation (and solve for X), you multiply both sides of the equation

for the inverse of A (shown as A −1 ). A −1 multiplied by A will produce the identity

matrix: A −1 AX = A −1 B. This will allow us to isolate X such that X = A −1 B.

Completed, it should resemble the following:

11

24

a

b

35

94

Here is the Python code that will produce the matrices shown previously:

import numpy as np

# solve the following linear equation

print ('1a + 1b = 35')

print ('2a + 4b = 94')

A = np.matrix([[1,1],[2,4]])

B = np.matrix([[35],[94]])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!