- 密碼學教程
- 密碼學 - 首頁
- 密碼學 - 起源
- 密碼學 - 歷史
- 密碼學 - 原理
- 密碼學 - 應用
- 密碼學 - 優點與缺點
- 密碼學 - 現代密碼學
- 密碼學 - 傳統密碼
- 密碼學 - 加密的需求
- 密碼學 - 雙重強度加密
- 密碼系統
- 密碼系統
- 密碼系統 - 組成部分
- 密碼系統攻擊
- 密碼系統 - 彩虹表攻擊
- 密碼系統 - 字典攻擊
- 密碼系統 - 暴力破解攻擊
- 密碼系統 - 密碼分析技術
- 密碼學型別
- 密碼系統 - 型別
- 公鑰加密
- 現代對稱金鑰加密
- 密碼學雜湊函式
- 金鑰管理
- 密碼系統 - 金鑰生成
- 密碼系統 - 金鑰儲存
- 密碼系統 - 金鑰分發
- 密碼系統 - 金鑰撤銷
- 分組密碼
- 密碼系統 - 流密碼
- 密碼學 - 分組密碼
- 密碼學 - Feistel 分組密碼
- 分組密碼的工作模式
- 分組密碼的工作模式
- 電子密碼本 (ECB) 模式
- 密碼分組連結 (CBC) 模式
- 密文反饋 (CFB) 模式
- 輸出反饋 (OFB) 模式
- 計數器 (CTR) 模式
- 經典密碼
- 密碼學 - 反向密碼
- 密碼學 - 凱撒密碼
- 密碼學 - ROT13 演算法
- 密碼學 - 轉置密碼
- 密碼學 - 轉置密碼加密
- 密碼學 - 轉置密碼解密
- 密碼學 - 乘法密碼
- 密碼學 - 仿射密碼
- 密碼學 - 簡單替換密碼
- 密碼學 - 簡單替換密碼加密
- 密碼學 - 簡單替換密碼解密
- 密碼學 - 維吉尼亞密碼
- 密碼學 - 維吉尼亞密碼的實現
- 現代密碼
- Base64 編碼與解碼
- 密碼學 - XOR 加密
- 替換技術
- 密碼學 - 單字母替換密碼
- 密碼學 - 單字母替換密碼的破解
- 密碼學 - 多字母替換密碼
- 密碼學 - Playfair 密碼
- 密碼學 - Hill 密碼
- 多字母替換密碼
- 密碼學 - 一次性密碼本密碼
- 一次性密碼本密碼的實現
- 密碼學 - 轉置技術
- 密碼學 - 柵欄密碼
- 密碼學 - 列置換密碼
- 密碼學 - 密碼隱寫術
- 對稱演算法
- 密碼學 - 資料加密
- 密碼學 - 加密演算法
- 密碼學 - 資料加密標準 (DES)
- 密碼學 - 三重 DES
- 密碼學 - 雙重 DES
- 高階加密標準 (AES)
- 密碼學 - AES 結構
- 密碼學 - AES 變換函式
- 密碼學 - 位元組替換變換
- 密碼學 - 行移位變換
- 密碼學 - 列混淆變換
- 密碼學 - 輪金鑰加變換
- 密碼學 - AES 金鑰擴充套件演算法
- 密碼學 - Blowfish 演算法
- 密碼學 - SHA 演算法
- 密碼學 - RC4 演算法
- 密碼學 - Camellia 加密演算法
- 密碼學 - ChaCha20 加密演算法
- 密碼學 - CAST5 加密演算法
- 密碼學 - SEED 加密演算法
- 密碼學 - SM4 加密演算法
- IDEA - 國際資料加密演算法
- 公鑰(非對稱)密碼演算法
- 密碼學 - RSA 演算法
- 密碼學 - RSA 加密
- 密碼學 - RSA 解密
- 密碼學 - 建立 RSA 金鑰
- 密碼學 - 破解 RSA 密碼
- 密碼學 - ECDSA 演算法
- 密碼學 - DSA 演算法
- 密碼學 - Diffie-Hellman 演算法
- 密碼學中的資料完整性
- 密碼學中的資料完整性
- 訊息認證
- 密碼學數字簽名
- 公鑰基礎設施 (PKI)
- 雜湊
- MD5(訊息摘要演算法 5)
- SHA-1(安全雜湊演算法 1)
- SHA-256(安全雜湊演算法 256 位)
- SHA-512(安全雜湊演算法 512 位)
- SHA-3(安全雜湊演算法 3)
- 密碼雜湊
- Bcrypt 雜湊模組
- 現代密碼學
- 量子密碼學
- 後量子密碼學
- 密碼協議
- 密碼學 - SSL/TLS 協議
- 密碼學 - SSH 協議
- 密碼學 - IPsec 協議
- 密碼學 - PGP 協議
- 影像和檔案加密
- 密碼學 - 影像
- 密碼學 - 檔案
- 密碼隱寫術 - 影像
- 檔案加密和解密
- 密碼學 - 檔案加密
- 密碼學 - 檔案解密
- 物聯網中的密碼學
- 物聯網安全挑戰、威脅和攻擊
- 物聯網安全的密碼技術
- 物聯網裝置的通訊協議
- 常用的密碼技術
- 自定義構建密碼演算法(混合密碼學)
- 雲密碼學
- 量子密碼學
- 密碼學中的影像隱寫術
- DNA 密碼學
- 密碼學中的一次性密碼 (OTP) 演算法
- 區別
- 密碼學 - MD5 與 SHA1
- 密碼學 - RSA 與 DSA
- 密碼學 - RSA 與 Diffie-Hellman
- 密碼學與密碼學
- 密碼學 - 密碼學與密碼分析
- 密碼學 - 經典密碼學與量子密碼學
- 密碼學與隱寫術
- 密碼學與加密
- 密碼學與網路安全
- 密碼學 - 流密碼與分組密碼
- 密碼學 - AES 與 DES 密碼
- 密碼學 - 對稱密碼與非對稱密碼
- 密碼學有用資源
- 密碼學 - 快速指南
- 密碼學 - 討論
密碼學 - 轉置密碼解密
在上一章中,我們學習了轉置密碼加密。現在我們將學習轉置密碼的解密及其使用不同方法的實現。
解密過程只是反轉加密演算法的步驟。我們使用相同的金鑰來查詢讀取列以恢復實際訊息的正確順序。
轉置密碼解密演算法
以下是轉置密碼解密的演算法:
輸入
密文 - 加密的訊息。
金鑰 - 用於加密的相同金鑰。
步驟
首先,我們需要根據金鑰長度將密文分成多行。這意味著我們將為金鑰中的每個字元建立一個行。
現在,我們將建立一個空的網格,其行數與金鑰長度相同,列數等於密文長度除以金鑰長度的結果。
接下來,我們必須根據金鑰填充網格。因此,迭代密文字元以確定其列索引。
並將字元新增到網格中的相應單元格中。
從第一列開始,向下讀取字元,到達底部後移動到下一列。對所有列繼續此過程。這會根據原始轉置順序重新排列字元。
列印明文訊息。
示例
輸入
密文:OLHEL LWRDO
金鑰:SECRET
例如,如果我們知道金鑰是“SECRET”,並且原始訊息是根據金鑰中字母的順序進行轉置的。我們可以像下面這樣重新排列列:
| 金鑰 | S E C R E T |
| 列 | 5, 1, 2, 4, 6, 3 |
現在,我們將根據金鑰重新排序轉置訊息“OLHEL LWRDO”的列,如下所示:
| S | O | L |
| E | H | E |
| C | L | W |
| R | H | R |
| E | E | D |
| T | L | O |
最後,我們逐行讀取字元以獲取原始訊息:
原始訊息:HELLO WORLD
因此,使用金鑰“SECRET”對加密訊息“OLHEL LWRDO”解密後的訊息是“HELLO WORLD”。
太棒了!我們已經使用轉置密碼解密了我們的訊息。
使用 Python 實現
可以使用不同的方法實現轉置密碼的解密:
使用 Math 模組
使用 Pyperclip 模組
因此,讓我們在以下部分逐一檢視這兩種方法:
使用 Math 模組
此 Python 程式碼解密使用轉置密碼加密的訊息。我們將使用 math 模組執行數學運算,主要是計算解密訊息所需的列數。math.ceil() 用於將訊息長度除以金鑰的結果向上取整。因此,此程式碼有效地解密轉置密碼訊息。
示例
以下是使用 Math 模組的轉置密碼解密演算法的 Python 程式碼。請參見下面的程式:
import math
def transposition_decrypt(key, message):
num_of_columns = math.ceil(len(message) / key)
num_of_rows = key
num_of_shaded_boxes = (num_of_columns * num_of_rows) - len(message)
plaintext = [''] * num_of_columns
col = 0
row = 0
for symbol in message:
plaintext[col] += symbol
col += 1
if (col == num_of_columns) or (col == num_of_columns - 1 and row >= num_of_rows - num_of_shaded_boxes):
col = 0
row += 1
return ''.join(plaintext)
ciphertext = 'Toners raiCntisippoh'
key = 6
plaintext = transposition_decrypt(key, ciphertext)
print("Cipher Text: ", ciphertext)
print("The plain text is: ", plaintext)
以下是上述示例的輸出:
輸入/輸出
Cipher Text: Toners raiCntisippoh The plain text is: Transposition Cipher
在上面的輸出中,您可以看到密文訊息是 Toners raiCntisippoh,解密後的訊息是 Transposition Cipher。
使用 Pyperclip 模組
在此示例中,我們將使用 Python 的 pyperclip 模組,它用於複製和貼上剪貼簿功能。因此,我們將使用此模組將解密的訊息複製到剪貼簿。
此程式碼與上面的程式碼類似,但在本程式碼中,我們使用 pyperclip 模組將解密的訊息複製到剪貼簿。
示例
以下是使用 pyperclip 模組的轉置密碼解密演算法的 Python 程式碼。請檢查下面的程式碼:
import math
import pyperclip
def transposition_decrypt(key, message):
num_of_columns = math.ceil(len(message) / key)
num_of_rows = key
num_of_shaded_boxes = (num_of_columns * num_of_rows) - len(message)
plaintext = [''] * num_of_columns
col = 0
row = 0
for symbol in message:
plaintext[col] += symbol
col += 1
if (col == num_of_columns) or (col == num_of_columns - 1 and row >= num_of_rows - num_of_shaded_boxes):
col = 0
row += 1
return ''.join(plaintext)
ciphertext = 'Toners raiCntisippoh'
key = 6
plaintext = transposition_decrypt(key, ciphertext)
print("Cipher Text:", ciphertext)
print("The plain text is:", plaintext)
# Copy the decrypted plaintext to the clipboard
pyperclip.copy(plaintext)
print("The Decrypted Message is Copied to the Clipboard")
以下是上述示例的輸出:
輸入/輸出
Cipher Text: Toners raiCntisippoh The plain text is: Transposition Cipher The Decrypted Message is Copied to the Clipboard
在上面的輸出中,我們可以看到明文已複製到剪貼簿,並且顯示訊息“解密後的訊息已複製到剪貼簿”。
使用 Java 實現
我們將使用 Java 程式語言來實現轉置密碼的解密。基本上,我們將使用 Java 在此實現中反轉加密過程。
請參見 Java 中的以下程式碼:
示例
public class TranspositionCipher {
// Function to decrypt using transposition cipher
public static String decrypt(String ciphertext, int key) {
StringBuilder plaintext = new StringBuilder();
int cols = (int) Math.ceil((double) ciphertext.length() / key);
char[][] matrix = new char[cols][key];
// Fill matrix with ciphertext characters
int index = 0;
for (int j = 0; j < key; ++j) {
for (int i = 0; i < cols; ++i) {
if (index < ciphertext.length())
matrix[i][j] = ciphertext.charAt(index++);
else
matrix[i][j] = ' ';
}
}
// Read matrix row-wise to get plaintext
for (int i = 0; i < cols; ++i) {
for (int j = 0; j < key; ++j) {
plaintext.append(matrix[i][j]);
}
}
return plaintext.toString();
}
public static void main(String[] args) {
String ciphertext = "Hohiebtlre,isrei ll s yafwdlT v uuo ";
int key = 4;
System.out.println("The Encrypted text: " + ciphertext);
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted text: " + decryptedText);
}
}
以下是上述示例的輸出:
輸入/輸出
The Encrypted text: Hohiebtlre,isrei ll s yafwdlT v uuo Decrypted text: Hello, This is very beautiful world
使用 C++ 實現
現在,我們將使用 C++ 程式語言來實現轉置密碼的解密演算法。在其中,我們將透過 decrypt 函式反轉加密過程。它透過計算密文長度和金鑰來找到所需的列數。它將密文中的字元按列新增到矩陣中。之後,它使用矩陣的逐行讀取來構建明文。
以下是使用 C++ 實現轉置密碼解密的程式碼:
示例
#include <iostream>
#include <string>
#include <cmath>
// Function to decrypt using transposition cipher
std::string decrypt(std::string ciphertext, int key) {
std::string plaintext = "";
int cols = (ciphertext.length() + key - 1) / key;
char matrix[cols][key];
// Fill matrix with ciphertext characters
int index = 0;
for (int j = 0; j < key; ++j) {
for (int i = 0; i < cols; ++i) {
if (index < ciphertext.length())
matrix[i][j] = ciphertext[index++];
else
matrix[i][j] = ' ';
}
}
// Read matrix row-wise to get plaintext
for (int i = 0; i < cols; ++i) {
for (int j = 0; j < key; ++j) {
plaintext += matrix[i][j];
}
}
return plaintext;
}
int main() {
std::string ciphertext = "Houiptme,tao il oliFllTrsnay";
int key = 4;
std::cout << "The Encrypted text: " << ciphertext << std::endl;
std::string decrypted_text = decrypt(ciphertext, key);
std::cout << "The Decrypted text: " << decrypted_text << std::endl;
return 0;
}
以下是上述示例的輸出:
輸入/輸出
The Encrypted text: Houiptme,tao il oliFllTrsnay The Decrypted text: Hello, Tutorialspoint Family
總結
在本章中,我們已經瞭解瞭如何使用 Python、C++ 和 Java 中的轉置密碼解密演算法將密文解密回其原始形式。