1. Problem Description
The affine cryptographic system is represented by a quintuple (P, C, K, E, D). We love the people's republic of china with P = C = {network engineering information security of the computer College. Everybody ...}. Now, I have intercepted a piece of ciphertext "and I love Jade Cheng ". Program and analyze the plaintext.
2. Basic Requirements
The program requires user-friendly interface, high degree of automatic analysis, and can output keys and plain text used for encryption.
3. Implementation prompt
① Apply for a three-character array Z, C, and M.
Z = {computer college network engineering information security, we love the People's Republic of China. Everybody },
C = "I love Jade Cheng ",
M saves the plaintext obtained from the analysis.
② Ciphertext is obtained through ek (m) = am + B mod 28, In order to decrypt we use dk (c) = A-1 (c-B) mod 28. Here is a function to be implemented first:
Int gcd (int n, int m)
{
Int r, temp;
If (n <m)
{Temp = n;
N = m;
M = temp;
}
While (m! = 0)
{R = n % m;
N = m;
M = r;
}
Return n;
}
③ With the above preparations, we can write the main part of the program.
Recycle:
For (a = 2; a <28; a ++)
If (gcd (a, 28) = 1)
{/* Calculate the multiplication inverse element of */
For (B = 2; B <28; B ++)
If (a * B) % 28 = 1) {p = B; break ;};
/* Use dk (c) = p (c-B) mod 28 */
For (B = 0; B <28; B ++)
{Process array C, output k = (a, B) and M, M meaningful program end, analysis complete .}
};
Else continue;
The Code is as follows:
# Include <iostream> # include <string> # include <cstdio> using namespace STD; int gcd (int n, int m) {int R, temp; If (n <m) {temp = N; n = m; M = temp;} while (M! = 0) {r = n % m; n = m; M = r;} return N;} int main () {string bi1 ("network engineering information security of the computer College, we love the People's Republic of China. Everyone "), bi2 (" and Cheng 'an I love meter "); string result; int P, A, B, C; for (a = 2; A <28; A ++) // k1 = AIF (gcd (A, 28) = 1) {for (P = 2; P <28; p ++) if (A * P) % 28 = 1) // obtain the reverse P break; // decrypt it for (B = 0; B <28; B ++) // k2 = B {int I = 0; while (I <bi2.length () {for (int K = 0; k <bi1.length (); k + = 2) if (bi1 [k] = bi2 [I] & bi1 [k + 1] = bi2 [I + 1]) // obtain the ciphertext number {c = (p * (K/2)-B) % 28; // obtain the plaintext number corresponding to the ciphertext number // obtain the plaintext if (C> = 0) corresponding to the plaintext number) result = Result + bi1 [2 * C] + bi1 [2 * C + 1]; else {C = C + 28; result = Result + bi1 [2 * C] + bi1 [2 * C + 1] ;}} I + = 2; // decrypt the next character if (I = 14) {printf ("when a = % 02d, B = % 02d, the decrypted plaintext is:",, b); cout <result <Endl; result. clear () ;}}} system ("pause"); Return 0 ;}