用數組實現轉輪機加解密功能

用數組實現轉輪機加解密算法功能

算法原理、算法轉輪機密文明文初始化的形式即參考下圖

轉輪密碼機原理如圖3-1所示,它是一個三轉輪密碼機模型,3個帶有數字的矩形框代表3個轉輪,從左到右分別稱爲慢輪子、中輪子和快輪子。轉輪內部相當於一個單表代換。當按下某-鍵時,電信號從慢輪子的輸入引腳進人,經過內部連線流經每個轉輪,最後從快輪子的輸出引腳輸出密文。如在圖3-1(a)中.如果按下字母鍵A.則一個電信號被加到慢輪子的輸入引腳24並通過內部連線連接到慢輪子的輸出引腳24,經過中輪子的輸人引腳24和輸出引腳24,連接到快輪子的輸人引腳18,最後從快輪子的輸出引腳18輸出密文字母B.(這個是貼的教科書上的內容哈)
轉輪密碼機原理如圖3-1所示,它是一個三轉輪密碼機模型,3個帶有數字的矩形框代表3個轉輪,從左到右分別稱爲慢輪子、中輪子和快輪子。轉輪內部相當於一個單表代換。當按下某-鍵時,電信號從慢輪子的輸入引腳進人,經過內部連線流經每個轉輪,最後從快輪子的輸出引腳輸出密文。如在圖3-1(a)中.如果按下字母鍵A.則一個電信號被加到慢輪子的輸入引腳24並通過內部連線連接到慢輪子的輸出引腳24,經過中輪子的輸人引腳24和輸出引腳24,連接到快輪子的輸人引腳18,最後從快輪子的輸出引腳18輸出密文字母B.

部分核心功能介紹

1. int searchindex(int wheel[26],int figure)、int searchindex(char wheel[27], char figure)
  這是兩個重載函數,第一個實現輸出查找的數字的下標。在轉輪機實現了輪子之間的連線。第二個函數實現了輸出查找的字符的下標。在轉輪機中實現了密文與數字和明文與數字之間的轉化。
  1. void roatewheel(int wheel[26])
    該函數實現了轉輪機輪子的轉動功能。
  2. void OutputCipertext(string plaintext)
    該函數實現明文轉密文的功能。
  3. void OutputPlaintext(string cipertext)
    該函數實現密文轉明文的功能。

下面是就直接貼代碼了哈!!!(註釋寫的比較詳細,這邊就不分函數細講了)

#include<iostream>
#include<cstring>
using namespace std;
#define ERROR -1
int flag1 = 0, flag2 = 0;                                            //轉輪轉動標誌位
int slowL[26] = {24,25,26,1,2,3,4,5,6,7
                 ,8,9,10,11,12,13,14,15,16
                 ,17,18,19,20,21,22,23};
int slowR[26] = { 21,3,15,1,19,10,14,26,20,8
                ,16,7,22,4,11,5,17,9,12,23,18
                ,2,25,6,24,13 };                                     //慢輪子
int midL[26] = {26,1,2,3,4,5,6,7,8,9,10,11
               ,12,13,14,15,16,17,18,19
               ,20,21,22,23,24,25};
int midR[26] = {20,1,6,4,15,3,14,12,23,5,16
               ,2,22,19,11,18,25,24,13,7,10
               ,8,21,9,26,17 };                                      //中輪子
int fastL[26] = {1,2,3,4,5,6,7,8,9,10,11
                ,12,13,14,15,16,17,18,19
                ,20,21,22,23,24,25,26};
int fastR[26] = {8,18,26,17,20,22,10,3,13,11,
                4,23,5,24,9,12,25,16,19,6,15,
                21,2,7,1,14};                                        //快輪子
char input[27] = {'A','B','C','D','E','F','G','H','I','J','K'        //將帶轉化明文轉化爲數字的參照表        
                   ,'L','M','N','O','P','Q','R','S','T','U','V'
                   ,'W','X','Y','Z' };
char output[27] = { 'A','B','C','D','E','F','G','H','I','J','K'      //將待輸出密文下標轉化成字符的參照表
                   ,'L','M','N','O','P','Q','R','S','T','U','V'
                   ,'W','X','Y','Z' };                                               
void roatewheel(int wheel[26]) {                              //實現輪子轉動,每次向下轉動一格
    int temp = wheel[25];
    for (int i = 24; i >=0; i--) {
        wheel[i + 1] = wheel[i];
    }
    wheel[0] = temp;
    return;
}
int searchindex(int wheel[26],int figure) {                              //查找整型類型的輪子的下標,並返回下標
    for (int i = 0; i < 26; i++) {
        if (wheel[i] == figure)
            return i;
    }
    printf("查詢失敗!");
    return ERROR;
}
int searchindex(char wheel[27], char figure) {                       //查找字符串型的輪子的小標,並返回下標                 
    for (int i = 0; i < 26; i++) {
        if (wheel[i] == figure)
            return i;
    }
    printf("查詢失敗!");
    return ERROR;
}
void OutputCipertext(string plaintext) {                              //輸出密文
    cout << "輸出明文爲:" << endl;
    int len = plaintext.length();
    int ind1, ind2, ind3, ind4;                                       //定義四個下標量
    for (int i = 0; i < len; i++) {
        ind1 = searchindex(input, plaintext[i]);                      //明文字符轉化爲對應數字,並返回數字下標
        ind2 = searchindex(slowR, slowL[ind1]);                       //進入中輪子  
        ind3 = searchindex(midR, midL[ind2]);                         //進入快輪子
        ind4 = searchindex(fastR, fastL[ind3]);                       //返回密文字符對應的下標
        cout << output[ind4];                                         //輸出字符對應的密文
        flag1++;
        roatewheel(fastL);                                            //每輸出一位,快輪子轉動一次
        roatewheel(fastR);
        if (flag1 % 26 == 0 && flag1 != 0) {                          //快輪子每轉動26次,中輪子轉動一次
            roatewheel(midL);
            roatewheel(midR);
            flag2++;
        }
        if (flag2 % 26 == 0 && flag2 != 0) {                          //中輪子每轉動26次,慢輪子轉動一次
            roatewheel(slowL);
            roatewheel(slowR);
        }
    }
    flag1 = flag2 = 0;                                                //解密結束,標誌位初始化爲0
    return;
}
void OutputPlaintext(string cipertext) {                              //解密密文
    cout << "輸出密文爲:" << endl;
    int len = cipertext.length();
    int ind1, ind2, ind3, ind4;
    for (int i = 0; i < len; i++) {
        ind1 = searchindex(output, cipertext[i]);                     
        ind2 = searchindex(fastL, fastR[ind1]);
        ind3 = searchindex(midL, midR[ind2]);
        ind4 = searchindex(slowL, slowR[ind3]);
        cout << input[ind4];
        flag1++;
        roatewheel(fastL);
        roatewheel(fastR);
        if (flag1 % 26 == 0 && flag1 != 0) {
            roatewheel(midL);
            roatewheel(midR);
            flag2++;
        }
        if (flag2 % 26 == 0 && flag2 != 0) {
            roatewheel(slowL);
            roatewheel(slowR);
        }
   
    }
    flag1 = flag2 = 0;
    return;
}
int main() {
    int choice;
    string plaintext;
    string cipertext;
    cout << "輸入0,加密明文" << endl;
    cout << "輸入1,解密密文" << endl;
    while (!0) {
        cin >> choice;
        switch (choice) {
        case 1:
            cout << "請輸入想要解密的密文" << endl;
            cin >> cipertext;
            OutputPlaintext(cipertext);
            break;
        case 0:
            cout << "請輸入想要加密的明文:" << endl;
            cin >> plaintext;
            OutputCipertext(plaintext);
            break;
        default:
            cout << "輸入錯誤!請輸入0或1!" << endl;
        }
    }
	return 0;
}

函數運行結果分享

隨機輸入一組明文,進行加密
在這裏插入圖片描述
將加密結果複製,進行解密
在這裏插入圖片描述
比較得到,加解密結果一致,函數功能實現成功!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章