In many textbooks, the definitions of line replacement passwords and column replacement passwords are not exactly the same, and even the core ideas are fundamentally different. Based on my own learning experience, I will briefly introduce a so-called "line replacement password" algorithm in some textbooks. We will discuss and discuss it together.
Using this encryption method, the plaintext is written in a matrix by line, while the plaintext is generated by reading in a predetermined order by column. For example, if the matrix contains four columns and five rows, the plaintext "encryption algorithm" (after spaces are omitted) can be written to the matrix as follows:
2 |
3 |
1 |
4 |
E |
N |
C |
R |
Y |
P |
T |
I |
O |
N |
A |
L |
G |
O |
R |
I |
T |
H |
M |
S |
Read columns in a certain order to generate ciphertext. For this example, if the read order is incremental, the plaintext is "ctarm eyogt npnoh rilis" (adding spaces for observation ). The encryption key is the number of columns and the order of reading columns. If there are many columns, it may be difficult to remember, so it can be expressed as a keyword. The length of this keyword is equal to the number of columns, and its alphabetic order determines the order of reading columns. For example, the keyword "general" has seven letters, meaning that the matrix has seven columns. Because "a" is the lowest in the "general", the number 1 is placed in the 6th column; from left to right, the first "e" is followed, so the number 2 is placed in the 2nd column; the second "e" is to place the number 3 in the 4th column. The final order is as follows: g e n e r a l 4 2 6 3 7 1 5 _______________________________________________________________________ This scheme is to write the message in a rectangle, row by row, and read the message off, column by column, but permute the order of the columns. the order of the columns then becomes the key to the algorithm. for example, Key: 4 3 1 2 5 6 7 Plaintext: a t a c k p o s t p o n E d u n t I l t w o a m x y z Ciphertext: ttna aptm tsuo aodw coix knly petz (again, spaces are only for observation) Thus, in this example, the key is 4312567.To encrypt, start with the column that is labeled 1, in this case column 3. write down all the letters in that column. **************************************** **************************************** * ***** the preceding is an encryption, you can also use the ciphertext as the plaintext of a new round of encryption, and replace the encrypted line again. Reference: Cryptography and Network Security Principles and Practice, privacy Edition ---- William Stallings Classical And Contemporary Cryptology ---- row replacement password on Richard Spillman/Z26 # include <stdio. h> # include <string. h> # include <stdlib. h> # include <time. h> int length; // The plaintext length char plain [100000]; char cipher [100000]; char out [100000]; int l; // The key length int key [100]; // key int done_key [100] = {0}; // indicates whether the key has been converted to an int num_key [100] = {0}; // Replace the key with the number int num [100]; // a temporary matrix that stores the ascending subscript void gen (); // key generation algorithm void encryption (); void decryption (); int length_str (int a []); int main () {int I; FILE * fp; fp = fopen ("plain.txt", "r"); fscanf (fp, "% s", plain); // read the plaintext fclose (fp) from the file ); length = strlen (plain); gen (); encryption (); printf ("the above is true"); decryption (); for (I = 0; I <length; I ++) {printf ("% c", out [I]);} return 0;} void gen () // key generation algorithm {int I; pri Ntf ("Enter the length of the random key you want to generate:"); scanf ("% d", & l); srand (time (0 )); for (I = 0; I <l; I ++) {key [I] = rand () % 26;} printf ("\ n randomly generated key string: "); for (I = 0; I <l; I ++) {printf (" % c ", key [I] + 97 );} printf ("\ n");} char a [50000] [100]; // temporary matrix, to make it easier to convert the ciphertext to the array // This array must be set to global. If it is declared in the function, an error will occur when applying for memory !!! Void encryption () {// conversion: sorts the key string into numbers int k = 1; int I, j, m; int small; // The minimum Letter Number (the unnumbered, first, and smallest letters) for (I = 0; I <l; I ++) // Replace the letter with the number {m = 0; while (done_key [m] = 1) m ++; small = m; for (j = 0; j <l; j ++) {if (done_key [j] = 0) // if not converted, continue if (key [j] <key [small]) small = j;} num_key [small] = k; done_key [small] = 1; k ++;} // printf ("The order of the key is: \ n"); for (I = 0; I <l; I ++) {printf ("% d", num_key [I]);} Printf ("\ n"); for (I = 0; I <length; I ++) // ignore non-letter characters, convert uppercase to lowercase {if (plain [I]> = 65 & plain [I] <= 90) {plain [I] + = 32 ;}} while (length % l) {strcat (plain, "p"); // when division is not allowed, fill in the invalid character p length ++;} // generate the ciphertext matrix k = 0; for (I = 0; I <length/l; I ++) // row for (j = 0; j <l; j ++) // column {a [I] [j] = plain [k ++];} k = 0; for (I = 0; I <l; I ++) // column {for (j = 0; j <l; j ++) if (num_key [j] = I + 1) num [I] = j ;} k = 0; for (m = 0; m <l; m ++) // column for (j = 0; j <length/l; j ++) // line cipher [K ++] = a [j] [num [m];} char B [50000] [100]; void decryption () // decryption function {int I, j, k; k = 0; for (I = 0; I <l; I ++) // num [I] As the column for (j = 0; j <length/l; j ++) // Row B [j] [num [I] = cipher [k ++]; k = 0; for (I = 0; I <length/l; I ++) // row for (j = 0; j <l; j ++) // column out [k ++] = B [I] [j];} // The folder contains the “plain.txt file, which must contain all English letters and can contain uppercase and lowercase letters, however, other characters, such as spaces, line breaks, and numbers, cannot appear.