密碼學 - ROT13 演算法



到目前為止,您已經瞭解了反向密碼和凱撒密碼演算法。現在,讓我們討論 ROT13 演算法及其實現。

因此,我們將討論 ROT13 密碼演算法。旋轉 13 個位置被稱為 ROT13。它是凱撒密碼加密演算法的一種特殊情況。它是最簡單的加密方法之一。由於加密和解密的演算法相同,因此它是一種對稱金鑰加密技術。

因為加密金鑰將字母 A 到 Z 顯示為數字 0 到 25,所以密文與明文字母相差 13 個空格。例如,A 變為 N,B 變為 O,依此類推。由於加密和解密過程相同,因此它們可以以程式設計方式完成。

ROT13 演算法的解釋

ROT13 密碼指的是縮寫形式旋轉 13 個位置。在此特定凱撒密碼中,移位始終為 13。透過將每個字母移動 13 個位置來加密或解密訊息。

讓我們檢視下圖以瞭解 ROT13 演算法的工作原理 -

ROT13 algorithm

您可以看到,每個字母都向右移動了 13 個空格。例如,“B”將變為“O”,“D”將變為“Q”,依此類推。

ROT13 的特點

以下是 ROT13 的一些主要特徵 -

  • 由於 ROT13 是對稱的,因此加密和解密都使用相同的演算法和金鑰。

  • 它適用於每個字母,將每個字母替換為字母表中向前(或向後,基於解密)13 個位置的字母。

  • ROT13 中的旋轉值為 13 是固定的。

  • 應用 ROT13 時,如果旋轉超過“Z”,則返回到字母表的開頭“A”。

  • ROT13 非常易於實現和使用。

  • 它無法抵禦任何有決心的攻擊者,因為它很容易透過簡單的頻率分析破解。

使用 Python 實現

因此,我們可以透過不同的方式實現此演算法 -

使用 For 迴圈和 ord() 函式

我們首先將給定的訊息加密並使用 for 迴圈生成 ROT13 程式碼。在此程式中,for 迴圈將用於迭代給定文字中的每個字元。如果字元是字母,我們將保留它(大寫或小寫)並將其移動 13 個位置。非字母字元將保持不變。我們還將使用 Python 的 ord() 函式,該函式用於將單個 Unicode 字元更改為其整數表示。

示例

以下是 ROT13 密碼的簡單 Python 程式碼。請參閱以下程式碼 -

# Encryption Function 
def rot13_encrypt(text):
   encrypted_text = ''
   for char in text:
      if char.isalpha():
         shifted = ord(char) + 13
         if char.islower():
            if shifted > ord('z'):
               shifted -= 26
         else:
            if shifted > ord('Z'):
               shifted -= 26
         encrypted_text += chr(shifted)
      else:
         encrypted_text += char
   return encrypted_text

# Decryption Function 
def rot13_decrypt(text):
   decrypted_text = ''
   for char in text:
      if char.isalpha():
         shifted = ord(char) - 13  # Decryption involves shifting back by 13
         if char.islower():
            if shifted < ord('a'):
               shifted += 26
         else:
            if shifted < ord('A'):
               shifted += 26
         decrypted_text += chr(shifted)
      else:
         decrypted_text += char
   return decrypted_text

# function execution
message = "Hello, Tutorialspoint!"
 encrypted_msg = rot13_encrypt(message)
print("The Encrypted message:",  encrypted_msg)  

 decrypted_msg = rot13_decrypt( encrypted_msg)
print("The Decrypted message:",  decrypted_msg)

以下是上述示例的輸出 -

輸入/輸出
The Encrypted message: Uryyb, Ghgbevnyfcbvag!
The Decrypted message: Hello, Tutorialspoint!

使用列表推導式

在此示例中,列表推導式用於執行 ROT13 加密。使用列表,我們將使用末尾的 for char in text 部分迭代輸入文字中的每個字元。對於輸入文字中的每個字元,我們將使用條件表示式查詢加密值。

示例

以下是 ROT13 演算法的簡單 Python 程式碼。請參閱以下程式 -

# Encryption function
def rot13_encrypt(text):
   encrypted_text = ''.join([chr(((ord(char) - 65 + 13) % 26) + 65) if 'A' <= char <= 'Z' else 
      chr(((ord(char) - 97 + 13) % 26) + 97) if 'a' <= char <= 'z' else char for char in text])
   return encrypted_text

# Decryption function
def rot13_decrypt(text):
   decrypted_text = ''.join([chr(((ord(char) - 65 - 13) % 26) + 65) if 'A' <= char <= 'Z' else 
      chr(((ord(char) - 97 - 13) % 26) + 97) if 'a' <= char <= 'z' else char for char in text])
   return decrypted_text

# Function execution
message = "Hello, Everyone!"
 encrypted_msg = rot13_encrypt(message)
print("The Encrypted message:",  encrypted_msg)

 decrypted_msg = rot13_decrypt( encrypted_msg)
print("The Decrypted message:",  decrypted_msg)

以下是上述示例的輸出 -

輸入/輸出
The Encrypted message: Uryyb, Rirelbar!
The Decrypted message: Hello, Everyone!

使用字典

在此示例中,我們將使用兩個字典來實現 ROT13 的程式。因此,第一個字典將字母表中的大寫字母對映到它們在字母表中的索引。第二個字典將移位的索引映射回大寫字母,從“Z”到“A”。加密函式在使用第一個字典識別每個字母的索引並新增移位值後,使用第二個字典將結果索引發送回字母。在解密函式中,我們將反轉此過程。

示例

以下是用兩個字典實現的 ROT13 演算法的簡單 Python 程式。檢視以下程式碼 -

# Dictionary to lookup the index 
dictionary1 = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5,
   'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10,
   'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15,
   'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20,
   'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}

# Dictionary to lookup alphabets 
dictionary2 = {0: 'Z', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E',
   6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J',
   11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O',
   16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T',
   21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y'}

# Encryption Function 
def encrypt(msg, shift):
   cipher = ''
   for letter in msg:
      # check for space
      if letter != ' ':
         num = (dictionary1[letter] + shift) % 26
         cipher += dictionary2[num]
      else:
         cipher += ' '

   return cipher

# Decryption Function 
def decrypt(msg, shift):
   decipher = ''
   for letter in msg:
      # checks for space
      if letter != ' ':
         num = (dictionary1[letter] - shift + 26) % 26
         decipher += dictionary2[num]
      else:
         decipher += ' '

   return decipher

msg = "Hey Tutorialspoint"
shift = 13
result = encrypt(msg.upper(), shift)
print("The Encrypted message: ", result)

msg = "URL GHGBEVNYFCBVAG"
shift = 13
result = decrypt(msg.upper(), shift)
print("The Decrypted message: ", result)

以下是上述示例的輸出 -

輸入/輸出
The Encrypted message:  URL GHGBEVNYFCBVAG
The Decrypted message:  HEY TUTORIALSPOINT

使用 C++ 實現

為了實現 ROT13 演算法,我們將使用 C++ 程式語言。使用 rot13Func 函式,我們將加密給定的字串訊息。迭代給定輸入文字中的每個字母。如果它是字母,則將其字元更改 13 個位置以獲取匹配的 ROT13 字元。主函式呼叫 rot13Func 函式並輸出加密文字。

示例

#include <iostream>
#include <string>

using namespace std;

// Function to perform rot13Func encryption
string rot13Func(string text) {
   for (char& c : text) {
      if (isalpha(c)) {
         char base = islower(c) ? 'a' : 'A';
         c = (c - base + 13) % 26 + base;
      }
   }
   return text;
}

int main() {
   string plaintext = "Hello this world is so beautiful";
   cout << "The Plaintext Message is: " << plaintext << endl;

   string encrypted_text = rot13Func(plaintext);
   cout << "Encrypted text: " << encrypted_text << endl;

   return 0;
}

以下是上述示例的輸出 -

輸入/輸出

The Plaintext Message is: Hello this world is so beautiful
Encrypted text: Uryyb guvf jbeyq vf fb ornhgvshy

使用 Java 實現

在此實現中,我們將使用 Java 程式語言來建立 ROT13 演算法。rot13Func 方法使用字串文字作為輸入,使用 ROT13 對其進行加密。迭代輸入文字中的每個字元。如果它是字母,則將其字元更改 13 個位置以獲取匹配的 ROT13 字元。在收到輸入訊息後,main 方法呼叫 rot13 函式並輸出加密文字。

示例

public class ROT13Class {
   // Function to perform ROT13 encryption
   public static String rot13Func(String text) {
      StringBuilder result = new StringBuilder();
      for (char c : text.toCharArray()) {
         if (Character.isLetter(c)) {
            char base = Character.isLowerCase(c) ? 'a' : 'A';
            c = (char) (((c - base + 13) % 26) + base);
         }
         result.append(c);
      }
      return result.toString();
   }

   public static void main(String[] args) {
      String plaintext = "The world is so beautiful!";
      System.out.println("The Plain Text Message: " + plaintext);
      String encryptedText = rot13Func(plaintext);
      System.out.println("The Encrypted text: " + encryptedText);
   }
}

以下是上述示例的輸出 -

輸入/輸出

The Plain Text Message: The world is so beautiful!
The Encrypted text: Gur jbeyq vf fb ornhgvshy!

缺點

由於 ROT13 密碼實際上是凱撒密碼的一種特殊情況應用,因此它並不安全。儘管 ROT13 密碼可以透過簡單地將字母移動 13 個位置來破解,但凱撒密碼只能透過頻率分析或嘗試所有 25 個金鑰來破解。因此,它在現實生活中毫無用處。

ROT13 演算法分析

ROT13 密碼演算法被認為是凱撒密碼的一種特殊情況。它不是一種非常安全的演算法,可以很容易地透過頻率分析或僅嘗試可能的 25 個金鑰來破解,而 ROT13 可以透過移動 13 個位置來破解。因此,它沒有任何實際用途。

總結

在本文中,我們檢查了 Python 密碼學 ROT13 演算法。ROT13 是一種快速有效的方法,可以透過將字母表中的每個字母移動 13 個位置來加密給定的訊息。它主要用於基本加密任務。我們在本章中實現了不同的方法,因此您現在可以使用 Python 中的 ROT13 演算法加密和解密訊息。

廣告
© . All rights reserved.