Programming and implementation of the ngerma encryptor (C ++)
I believe that after reading "Imitation games", you will be curious about the encryption method of World War II. I am no exception, So I compiled a program to implement the ngemma encryptor, the biggest feature of this machine is self-inverse. As long as the initial settings are consistent, the machine is self-inverse. For example, input A and encrypted B are in the same settings, input B will certainly Output.
The following is a simplified version with no plug-in Board (if it is added, it is very easy to replace the specified letter. I will not add it here for simplicity)
# Include
# Include
Using namespace std; string Enigma (string input) {int code; int n [6] = {24, 2, 5, 4, 10, 23}; // defines 6 rotor int nsize = 6; string output; for (int I = 0; I <input. size (); I ++) {if (input [I] = '') {output + =''; continue ;} code = input [I]-'A'; for (int j = 0; j <nsize; j ++) {code = (code + n [j]) % 26;} if (code % 2 = 0) code ++; else code --; // if an even number + 1, an odd number-1, the reflectors can be paired with each other. For (int j = nsize-1; j> = 0; j --) {code = code-n [j]; if (code <0) code = 26 + code ;} n [0] ++; for (int j = 0; j <nsize-1; j ++) {if (n [j]> = 26) {n [j + 1] + +; n [j] = 0 ;}} n [nsize-1] = n [nsize-1] % 26; output + = code + 'a';} return output;} int main () {string text = hey helloworld; string miwen = Enigma (text); cout <ciphertext: <miwen <endl; cout <plaintext: <Enigma (miwen) <endl; return 0 ;}