Implement column permutation encryption through programming, gain a deeper understanding of its principles, and master the column permutation algorithm.
Steps:
(1) enter the key
(2) sort out the letters in the key
(3) Input plaintext
(4) The plaintext order is disrupted by the letter order in the key.
(5) Output ciphertext
(6) Decryption
(7) Output plaintext
# Include <stdio. h> # include <string. h> int main () {int I, j, L, K, M, num [30]; char key [30], plantext [100], plantext2 [100], cipher [100]; printf ("Please input the key \ n"); scanf ("% s", key); L = strlen (key); for (I = 0; I <L; I ++) {num [I] = 0; For (j = 0; j <L; j ++) {If (Key [J] <= Key [I]) {num [I] = num [I] + 1 ;} if (Key [J] = Key [I] & J> I) num [I] = num [I]-1 ;}} printf ("Please input the plantext \ n"); scanf ("% s", plantext); k = strlen (plantext); I = 0; // complete xfor (M = K % L; m <L; m ++) {plantext [K + I] = 'X'; I ++ ;} if (K % L = 0) // calculate the number of rows M = K/L; else M = K/L + 1; printf ("encrypted: \ n "); for (I = 0; I <m; I ++) {for (j = 0; j <L; j ++) {cipher [I * L + J] = plantext [I * L + num [J]-1] ;}}for (I = 0; I <L; I ++) // output ciphertext {for (j = 0; j <m; j ++) printf ("% C", cipher [J * L + I]);} printf ("\ n"); printf ("decryption: \ n"); // decrypt the ciphertext for (I = 0; I <m; I ++) {for (j = 0; j <L; j ++) {plantext2 [I * L + num [J]-1] = cipher [I * L + J];} plantext2 [(I-1) * l + J] = '\ 0'; printf ("% s \ n", plantext2); Return 0 ;}
You can only understand the principle of column replacement password, that is, hands-on exercises, but not practical applications.
The program runs as follows: