Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

Vigenere Cypher 구현을 위한 C++ 프로그램

<시간/>

Vigenere Cipher는 알파벳 텍스트를 암호화하는 일종의 다중 알파벳 대체 방법입니다.

Vigenere Cipher Table은 A부터 Z까지의 알파벳을 26행으로 쓰는 방식으로 암호화와 복호화를 위해 사용됩니다.

Vigenere Cypher 구현을 위한 C++ 프로그램

암호화

키: 환영합니다

메시지: Thisistutorialspoint

여기서 우리는 길이가 원래 메시지 길이와 같아질 때까지 주어진 키를 반복하여 키를 얻어야 합니다.

암호화를 위해 메시지의 첫 글자와 키(예:T 및 W)를 사용합니다. T 행과 W 열이 일치하는 Vigenere Cipher Table(예:P)에서 알파벳을 사용합니다.

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

마지막으로 암호화된 메시지 텍스트는 -

암호화된 메시지: PLTUWEXQXZTWMPOTZKBF.

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

Ei =(파이 + Ki) 모드 26

여기서 P는 일반 텍스트이고 K는 키입니다.

복호화

키: 환영합니다

암호화된 메시지: PLTUWEXQXZTWMPOTZKBF

생성된 키와 암호화된 메시지의 첫 번째 알파벳, 즉 P와 W를 가져옵니다. Vigenere Cipher Table을 분석하고 W열에서 알파벳 P를 찾으면 해당 행이 원본 메시지의 첫 번째 알파벳인 T가 됩니다.

암호화된 메시지의 모든 알파벳에 대해 이 과정을 반복합니다.

원본 메시지:Thisistutorialspoint

이것은 다음 방정식으로 대수적 형태로 나타낼 수 있습니다.

파이 =(Ei – Ki + 26) 모드 26

다음은 Vigenere 암호를 구현하는 C++ 프로그램입니다.

알고리즘

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

예시

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

출력

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