Playfair Cipher - Explanation + Setup

Formula Intuition
31 Oct 202020:21

Summary

TLDRThis video tutorial delves into the Playfair cipher, a block cipher that encrypts pairs of letters using a 5x5 matrix, known as the Polybius square. It contrasts with stream ciphers by encrypting two letters at a time, creating 600 possible 'digrams'. The video explains the cipher's initialization with a key matrix, handling of repeated letters, and the encryption process involving shifting within rows or columns and swapping for diagonals. It also covers decryption, which reverses these steps, and provides insights into coding the algorithm in C++, emphasizing modular arithmetic and matrix manipulation.

Takeaways

  • 🔐 The Playfair cipher is a block cipher that encrypts two letters at a time, unlike stream ciphers which encrypt one letter at a time.
  • 🔢 It uses a 5x5 matrix known as the Polybius square to arrange the letters of the alphabet, omitting one letter (traditionally 'J') to fit into the matrix.
  • 🔄 The Playfair cipher has three rules for encryption: shift right for same row, shift down for same column, and swap columns for diagonal.
  • 🔐 The process involves a pre-initialization stage where a key matrix is generated from the user's input key.
  • 🔡 The key matrix is filled by first placing the key letters into the matrix without repetition, then filling the rest with the remaining letters of the alphabet.
  • ✂️ If a plain text has an odd number of letters, 'X' is added to make it even for the encryption process.
  • 🔄 Decryption in Playfair is the reverse of encryption, using the same rules but in opposite directions for shifting.
  • 💻 The script describes a method for coding the Playfair cipher in C++, including functions for encryption, decryption, creating pairs, and generating the key matrix.
  • 🔍 The 'find in matrix' function is used to check if a letter has already been used in the key matrix to avoid repetitions.
  • 🔗 The script emphasizes the importance of managing spaces and ensuring the string length is even for the encryption process to work correctly.

Q & A

  • What is the main difference between stream ciphers and block ciphers?

    -Stream ciphers encrypt one letter at a time, whereas block ciphers encrypt multiple letters at once. The Playfair cipher, for example, encrypts two letters at a time.

  • Why is the Playfair cipher considered a block cipher?

    -The Playfair cipher is considered a block cipher because it encrypts two letters at a time, forming what are called 'digrams', as opposed to the one-letter-at-a-time approach of stream ciphers.

  • How many possible digrams are there in the Playfair cipher?

    -There are 600 possible digrams in the Playfair cipher, calculated by the formula 25 factorial over 23 factorial, since it encrypts two letters at a time.

  • What is a Polybius square and how does it relate to the Playfair cipher?

    -A Polybius square is a 5x5 2D array or matrix used in the Playfair cipher to place each letter of the alphabet. It's related to the Playfair cipher as it forms the basis of the key matrix used for encryption.

  • Why is the letter 'J' omitted in the Playfair cipher's Polybius square?

    -The letter 'J' is omitted in the Playfair cipher's Polybius square because the matrix is 5x5, allowing only 25 letters. 'J' is typically removed, but any letter can be omitted; 'J' is a standard choice.

  • What is the pre-initialization stage in the Playfair cipher?

    -The pre-initialization stage in the Playfair cipher involves generating a key matrix using the user-provided key, which is then used for encryption.

  • How are pairs created from plain text in the Playfair cipher?

    -Pairs are created by splitting the plain text into pairs of two letters each. If a letter is repeated, an 'X' is inserted to break the repetition, and if the text length is odd, an 'X' is added at the end.

  • What are the three rules for encrypting pairs in the Playfair cipher?

    -The rules for encrypting pairs are: if pairs are on the same row, shift right modulo 5; if on the same column, shift down modulo 5; if diagonal, swap the columns but keep the rows.

  • How does decryption work in the Playfair cipher?

    -Decryption in the Playfair cipher is the reverse of encryption: shift left modulo 5 for same-row pairs, shift up modulo 5 for same-column pairs, and swap columns for diagonal pairs.

  • Why is the 'removeX' function necessary in the Playfair cipher?

    -The 'removeX' function is necessary to remove any 'X' letters that were inserted to handle repeated letters or odd-length plain text, ensuring the original message is accurately reconstructed after decryption.

Outlines

00:00

🔐 Introduction to the Playfair Cipher

The video introduces the Playfair cipher, contrasting it with stream ciphers by explaining that it's a block cipher encrypting two letters at a time instead of one. The concept of digraphs is introduced, and the necessity of a 5x5 Polybius square is explained, which accommodates 25 unique letters of the alphabet (excluding 'j'). The video also covers the pre-initialization process of creating a key matrix using the user-inputted key, ensuring no letter repeats in the matrix. The process of preparing the plaintext for encryption by creating pairs of letters is also discussed, including the handling of repeated letters and odd-length strings.

05:01

🔄 Playfair Cipher Encryption Process

This section delves into the encryption rules of the Playfair cipher. It explains how to handle pairs of letters based on their position relative to each other in the key matrix: shifting right for same-row pairs, shifting down for same-column pairs, and swapping columns for diagonal pairs. The video provides a step-by-step example of how to encrypt the word 'hello' using the Playfair cipher, resulting in the ciphertext 'kgyvrv'. The decryption process is also briefly touched upon, indicating that it involves reversing the encryption steps.

10:01

💻 Coding the Playfair Cipher Algorithm

The paragraph discusses the implementation of the Playfair cipher algorithm in C++. It emphasizes the need to handle spaces and repeated letters when preparing the plaintext for encryption. The process involves adding letters one by one, inserting an 'x' when a repeat is detected, and ensuring the final string length is even by appending an 'x' if necessary. The paragraph also mentions the inclusion of necessary header files and the use of a 'find in matrix' function to locate letters within the key matrix.

15:02

🔑 Generating the Key Matrix

This part of the script explains how to generate the key matrix for the Playfair cipher. It involves inserting letters of the key into a 5x5 grid while ensuring no repeats, then filling the remaining spaces with the rest of the alphabet, excluding any letters already used. The process is facilitated by a 'used' vector to track inserted letters and a counter to manage the transition from inserting key letters to filling in the alphabet. Special attention is given to omitting the letter 'j' by default.

20:07

📝 Final Thoughts on the Playfair Cipher Implementation

The final paragraph briefly confirms the successful implementation of the key matrix generation for the Playfair cipher. It implies that the algorithm is working as expected, suggesting a positive outcome from the efforts to code the cipher in C++.

Mindmap

Keywords

💡Playfair cipher

The Playfair cipher is a type of encryption technique categorized as a block cipher. It encrypts two letters (a pair) at a time, rather than individual letters as in stream ciphers. In the video, this cipher is the focus of the explanation, and its encryption process involves a 5x5 matrix and rules that dictate how pairs of letters are encrypted.

💡Block cipher

A block cipher encrypts data in fixed-size blocks, in contrast to stream ciphers, which encrypt one letter at a time. In the Playfair cipher, two letters are encrypted at a time, forming what are called diagrams or bigrams. This concept is central to understanding how the Playfair cipher operates, as it processes chunks of data rather than individual letters.

💡Polybius square

The Polybius square is a 5x5 grid used in the Playfair cipher to arrange letters of the alphabet for encryption. It can only hold 25 letters, so one letter (usually 'J') is omitted. This square is essential for mapping pairs of letters and applying the rules of encryption in the Playfair method.

💡Diagrams/bigrams

In the Playfair cipher, two-letter combinations (called diagrams or bigrams) are used instead of single letters. This pairing method expands the possible encrypted outputs, increasing the complexity of the cipher. The video explains that there are 600 possible diagrams, which greatly strengthens the encryption compared to single-letter (monogram) ciphers.

💡Key matrix

The key matrix is a 5x5 grid filled with letters used in the Playfair cipher. This matrix is generated based on a key provided by the user. It determines how pairs of letters are encrypted. In the video, the 'create key matrix' function is essential for setting up the encryption process by arranging the alphabet and filling in the remaining letters after accounting for the key.

💡Modular arithmetic

Modular arithmetic is used in the Playfair cipher to handle shifts when encrypting letters. For example, when letters are shifted right or down within the 5x5 matrix, if they go beyond the edge of the grid, they wrap around to the beginning, using modulo 5 arithmetic. This ensures that the encryption process stays within the boundaries of the matrix.

💡Encrypt function

The encrypt function in the Playfair cipher takes the key matrix and the pairs of letters (bigrams) and applies the rules of encryption. Depending on whether the pairs are in the same row, column, or diagonal in the matrix, different shifting or swapping operations are performed to encrypt the data. This function is crucial to the overall process described in the video.

💡Decrypt function

The decrypt function reverses the Playfair encryption process by applying inverse operations to retrieve the original plaintext. The same key matrix is used, and the encrypted pairs of letters are shifted or swapped in the opposite direction. This concept highlights the reversible nature of the cipher, allowing encrypted messages to be decoded using the same method.

💡Same row/column rule

In the Playfair cipher, if a pair of letters appears in the same row of the key matrix, they are shifted right (or left during decryption). Similarly, if the pair is in the same column, they are shifted down (or up during decryption). These specific rules govern how letters are encrypted or decrypted, ensuring that the transformation follows a logical pattern within the matrix.

💡Remove X function

The remove X function is used in the decryption process to handle placeholder letters, such as 'X', which are inserted during encryption when duplicate letters or odd-length plaintexts occur. This function ensures that the decrypted message restores the original text by removing unnecessary 'X' characters added during the encryption phase.

Highlights

Introduction to the Playfair cipher, a block cipher that encrypts two letters at a time.

Explanation of the shift from stream ciphers to block ciphers in cryptographic methods.

The Playfair cipher uses a 5x5 matrix, called a Polybius square, to encrypt text.

The concept of 'diagrams' is introduced for two-letter combinations in the Playfair cipher.

Calculation of possible diagrams in the Playfair cipher: 25 factorial over 23 factorial.

Omission of the letter 'j' in the Polybius square to fit 25 letters into a 5x5 matrix.

Pre-initialization stage required to generate a key matrix using the user-inputted key.

Process of filling the Polybius square with the rest of the alphabet after inserting the key.

Handling of repeated letters in plaintext by inserting an 'x' and adjusting pairs.

Ensuring the plaintext length is even for encryption, with an 'x' added if odd.

Three sets of rules for encryption in the Playfair cipher: same row, same column, and diagonal.

Use of modular arithmetic for shifting letters in the matrix.

Decryption process as the reverse of encryption with adjusted shifting directions.

The creation of pairs from plaintext and the handling of repeated letters.

Coding considerations for managing spaces and ensuring even string lengths.

Algorithm for generating the key matrix with checks for repeated letters.

Use of a vector to track used letters and ensure no repeats in the matrix.

Two-stage process for filling the key matrix: inserting the key and then filling in the alphabet.

Method for ensuring no gaps are left in the matrix during the insertion of letters.

Final output of the encrypted text after applying the Playfair cipher rules.

Testing the algorithm with the example 'hello' and the key 'playfair'.

Transcripts

play00:00

welcome back to another video this video

play00:03

is on the playfair cipher

play00:06

so this cipher method is different to

play00:08

the previous videos we've done

play00:11

because the previous videos are in a

play00:13

certain classification of ciphers called

play00:15

stream ciphers

play00:16

now we've seen what stream ciphers do a

play00:18

stream cipher just takes in one letter

play00:20

at a time

play00:21

and encrypts over the key the play for

play00:23

cipher is however

play00:25

a different type of cipher called a

play00:27

block cipher

play00:28

this still isn't modern encryption but

play00:30

modern cryptography is highly

play00:32

um oriented around a block ciphers

play00:36

so in a block cipher instead of

play00:38

encrypting one letter at a time

play00:40

this time we're encrypting two letters

play00:42

at a time

play00:44

so with the substitution cipher if we're

play00:46

encrypting one at

play00:47

a time that means we have 26 possible

play00:50

monograms right

play00:52

since there's 26 letters in the alphabet

play00:54

whereas in the play first cipher

play00:56

because we're encrypting two letters at

play00:57

a time but and when you're encrypting

play00:59

two letters they're called diagrams

play01:01

instead of monograms

play01:02

or bi-grams and because we're encrypting

play01:04

two letters at a time

play01:06

mata means we have 600 possible diagrams

play01:10

and you get that by just doing 25

play01:12

factorial over 23 factorial

play01:15

and i'll show you why it's 25 and not

play01:17

26.

play01:18

in the play first cipher we make use of

play01:21

this concept called

play01:22

a polybius square and what this is is

play01:25

it's simply

play01:26

a 5x5 2d array or a matrix

play01:30

and we simply put in each letter in the

play01:32

alphabet inside of this matrix

play01:36

although we don't repeat any of the

play01:37

letters and because

play01:39

it's five by five that means we can only

play01:41

have 25 possible entries in the matrix

play01:44

right

play01:44

so we can only put 25 letters from the

play01:46

alphabet in this matrix

play01:48

as such we have to emit a letter

play01:51

in this case we omit the letter j so if

play01:54

you look

play01:55

at the sequence in in the key block we

play01:58

go

play01:58

h i k and we miss the letter j that's

play02:01

just a standard for the playfair cipher

play02:03

you can change the letter to anything

play02:05

you want you can emit whatever

play02:07

letter you want so unlike the other

play02:09

cipher methods where you just take in

play02:11

the key and you encrypt one letter at a

play02:12

time

play02:13

in the playfair cipher there is a

play02:15

pre-initialization stage where you need

play02:17

to

play02:17

generate a key matrix using the key

play02:20

that's inputted by the user and so

play02:22

simply

play02:23

if we have the key playfair and we throw

play02:25

it into this

play02:26

create key matrix function the output we

play02:29

get is

play02:30

is this 2d array and if you noticed we

play02:32

don't repeat

play02:33

any of the letters so your p l a

play02:37

y f and when you get to a we've seen a

play02:39

is already in the 2d array

play02:42

so we skip a and we move to i and then

play02:44

we go r

play02:45

now we filled in every letter in the key

play02:48

so

play02:49

what we do is we fill in the 2d array

play02:51

with the rest of the alphabet

play02:52

starting from the beginning so you try

play02:54

to add an a because we can see a is

play02:56

already there

play02:57

so we do b instead then we add c and d

play03:00

and then f is already used so we go to g

play03:04

is already so we go to h okay l is used

play03:07

so you do m instead

play03:08

n o p is used so you do q

play03:12

s t u v w

play03:15

x and z and y is used in the original

play03:18

key

play03:19

and now we're just left with one more

play03:22

pre-initialization stage

play03:24

before we can start encrypting and

play03:26

that's creating the pairs out of our

play03:28

plain text

play03:30

now since we're encrypting two letters

play03:31

at a time we want to

play03:34

split our string into pairs two letters

play03:36

at a time right

play03:37

so if you have the plain text hello and

play03:39

we throw hello into create pairs

play03:41

we first split the string into pairs of

play03:44

two

play03:44

so we put he on its own l on its own and

play03:47

o on its own

play03:49

first of all the playfair cipher doesn't

play03:50

let you repeat any letters

play03:52

so instead of pairing l with another l

play03:55

we pair the first l with an

play03:56

x that we insert in and then pair the

play04:00

second l with the o

play04:01

so it becomes h e l x l o instead

play04:05

and um they're supposed to be in pairs

play04:07

but when we're coding this

play04:08

there won't be any gaps there won't be

play04:10

any spaces in between so in the end

play04:11

it'll just look like h e l

play04:13

x l o now what's important to notice is

play04:16

since they're in pairs

play04:18

the length of the string is even however

play04:20

if we had the case where

play04:21

the length of our string was odd at this

play04:23

point that means there is no pair for

play04:25

the final letter

play04:26

and so to fix this we would just throw

play04:28

an x at the end

play04:30

so now that we've finished

play04:32

pre-initializing our data

play04:34

we can start encrypting as you can see

play04:36

we combine our key matrix with the pairs

play04:38

that we've generated together and throw

play04:40

it into the encrypt function

play04:43

now the playfair cipher has three sets

play04:45

of rules for encrypting

play04:47

if our pairs of letters are on the same

play04:49

row then we

play04:50

shift right modulo 5. if our pairs of

play04:53

letters are on the same column then we

play04:55

shift down modulo 5.

play04:57

by module 5 i just mean we're doing

play04:59

modular arithmetic

play05:00

so if i would usually get an index out

play05:02

of bounds exception we just use module 5

play05:04

to loop around the 2d array

play05:05

and if it's the case that it's neither

play05:08

on the same row or column that means the

play05:10

letters are diagonal

play05:11

and so the method we use is we just swap

play05:13

the columns but keep the rows

play05:15

let's take the letters h and e so let's

play05:17

find where h

play05:18

and e is in our 2d array so h is here

play05:22

and e is here so let's look at our rules

play05:24

that are on the same row

play05:26

so we shift right to modulo 5. so we

play05:28

shift h by one unit and it becomes k

play05:31

and we shift the e by one unit and it

play05:32

becomes g so our output is k

play05:35

g as you can see we got kg here

play05:38

now let's do the next one lx our l is

play05:41

here and our x is here

play05:43

they're not on the same row and they're

play05:45

not on the same color column

play05:47

so they're diagonal so let's look at the

play05:49

rule for diagonal we swap the columns

play05:51

so if i swap the columns first of all

play05:53

you need to notice that we create a box

play05:55

here we create a rectangle

play05:57

and we want the letters on the opposite

play05:58

corner so we have l and x on these

play06:00

corners

play06:01

so the one the opposite one for l is y

play06:04

and the opposite corner for x is v

play06:06

so our output is y v such we get y v

play06:09

here let's look at the final one l o

play06:12

we have l here and o here they're on the

play06:15

same row

play06:16

so we shift down modulo 5. as such we

play06:20

shift l down to r and o down to v so we

play06:23

got r

play06:23

v and as you can see our output is rv so

play06:26

now that we have

play06:27

each individual output for our pairs we

play06:30

just join them together we can cut any

play06:32

to the strings together so we get kgyv

play06:34

rv

play06:36

and that's our final encrypted text

play06:39

now decrypting is very easy we just do

play06:42

the exact same thing but in reverse

play06:44

we have our decrypt function we throw in

play06:46

the same

play06:48

k matrix but this time we throw in the

play06:51

encrypted text instead now if you look

play06:54

at our encrypt

play06:55

in our encryption rules if they're on

play06:56

the same row instead of shifting right

play06:59

we shift to left modulo 5.

play07:01

for the same column instead of shifting

play07:03

um down we shift up modulo 5

play07:06

and for diagonals that the rule is the

play07:08

same swap the columns

play07:10

let's look at kg so k is here

play07:13

and g's here they're on the same row so

play07:16

we shift the left modulo 5.

play07:18

so for k we get h and for g we get to e

play07:21

so

play07:21

our output is h e for the next one we

play07:24

have y

play07:25

v so y is here and v is here

play07:29

and there are diagonal so we swap the

play07:31

columns our output is l and x

play07:33

such we get l and x and finally r

play07:36

v we have r here and v here and they're

play07:39

on the same

play07:40

row so we they're on the same column

play07:42

sorry and we shift up modulo 5.

play07:44

so our output is l in r we concatenate

play07:47

these together and we get h-e-l-x-l-o

play07:50

so remember we had this x in the middle

play07:52

because there was a repeat

play07:54

l and because of this we need an extra

play07:56

function here called removex so we throw

play07:58

this output

play08:00

into the remove x function and then this

play08:02

just spits out the original input

play08:04

without that

play08:04

x in the middle and here that's that's

play08:07

how the playfast cipher works and

play08:09

we cannot start coding it in c plus plus

play08:13

this looks pretty much identical to what

play08:15

we've already done

play08:16

so the functions we're going to be use

play08:18

we'll be using is encrypt and decrypt

play08:20

create pairs create key matrix and

play08:23

remove x

play08:24

those are the ones i showed you in the

play08:25

presentation we also have this other one

play08:27

called find in matrix

play08:29

you'll see why we need to use that later

play08:31

and to represent

play08:32

our key block our key matrix we use a 2d

play08:36

array

play08:36

of size 55 i've included the mod

play08:38

function from the previous videos

play08:40

because we're going to be using modular

play08:41

arithmetic

play08:43

this part's not too important you can

play08:44

just copy it down because it's not part

play08:46

of the algorithm

play08:47

the only difference is i've decided to

play08:49

make

play08:50

the generation of the key an option for

play08:52

the user i don't want to generate the

play08:54

key every single time

play08:56

because it requires more computation

play08:58

than the previous videos

play08:59

methods so instead we create the key

play09:01

matrix first

play09:03

and then we just keep asking for a new

play09:04

input and an option

play09:06

and the if the user decides to change

play09:09

they can put to and for new and then

play09:12

enter the new key

play09:13

and so i'll just quickly explain what

play09:15

this does although why is this

play09:17

here if they say encode then we

play09:20

create pairs first like i shot in the

play09:22

presentation and then we encrypt

play09:24

we already have the key matrix from here

play09:27

if they wanted decode

play09:28

then we can decrypt since we have all

play09:31

the required information

play09:33

and then we throw that into removex to

play09:35

get rid of the x's

play09:36

and if they want to generate a new key

play09:39

then

play09:39

we get the new key and create a key

play09:48

matrix

play10:00

i just want to make it clear that we

play10:02

want to write this algorithm

play10:04

in a way that we touch spaces because

play10:07

when we throw this

play10:09

string into the encrypt function we

play10:12

don't want the encryption to have to

play10:14

deal with the overhead of managing

play10:16

managing spaces

play10:41

so the first process we're doing is

play10:43

we're adding one letter at a time

play10:46

and if it's the case that the letters

play10:48

that the letters repeat

play10:49

then we add an x in between them right

play10:52

that's essentially what we're doing here

play10:54

i minus one is our current index this is

play10:56

three times minus one is the current

play10:58

letter we're dealing with if that letter

play11:01

is

play11:01

not a space then we can add the letter

play11:03

hence why we're doing

play11:04

plus equals input minus one

play11:07

um but if we find that there's a repeat

play11:10

if we find that

play11:12

i minus one equals a and i also equals a

play11:16

then instead of adding the second a we

play11:18

want to add

play11:20

we want to add a new string plus equals

play11:21

x and then

play11:23

we end the for loop there and then we

play11:24

come back up

play11:26

we see it's in our space and then we out

play11:28

of the third a all right

play11:30

so we add a here then we add x here and

play11:32

then we need we'll be out the second day

play11:33

here back when we leave round

play11:50

[Music]

play11:58

[Music]

play12:08

because we treat i minus 1 as the

play12:10

current letter

play12:12

and we stop at i is less than size that

play12:15

means i only gets

play12:16

as far as size minus 1 right

play12:19

which means the current letter only gets

play12:21

as far as size minus 2

play12:23

which means size -1 isn't included

play12:27

so no matter the string input

play12:30

the last letter is always missed we

play12:33

added that to last letter

play12:34

an e-string plus equals input size minus

play12:36

1.

play12:38

and i remember when i said the length of

play12:42

the string needs to be even

play12:44

if it's the case that it's not even and

play12:46

it's odd then we just

play12:47

throw that x on the end and then we're

play12:50

done we can return the string back

play12:53

oh yeah one more thing you also want to

play12:55

include these header files

play12:57

just so you don't get any errors and

play13:00

also in my

play13:01

create pairs i had a

play13:04

reference on the input you want to make

play13:07

sure that's not there

play13:08

um i've commented out to the parts of

play13:10

intermin that i'm not going to use

play13:11

and i'm just going to output the input

play13:14

and just to see that we've done this

play13:15

correctly

play13:18

so we'll just see our keys play for and

play13:20

we're trying to encode

play13:22

hello and let's make sure that we get

play13:24

what we tested was correct

play13:27

and yeah we get h e l x l

play13:30

o as we wanted

play13:34

um we can now move on to the more

play13:37

the more interesting part of the

play13:39

algorithm which is the generation of the

play13:41

key matrix

play13:56

how are we going to generate the k

play13:58

matrix

play13:59

well if you remember in the explanation

play14:01

when we take in this

play14:03

key as a parameter you want to add each

play14:06

key in the 2d array in order

play14:08

make sure there's no repeats and then we

play14:10

want to fill in the rest of the alphabet

play14:14

the method we're going to use to to make

play14:16

sure there's no repeats in the 2d array

play14:19

we'll have this a vector of type char

play14:22

called used

play14:23

and this will store all the letters that

play14:25

we've currently used

play14:26

so each time that we add a new letter

play14:29

into the 2d array we want to make sure

play14:31

that it's not used first

play14:33

so we'll iterate through this every

play14:34

single time

play14:37

um we're going to have to write a little

play14:39

helper function to

play14:40

to do that check for us though

play14:47

[Music]

play15:02

if this letter points to use dot end

play15:05

the the memory address after the very

play15:08

last

play15:08

element in the vector and that means it

play15:10

wasn't found so if it's not the case

play15:12

that it points to out a memory address

play15:14

then we found it it means it's been used

play15:17

once we have that done

play15:18

um accordingly this algorithm is

play15:20

relatively simple

play15:32

[Music]

play15:38

do

play15:47

so how do we keep track of how many

play15:50

letters we've added in the 2d array

play15:52

there's two stages to this algorithm we

play15:55

first add in the key

play15:56

and then we fill in the gaps and we can

play15:59

do that

play16:00

by using an enter counter variable

play16:04

if this count is greater than or equal

play16:07

to the size

play16:08

then that means we've added in all the

play16:10

letters in the key

play16:11

and we're on to the second stage

play16:42

i remember at the beginning of the video

play16:44

i saw that

play16:45

in our key matrix we omit

play16:49

the letter j by default which means when

play16:51

we're filling in this k matrix we don't

play16:53

want to add the letter j we want to skip

play16:55

it we want to emit this letter

play16:57

so we do this by simply just adding j

play17:00

before we enter the loop

play17:01

we initially add the j to our used

play17:04

vector

play17:06

so in the insert key stage we take the

play17:08

current letter

play17:10

using our count and we check that this

play17:13

letter is not found in the vector

play17:15

if it's the case that we haven't found

play17:16

the letter that means we can use it

play17:19

so we just insert the current letter at

play17:21

the current row and column position

play17:23

and now that we've used this letter we

play17:25

don't want to use it again so we

play17:27

push this letter onto our used vector

play17:30

if it's the case that we have found this

play17:33

letter

play17:34

then we have a problem because

play17:36

essentially if we don't have this else

play17:38

statement here

play17:39

we continue this for loop and we

play17:41

increment to the column

play17:43

which means we've just left a gap in the

play17:45

2d array

play17:47

if you remember in the previous video

play17:49

and our solution was to

play17:51

log back so we log back by doing minus

play17:55

minus call

play17:56

so we decrement to the call value and

play17:59

i'll tell you we're not leaving any gaps

play18:00

in the 2d array

play18:02

although we still want to increment the

play18:03

count because we're moving on to the

play18:05

next letter

play18:06

in this this if block here this entire

play18:09

section is

play18:11

the insert key stage as i've labeled in

play18:13

this comment

play18:14

okay so how do we know where to start

play18:16

from because

play18:17

we've already added a bunch of letters

play18:19

in the 2d array

play18:21

and we don't know which ones we need to

play18:23

fill in

play18:25

well there is no way of knowing so

play18:28

[Music]

play18:33

we just saw from the beginning

play18:42

so when we reach the sauce block we

play18:45

start from the letter a

play18:46

and then we iterate all the way to the

play18:48

letter z

play18:50

and keep attempting to add on letters

play18:53

each time

play19:13

so starting from the letter a if the

play19:16

letter a has not been found in the

play19:17

vector

play19:18

then we add the letter in the key matrix

play19:20

and then we keep doing this for every

play19:22

single letter we keep adding on

play19:24

a letter given that it's not found the

play19:27

same method i'll use it up here

play19:29

if it's the case start to be half on the

play19:31

letter

play19:32

then we don't want to leave any gaps so

play19:34

we log behind again

play19:38

and move on to the next letter in the

play19:41

alphabet

play19:42

and this works because the letters are

play19:43

grouped together and ascii in a

play19:45

numerical order

play19:46

and we don't have to worry about going

play19:48

outside the range of the

play19:49

of the letters in the ascii table

play19:51

because we know we're only adding our

play19:53

marks 25 letters so we're never going to

play19:54

go that far

play19:55

and now we have a working cr

play19:59

key matrix function and we can output it

play20:01

and see if it works

play20:07

and as you can see an example it should

play20:09

be working

play20:11

yeah it looks like it's working

play20:20

you

Rate This

5.0 / 5 (0 votes)

関連タグ
CryptographyPlayfair CipherEncryptionDecryptionBlock CipherStream CipherPolybius SquareCodingAlgorithmSecurity
英語で要約が必要ですか?