密碼學 - 維吉尼亞密碼的實現



在上一章中,我們看到了維吉尼亞密碼,它的方法、優點和缺點。現在我們將使用不同的程式語言(如 Python、Java 和 C++)來實現維吉尼亞密碼。

使用 Python 實現

這段 Python 程式碼實現了維吉尼亞密碼,這是一種用於加密和解密文字訊息的技術。generate_key() 函式以關鍵字作為輸入,基於該關鍵字生成金鑰,然後根據需要重複單詞以確保金鑰與文字長度匹配。encrypt_text() 函式使用維吉尼亞密碼演算法對文字進行加密,該演算法根據金鑰中匹配的字元對每個字元進行移位。同樣,decrypt_text() 方法透過反轉加密過程來解密密文並顯示原始文字。

示例

以下是使用 Python 實現維吉尼亞密碼的示例:

def generate_key(text, keyword):
   key = list(keyword)
   if len(text) == len(keyword):
      return key
   else:
      for i in range(len(text) - len(keyword)):
         key.append(key[i % len(keyword)])
   return "".join(key)

def encrypt_text(text, key):
   cipher_text = []
   for i in range(len(text)):
      x = (ord(text[i]) + ord(key[i])) % 26
      x += ord('A')
      cipher_text.append(chr(x))
   return "".join(cipher_text)

def decrypt_text(cipher_text, key):
   original_text = []
   for i in range(len(cipher_text)):
      x = (ord(cipher_text[i]) - ord(key[i]) + 26) % 26
      x += ord('A')
      original_text.append(chr(x))
   return "".join(original_text)

# Driver code
text = "TUTORIALSPOINT"
keyword = "KEY"
key = generate_key(text, keyword)
cipher_text = encrypt_text(text, key)
print("The Encrypted text:", cipher_text)
print("The Original/Decrypted Text:", decrypt_text(cipher_text, key))

以下是上述示例的輸出:

輸入/輸出

The Encrypted text: DYRYVGKPQZSGXX
The Original/Decrypted Text: TUTORIALSPOINT

使用 Java 實現

維吉尼亞密碼是一種用於加密和解密訊息的方法。程式碼使用文字訊息的長度和一個關鍵字來生成金鑰。然後,使用此金鑰透過將字母表中的每個字元移位到金鑰中其正確的位置來加密文字訊息。為了解密訊息,使用相同的金鑰反轉加密過程並將每個字元移回其原始字母位置。擁有金鑰的各方可以彼此私下通訊,結果是原始訊息。

示例

請參閱以下維吉尼亞密碼的 Java 實現:

public class VigenereCipher {
   static String generateKey(String text, String keyword) {
     int textLength = text.length();
     for (int i = 0; ; i++) {
         if (textLength == i)
            i = 0;
         if (keyword.length() == textLength)
            break;
         keyword += (keyword.charAt(i));
      }
      return keyword;
   }

   static String encryptText(String text, String keyword) {
      String ciphertext = "";

      for (int i = 0; i < text.length(); i++) {
         // converting in range 0-25
         int x = (text.charAt(i) + keyword.charAt(i)) % 26;

         // convert into alphabets(ASCII)
         x += 'A';

         ciphertext += (char)(x);
      }
      return ciphertext;
   }

   // Decrypt the encrypted text and return the original text
   static String decryptText(String ciphertext, String keyword) {
     String originalText = "";

      for (int i = 0 ; i < ciphertext.length() && i < keyword.length(); i++) {
         // converting in range 0-25
         int x = (ciphertext.charAt(i) - keyword.charAt(i) + 26) % 26;

         // convert into alphabets(ASCII)
         x += 'A';
         originalText += (char)(x);
      }
      return originalText;
   }

   // Driver code
   public static void main(String[] args) {
      String message = "Hello everyone";
      String keyword = "Best";

      String text = message.toUpperCase();
      String key = keyword.toUpperCase();

      String generatedKey = generateKey(text, key);
      String encryptedText = encryptText(text, generatedKey);

      System.out.println("The Encrypted Text : " + encryptedText);

      System.out.println("The Original/Decrypted Text : " + decryptText(encryptedText, generatedKey));
   }
}

以下是上述示例的輸出:

輸入/輸出

The Encrypted Text : IIDEPXWOFVQHOI
The Original/Decrypted Text : HELLOTEVERYONE

使用 C++ 實現

在本程式碼中,我們將使用 C++ 程式語言來實現維吉尼亞密碼。在這個密碼中,我們將對明文訊息的字元應用一系列凱撒移位,使用一個金鑰。金鑰中的每個字母對應一個不同的移位值,金鑰定義了對每個字元應用的移位量。在加密中,明文中的每個字元都根據金鑰中給出的相應數字在字母表中向前移位。

同樣,在解密中,透過將密文的字元向後移位相同數量(金鑰中顯示的數量),使原始明文可見。

示例

以下是使用 C++ 程式語言實現維吉尼亞密碼的簡單示例:

#include <iostream>  
#include <string>  
using namespace std;  
 
class VigenereCipher {  
   public:  
      string key;  

   VigenereCipher(string key) {  
      for (int i = 0; i < key.size(); ++i) {  
         if (key[i] >= 'A' && key[i] <= 'Z')  
            this -> key += key[i];  
         else if (key[i] >= 'a' && key[i] <= 'z')  
            this -> key += key[i] + 'A' - 'a';  
      }  
   }  
   
   string encryptFunc(string text) {  
      string out;  
      for (int i = 0, j = 0; i < text.length(); ++i) {  
         char c = text[i];  
         if (c >= 'a' && c <= 'z')  
            c += 'A' - 'a';  
         else if (c < 'A' || c > 'Z')  
            continue;  
         out += (c + key[j] - 2 * 'A') % 26 + 'A';  
         j = (j + 1) % key.length();  
      }  
      return out;  
   }  
   
   string decryptFunc(string text) {  
      string out;  
      for (int i = 0, j = 0; i < text.length(); ++i) {  
         char c = text[i];  
         if (c >= 'a' && c <= 'z')  
            c += 'A' - 'a';  
         else if (c < 'A' || c > 'Z')  
            continue;  
         out += (c - key[j] + 26) % 26 + 'A';  
         j = (j + 1) % key.length();  
      }  
      return out;  
   }  
}; 

int main() {  
   VigenereCipher cipher("VigenereCipher");   
   string plaintext = "Cyber Security";  
   string et = cipher.encryptFunc(plaintext);  
   string dt = cipher.decryptFunc(et);  

   cout << "The Plaintext: " << plaintext << endl;  
   cout << "The Encrypted Text: " << et << endl;  
   cout << "The Decrypted Text: " << dt << endl;  
}  

以下是上述示例的輸出:

輸入/輸出

The Plaintext: Cyber Security
The Encrypted Text: XGHIEWVGWZXAC
The Decrypted Text: CYBERSECURITY
廣告
© . All rights reserved.