00:01
In this question, we will learn how to write a c+ + program that encrypts text using playfair cypher.
00:08
First, we will prepare the key.
00:16
Then we will prepare cypher table.
00:24
Then we will calculate the row and column of a character in cypher table.
00:46
Then after using this table, we will learn to encrypt the plaintext.
00:54
Let's see the code for it.
01:07
So, first let's import important libraries.
01:35
Let's create a global variable.
02:04
Let's create a constant of integer type 10.
02:14
We will create a function for preparing the key.
02:26
Inside this, we will pass key as a parameter.
02:36
And key is of data type string.
02:47
So, we have created this empty string which we will update later.
03:07
First, we will remove the duplicates.
03:09
For the character c in key, we will run a for loop and check if isalpha c and end.
03:47
Negation of present to upper c minus upper case alphabet a.
04:06
Then we will present to upper c minus a equal to then prepared key plus equal to to upper.
04:41
So, using this code, we have removed the duplicates from the key that we passed in prepared key function.
04:56
This is string data type.
05:07
Now, we will create another for loop for replacing j with i.
05:19
For int i equal to zero, i is less than prepared key dot length i plus j.
05:41
In this case, we are running a for loop for the total length of the prepared key we just prepared using this code.
05:55
So, what we have done is, we have checked if alpha of character c in the key and not of to topper character c and capital a.
06:14
Means we have this present 26 false.
06:22
Means we have created an array of data type bool whose length is 26 elements like a, b, c, d, e, f, g, h.
06:33
They all are 26 characters.
06:35
So, we have created an array.
06:37
So, initial value of all the elements is false.
06:43
We have checked if these both conditions will be true, then program will run this line.
06:52
First, it is checking if there is any character inside a key, then it is checking if this character is not in this.
07:12
If it is not in this, then make this character element lies true.
07:22
So what we have done is, if let's say this character is a, this character is a.
07:29
So, a minus a, we will got zero.
07:32
So means this array's index which position is zero, that is first element.
07:38
If it's not there, this negation, if it's not there, then we'll put it there by simply making that element from false to true.
07:52
Okay, let's say it's already there.
07:55
Means the element's position will be not set to false, it will set to true.
08:01
Now if c comes, it will check at that position, it will get true and true becomes false due to this negation.
08:13
And if this is true, and this is false, the end -in -operation will return false.
08:17
And the program will come out of this if statement.
08:24
So this is how we are checking the duplicacy.
08:27
Okay, if they are not duplicates, then we will set that element to be true and move our character element to our prepared key string by using plus operator.
08:44
Understood? so after detecting the duplicacy in these lines, we are now replacing all the j with capital i, all the capital j with capital i.
09:00
So we are first scanning throughout the length of this prepared key.
09:05
Okay.
09:06
Now if prepared is equal to equal to j, then prepared key is equal to, so if it is j, we will put i in place of it, then we will come out of for loop and return prepared.
09:52
So after completing all the operations, we will return this r new prepared key.
10:02
So this is, so this r function is complete now.
10:07
Now let's jump to creating a next function, which will create the cipher table.
10:12
So, so first what we'll do is this is outside this for loop and this is a function, then we have to place it here like this.
11:04
Okay.
11:12
Oh, i forgot one more curly braces.
11:15
It is here.
11:18
So this part is under this if and this whole part is under this for loop and this part is under this if and this whole part is under this for loop.
11:29
This is a return prepared key and this is our whole function.
11:37
Okay.
11:41
Now void generate table.
11:50
In this we'll pass a key of type string, then character from table...