Cryptography Big Homework
Customize an encryption algorithm and its programming implementation
1. Encryption principle
plaintext space | plaintext length | Ciphertext Space | Ciphertext Length |
---|---|---|---|
a-z | Greater than 1 | a-z | Hill cipher encryption matrix Integer multiples of order |
Encryption order | Decryption order |
---|---|
plaintext | Ciphertext |
Virginia Cipher | Hill Cipher |
Affine Cipher | Affine Cipher |
Hill Cipher | Virginia Cipher |
ciphertext | plaintext |
1.1. Virginia code
Encryption function:
e
K
(
x
1
,
x
2
,
.
.
.
x
m
)
=
(
x
1
+
k
1
,
x
2
+
k
2
,
.
.
.
x
m
+
k
m
)
e_K(x_1, x_2, … x_m) = (x_1 + k_1, x_2 + k_2, … x_m + k_m)
eK?(x1?,x2?,…xm?)=(x1? + k1?,x2? + k2?,…xm? + km?)
Decryption function:
d
K
(
the y
1
,
the y
2
,
.
.
.
the y
m
)
=
(
the y
1
?
k
1
,
the y
2
?
k
2
,
.
.
.
the y
m
?
k
m
)
d_K(y_1, y_2, … y_m) = (y_1 – k_1, y_2 – k_2, … y_m – k_m)
dK?(y1?,y2?,…ym?)=(y1k1?,y2k2?,…ymkm?)
When encrypting, data is grouped. The length of each group is
m
m
m. If the last grouping is insufficient
$$
m
bit only
bit only
bit only
no
bit
bit
bit
(n , then only encrypt the former , then only encrypt the former , then only encrypt the former n$$ bits. Encryption function: e ( x ) = ( a x + b ) m o d m e(x) = (ax + b) \mod m e(x)=(ax + b)modm1.2. Affine cipher
Decryption function:
d
(
the y
)
=
a
?
1
(
the y
?
b
)
m
o
d
m
d(y) = a^{-1} (y – b) \mod m
d(y)=a?1(y?b)modm
in
a
a
a and
$$
m
reciprocal, if
m
=
26
,
but
is relatively prime, if m = 26, then
Reciprocal prime, if m=26, then
a \in {1,3,5,7,9,11,15,17,19,21,23,25}$$
1.3. Hill password
Encryption function:
e
K
(
x
)
=
x
K
e_K(x) = xK
eK?(x)=xK
Decryption function:
d
k
(
the y
)
=
the y
K
?
1
dk(y) = yK^{-1}
dk(y)=yK?1
where the key K is a reversible square matrix
1.3.1. Encrypted matrix inversion in finite fields
set matrix
A
A
A is reversible,
$$
A^-
is its inverse matrix,
is its inverse matrix,
is its inverse matrix,
D.
is its determinant,
is its determinant,
is its determinant,
A^*$$ is its adjoint matrix, then we have
A
?
=
A
?
D.
=
A
?
D.
?
A^- = \frac{A*}{D} = A^* D^-
A?=DA=A?D?
in
D.
?
D^-
D?
for
D.
D.
D in die
$$
m
The inverse element below, the above operations are all in the modulo
The inverse element below, the above operations are all in the modulo
The inverse element below, the above operations are all in the modulo
Calculated under m$$.
1.3.2. Encryption
If there is the following plaintext:
Plain text serial number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
corresponds The value of | 12 | 0 | 1 | 6 | 18 | 25 | 6 | 20 | 25 | 25 |
1.3.2.1. Process the plaintext first, then add the terminator
According to statistics, the word with the lowest frequency is z = 0.08, and z corresponds to 25, that is, the frequency of z in plaintext is relatively low, and 25 will be used as the terminator of plaintext. Therefore, if you encounter 25 during encryption, you need to escape it and replace it with two 25s.
Analyze the above plaintext, replace all 25 with two 25, and get the following plaintext:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25< /strong> |
Add a 25 at the end of the plaintext as a terminator to get the following ciphertext:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25 | 25 |
1.3.2.2. Padding groups
The plaintext is grouped, and the last group is insufficient to supplement 0 (it can also be filled with random values, but it is not easy to analyze, so here we simply fill in the same characters). If the block length is 8, the following ciphertext is obtained:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25 | 25 | 0 | 0 |
1.3.2.3. Encryption
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25 | 25 | 0 | 0 |
Matrix encryption for each packet
1.3.2.4. At this point, the encryption is over, just convert the numbers into corresponding letters
1.3.3. Decrypt
If the last group is not a complete group after the ciphertext is grouped, it means that the ciphertext and the key do not match and cannot be decrypted. If the packet is normal, the reverse process of encryption is used.
1.3.3.1. Decrypt
Decrypt each packet using the matrix. If the following plaintext is obtained after decryption:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25 | 25 | 0 | 0 |
1.3.3.2. Remove padding data
From the front to the back, find the subscript of the terminal 25, and then delete itself and all subsequent data to get the following plaintext:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 25 | 6 | 20 | 25 | 25 | 25 | 25 |
1.3.3.3. Combine two consecutive 25s into one to get the following plaintext:
Number | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
value | 12 | 0 | 1 | 6 | 18 | 25 | 6 | 20 | 25 | 25 |
1.3.3.4. At this point, the decryption is over, just put the serial number
2. Programmatic implementation
A winform program developed with C#.
2.1. Interface effects
2.1.1 Key Configuration
- After the app is opened, the user is required to configure the key. If you don’t want to enter it yourself, you can click the “Generate Random Key” button.
- generate random key
-
After generating a random key, you can click the “Export Key” button, and the program will save the key as a “JSON” file, and then you can send the key to others.
-
A pop-up window prompts that the save is successful
-
You can open it and have a look, the format is as follows
{<!-- --> "InvalidIntValue": 2147483647, "VigenereKey": [ 1, 15, 22, 24, 22, 9 ], "AffineKeyA": 23, "AffineKeyB": 6, "HillMatrix": [ [ 11, 15, 7, 10, 9, 20, 24, 10 ], [ 15, 19, 16, 1, 16, 7, 16, 21 ], [ 22, 6, 10, 24, 1, 18, 6, 22 ], [ 6, 13, 1, 22, 13, 12, 5, 16 ], [ 22, 20, 18, 24, 14, 5, 20, 17 ], [ 2, 15, 2, 17, 2, 17, 18, 7 ], [ 18, 14, 13, 8, 21, 1, 22, 17 ], [ 10, 9, 5, 20, 20, 2, 21, 3 ] ], "AllKeysReady": true }
-
-
If you need to decrypt other people’s ciphertext, you can click the “Import Key” button to import the other party’s key.
2.1.2 Encrypted and decrypted text
- If you check “Copy result to clipboard”, when the encryption operation is performed, the ciphertext will be copied directly to the clipboard, so that it can be pasted directly to others.
- If “Get text from clipboard” is checked, when the decryption operation is performed, the text decryption will be read from the clipboard
- Start using the encryption and decryption function, enter the letters to be encrypted, and click the “Encrypt” button
- Copy the ciphertext to “Data Source”, click the “Decrypt” button to get the plaintext. (If “Get Text from Clipboard” is checked, you can directly click the decrypt button, and the program will automatically fill the ciphertext in the clipboard into “Data Source” and perform the decryption operation)
2.2. Some source code
2.2.1 Virginia code
// Encryption public List<char> Decrypt(List<char> cipher) { var result = new List<char>(); int groupLength = vigenereKey. Length; for (int i = 0; i < cipher. Count; i ++ ) { var _ = cipher[i] - vigenereKey[i % groupLength]; result.Add((char)((_ % m + m) % m)); } return result; } // decrypt public List<char> Encrypt(List<char> plain) { var result = new List<char>(); int groupLength = vigenereKey. Length; for (int i = 0; i < plain. Count; i ++ ) { var _ = plain[i] + vigenereKey[i % groupLength]; result.Add((char)(_ % m)); } return result; }
2.2.2 Affine cipher
// decryption public List<char> Decrypt(List<char> cipher) { int ap = AAP[a]; // the inverse of a for (int i = 0; i < cipher. Count; i ++ ) { cipher[i] = (char)(((ap * (cipher[i] - b)) % 26 + 26) % 26); } return cipher; } // encryption public List<char> Encrypt(List<char> plain) { for (int i = 0; i < plain. Count; i ++ ) { plain[i] = (char)(((a * plain[i] + b) % 26 + 26) % 26); } return plain; }
2.2.3 Hill password
// decryption public List<char> Decrypt(List<char> cipher) { // TODO Delete //PrintList(cipher, "[before decryption]"); if (cipher. Count % groupLength != 0) { throw new FormatException("The format of the ciphertext is wrong, please check if there is missing data"); } // decrypt int groupNums = cipher.Count / groupLength; // total number of groups List<char> result = new List<char>(); // result for (int i = 0; i < groupNums; i ++ ) { // Decrypt the block using the encryption matrix and add to the result result. AddRange( MatrixIntGF26.MultiplyMod26( cipher.GetRange(i * groupLength, groupLength).ToArray(), DKEY ) ); } // Remove redundancy, remove the data after the terminator var index = result.LastIndexOf(Transfer_Char); if (index == -1) { throw new Exception("The data is wrong, or the data and the key of Hill Cipher do not match"); } result.RemoveRange(index, result.Count - index); // Merge two consecutive terminal symbols for (int i = 1; i < result. Count; i ++ ) { if (result[i] == Transfer_Char) { if (result[i - 1] == Transfer_Char) { result. RemoveAt(i); } } } return result; }
// Encryption public List<char> Encrypt(List<char> plain) { // copy plain var copy = new List<char>(); for (int i = 0; i < plain. Count; i ++ ) { copy. Add(plain[i]); } // Escape the terminal, that is, replace one terminal with two consecutive terminals for (int i = 0; i < copy.Count; i ++ ) { if (copy[i] == Transfer_Char) { copy. Insert(i, Transfer_Char); i + + ; } } // Add a terminator at the end of the data copy.Add(Transfer_Char); // Check the total length of the data, if the last group is not enough, you need to fill the data int appendLength = copy.Count % groupLength;//filling number if (appendLength != 0) { appendLength = groupLength - appendLength; } for (int i = 0; i < appendLength; i ++ ) { copy.Add(Append_Char); } // total number of groups int groupNums = copy.Count / groupLength + ((copy.Count % groupLength) == 0 ? 0 : 1); List<char> result = new List<char>(); // result for (int i = 0; i < groupNums; i ++ ) { // Encrypt the block using the encryption matrix and add it to the result result. AddRange( MatrixIntGF26.MultiplyMod26( copy. GetRange( i * groupLength, groupLength).ToArray(), EKEY ) ); } return result; }
2.2.4 Hill password auxiliary class
It is used to provide matrix multiplication and matrix inversion operations under mod26. There are more codes, so I won’t show them
Resources
Size: 221KB
Resource download: https://download.csdn.net/download/s1t16/87484796
Note: If the current article or code violates your rights, please private message the author to delete it!