編程實現恩格瑪加密機(C++)

來源:互聯網
上載者:User

標籤:加密   c++   編程   恩格瑪   模仿遊戲   

相信各位看了《模仿遊戲》之後,都會對這個二戰的加密方法感到很好奇吧,我也不例外,因此編了個程式實現了恩格瑪加密機,這機器最大的特點就是有著自反性,只要初始設定一致的時候,那麼它就是自反的,比如輸入A,加密後B,在一樣的設定下,輸入B一定會輸出A。
詳細的介紹可以看這裡:
http://www.zhihu.com/question/28397034

下面我實現的是簡化版的,沒有插線板(如果加上去也是很簡單的,只需要替換指定的字母就可以了,這裡為了簡潔就不添加了)

#include <string>#include <iostream>using namespace std;string Enigma(string input){    int code;    int n[6] = {24,2,5,4,10,23};  //定義6個轉子    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--;  //反射器如果偶數+1,奇數-1,反射器只要能實現字母兩兩配對就可以了。        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 hey  helloworld";string miwen=Enigma(text);cout <<"密文:"<< miwen<< endl;cout <<"明文:"<< Enigma(miwen) << endl;return 0;}

編程實現恩格瑪加密機(C++)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.