In a single table replacement password, the key is made up of letters and spaces such as Shana
Before there is a key effect, the permutation table is as follows
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
Under the action of the key, the permutation table will be changed, as follows
Fills the key in the permutation table, ignores the duplicate characters if it is encountered, and then fills in the order of the original table, ignoring the repeating characters, as shown in the following table
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
S |
H |
A |
N |
B |
C |
D |
E |
F |
G |
I |
J |
K |
L |
M |
O |
P |
Q |
R |
T |
U |
V |
W |
X |
Y |
Z |
First, Shan is filled in the table, because A already appears earlier, so ignore it, and then fill in sequentially the alphabet minus s H A n four characters
The specific C language code implementation is as follows, this code personal feeling write not concise, later have the opportunity to revise
The idea is to read the key and fill in the empty permutation table, and then fill the list into the permutation table to form the cipher table.
#include <stdio.h> #include <stdlib.h> int main () {//permutation table char list[27] = {0}; Alphabetic table Char Alpha[26] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', '
T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};
Key char key[101] = {0};
PlainText char message[101] = {0};
Ciphertext char ciphertext[101] = {0};
int Num = 0;
Get the key printf ("Please enter a key (less than 100 characters):");
Gets (Key); Construct permutation table for (int i = 0; Key[i];
++i) {if (key[i] = = ") continue;
for (int j = 0; J < ++j) {if (alpha[j] = = Key[i]) {alpha[j] = 0;
list[num++] = Key[i]; }} for (int i = 0; i < ++i) {if (!
Alpha[i]) continue;
list[num++] = Alpha[i];
//Get plaintext printf ("Please enter clear text (within 100 characters):");
Gets (message); Encrypt for (int i = 0; Message[i];
++i) {if (message[i] = = ') ciphertext[i] = ';
else ciphertext[i] = list[message[i]-' a ';
//Print ciphertext printf ("Encrypted ciphertext:%s\n", ciphertext);
System ("PAUSE");
return 0; }