Programming and implementation of the ngerma encryptor (C ++)

Source: Internet
Author: User

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 ;}
   
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.