Computer >> 컴퓨터 >  >> 프로그래밍 >> C++

비제네르 암호(Vigenère Cipher) 구현을 위한 C++ 프로그램


비제네르 암호(Vigenère Cipher)는 알파벳 텍스트를 암호화하는 다중 치환(polyalphabetic substitution) 방식의 암호 기법입니다. 이 방식에서는 A부터 Z까지의 알파벳을 26개의 행으로 배열한 비제네르 암호표(Vigenère Cipher Table)를 사용하여 암호화와 복호화를 수행합니다.

비제네르 암호(Vigenère Cipher) 구현을 위한 C++ 프로그램

암호화(Encryption)

키(Key): WELCOME

평문(Message): Thisistutorialspoint

먼저 주어진 키를 반복하여 그 길이가 원래 메시지의 길이와 같아질 때까지 확장합니다.

암호화할 때는 메시지의 첫 글자와 키의 첫 글자, 즉 T와 W를 가져옵니다. 그런 다음 비제네르 암호표에서 T행과 W열이 교차하는 지점의 알파벳, 즉 P를 찾습니다.

메시지 텍스트의 나머지 모든 알파벳에 대해 동일한 과정을 반복합니다.

최종적으로 암호화된 메시지는 다음과 같습니다.

암호문(Encrypted Message): PLTUWEXQXZTWMPOTZKBF

암호문은 아래 수식으로 생성할 수 있습니다.

Ei = (Pi + Ki) mod 26

여기서 P는 평문(plain text)이고, K는 키(key)입니다.

복호화(Decryption)

키(Key): WELCOME

암호문(Encrypted Message): PLTUWEXQXZTWMPOTZKBF

생성된 키와 암호문의 첫 번째 알파벳, 즉 P와 W를 가져옵니다. 비제네르 암호표에서 W열에서 P를 찾으면, 해당 행에 대응하는 알파벳이 원래 메시지의 첫 글자인 T가 됩니다.

암호문의 모든 알파벳에 대해 이 과정을 반복하면 됩니다.

원본 메시지(Original Message): Thisistutorialspoint

이 과정은 다음 수식으로 대수적으로 표현할 수 있습니다.

Pi = (Ei – Ki + 26) mod 26

다음은 비제네르 암호를 구현한 C++ 프로그램입니다.

알고리즘(Algorithms)

Begin
    Function encryption(string t)
    for i = 0, j = 0 to t.length() - 1
        char c = t[i]
        if (c >= 'a' and c <= 'z')
            c = c + 'A' - 'a'
        else if (c < 'A' or c > 'Z')
            continue
        output = output + (c + k[j] ) % 26 + 'A'
        j = (j + 1) % k.length()
    return output
End
Begin
    Function decryption(string t)
    for i = 0, j = 0 to t.length() - 1
        char c = t[i]
        if (c >= 'a' and c <= 'z')
            c = c + 'A' - 'a'
        else if (c < 'A' or c > 'Z')
            continue
        output =output + (c - k[j] + 26) % 26 + 'A'
        j = (j + 1) % k.length()
    return output
End

예제 코드(Example)

#include <iostream>
#include <string>
using namespace std;
class Vig {
    public:
        string k;
    Vig(string k) {
        for (int i = 0; i < k.size(); ++i) {
            if (k[i] >= 'A' && k[i] <= 'Z')
                this->k += k[i];
            else if (k[i] >= 'a' && k[i] <= 'z')
                this->k += k[i] + 'A' - 'a';
        }
    }
    string encryption(string t) {
        string output;
        for (int i = 0, j = 0; i < t.length(); ++i) {
            char c = t[i];
            if (c >= 'a' && c <= 'z')
                c += 'A' - 'a';
            else if (c < 'A' || c > 'Z')
                continue;
            output += (c + k[j] - 2 * 'A') % 26 + 'A'; //added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
            j = (j + 1) % k.length();
        }
        return output;
    }
    string decryption(string t) {
        string output;
        for (int i = 0, j = 0; i < t.length(); ++i) {
            char c = t[i];
            if (c >= 'a' && c <= 'z')
                c += 'A' - 'a';
            else if (c < 'A' || c > 'Z')
                continue;
            output += (c - k[j] + 26) % 26 + 'A';//added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ]
            j = (j + 1) % k.length();
        }
        return output;
    }
};
int main() {
    Vig v("WELCOME");
    string ori ="Thisistutorialspoint";
    string encrypt = v.encryption(ori);
    string decrypt = v.decryption(encrypt);
    cout << "Original Message: "<<ori<< endl;
    cout << "Encrypted Message: " << encrypt << endl;
    cout << "Decrypted Message: " << decrypt << endl;
}

실행 결과(Output)

Original Message: Thisistutorialspoint
Encrypted Message: PLTUWEXQXZTWMPOTZKBF
Decrypted Message: THISISTUTORIALSPOINT