- 密碼學教程
- 密碼學 - 首頁
- 密碼學 - 起源
- 密碼學 - 歷史
- 密碼學 - 原理
- 密碼學 - 應用
- 密碼學 - 優點與缺點
- 密碼學 - 現代
- 密碼學 - 傳統密碼
- 密碼學 - 加密的需求
- 密碼學 - 雙重強度加密
- 密碼系統
- 密碼系統
- 密碼系統 - 組成部分
- 密碼系統的攻擊
- 密碼系統 - 彩虹表攻擊
- 密碼系統 - 字典攻擊
- 密碼系統 - 暴力破解攻擊
- 密碼系統 - 密碼分析技術
- 密碼學的型別
- 密碼系統 - 型別
- 公鑰加密
- 現代對稱金鑰加密
- 密碼學雜湊函式
- 金鑰管理
- 密碼系統 - 金鑰生成
- 密碼系統 - 金鑰儲存
- 密碼系統 - 金鑰分發
- 密碼系統 - 金鑰撤銷
- 分組密碼
- 密碼系統 - 流密碼
- 密碼學 - 分組密碼
- 密碼學 - Feistel 分組密碼
- 分組密碼的工作模式
- 分組密碼的工作模式
- 電子密碼本 (ECB) 模式
- 密碼分組連結 (CBC) 模式
- 密碼反饋 (CFB) 模式
- 輸出反饋 (OFB) 模式
- 計數器 (CTR) 模式
- 經典密碼
- 密碼學 - 逆向密碼
- 密碼學 - 凱撒密碼
- 密碼學 - ROT13 演算法
- 密碼學 - 換位密碼
- 密碼學 - 加密換位密碼
- 密碼學 - 解密換位密碼
- 密碼學 - 乘法密碼
- 密碼學 - 仿射密碼
- 密碼學 - 簡單替換密碼
- 密碼學 - 簡單替換密碼的加密
- 密碼學 - 簡單替換密碼的解密
- 密碼學 - 維吉尼亞密碼
- 密碼學 - 實現維吉尼亞密碼
- 現代密碼
- Base64 編碼與解碼
- 密碼學 - XOR 加密
- 替換技術
- 密碼學 - 單字母替換密碼
- 密碼學 - 破解單字母替換密碼
- 密碼學 - 多字母替換密碼
- 密碼學 - Playfair 密碼
- 密碼學 - 希爾密碼
- 多字母替換密碼
- 密碼學 - 一次性密碼本密碼
- 一次性密碼本密碼的實現
- 密碼學 - 換位技術
- 密碼學 - 柵欄密碼
- 密碼學 - 列置換密碼
- 密碼學 - 隱寫術
- 對稱演算法
- 密碼學 - 資料加密
- 密碼學 - 加密演算法
- 密碼學 - 資料加密標準
- 密碼學 - 三重 DES
- 密碼學 - 雙重 DES
- 高階加密標準
- 密碼學 - AES 結構
- 密碼學 - AES 變換函式
- 密碼學 - 位元組替換變換
- 密碼學 - 行移位變換
- 密碼學 - 列混合變換
- 密碼學 - 輪金鑰加變換
- 密碼學 - AES 金鑰擴充套件演算法
- 密碼學 - Blowfish 演算法
- 密碼學 - SHA 演算法
- 密碼學 - RC4 演算法
- 密碼學 - Camellia 加密演算法
- 密碼學 - ChaCha20 加密演算法
- 密碼學 - CAST5 加密演算法
- 密碼學 - SEED 加密演算法
- 密碼學 - SM4 加密演算法
- IDEA - 國際資料加密演算法
- 公鑰(非對稱)密碼學演算法
- 密碼學 - RSA 演算法
- 密碼學 - RSA 加密
- 密碼學 - RSA 解密
- 密碼學 - 建立 RSA 金鑰
- 密碼學 - 破解 RSA 密碼
- 密碼學 - ECDSA 演算法
- 密碼學 - DSA 演算法
- 密碼學 - Diffie-Hellman 演算法
- 密碼學中的資料完整性
- 密碼學中的資料完整性
- 訊息認證
- 密碼學數字簽名
- 公鑰基礎設施
- 雜湊
- 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 密碼
- 密碼學 - 對稱與非對稱
- 密碼學有用資源
- 密碼學 - 快速指南
- 密碼學 - 討論
密碼學 - 簡單替換密碼的解密
在上一章中,我們看到了簡單替換密碼的不同加密程式。現在我們將討論解密方法,使用 Python、Java 和 C++ 使用簡單替換密碼解密給定的訊息。
如您所知,解密是加密的反向過程,因此我們將反轉我們在上一章中為加密建立的所有程式。
使用 Python 實現
我們可以使用以下方法實現簡單替換密碼的解密:
使用指定的偏移量
使用字典對映
直接使用 ASCII 值
在接下來的部分中,我們將嘗試詳細描述這些方法中的每一種,以便您可以更好地理解 Python 密碼學和簡單替換密碼解密。
方法 1:使用指定的偏移量
要為之前的加密程式建立解密函式,我們可以使用相同的方法,但反轉偏移方向。以下函式透過以與原始偏移相反的方向移動密文中每個字母來反轉加密過程。它的工作原理是減去偏移量而不是加上它。所以基本上我們在撤銷加密。
以下是使用此方法在 Python 中實現解密過程:
示例
# function for decryption
def substitution_decrypt(encrypted_text, shift):
decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
ascii_val = ord(char)
if char.isupper():
# Reverse the shift for uppercase letters
shifted_ascii = ((ascii_val - 65 - shift) % 26) + 65
else:
# Reverse the shift for lowercase letters
shifted_ascii = ((ascii_val - 97 - shift) % 26) + 97
decrypted_char = chr(shifted_ascii)
decrypted_text += decrypted_char
else:
decrypted_text += char
return decrypted_text
# our cipher text
encrypted_text = "Mjqqt, jajwdtsj!"
shift = 5
decrypted_text = substitution_decrypt(encrypted_text, shift)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)
以下是上述示例的輸出:
輸入/輸出
Cipher Text: Mjqqt, jajwdtsj! Decrypted text: Hello, everyone!
方法 2:使用字典對映
在這裡,我們將使用字典對映來實現簡單替換密碼解密。正如我們在上一章中使用字典對映來加密明文一樣。現在我們將使用相同的技術,但反向。我們需要反轉此對映。而不是將每個字母對映到下一個字母,我們將密文中每個字母映射回明文中的原始字母。
這需要建立一個反向字典,其中鍵是密文中的字元,值是明文中的相應字元。
以下是使用反向字典對映實現簡單替換密碼解密的 Python 程式碼:
# function for decryption
def substitution_decrypt(encrypted_text, key):
# a reverse dictionary mapping for decryption
reverse_mapping = {key[i]: chr(97 + i) for i in range(26)}
# Decrypt the using reverse mapping
decrypted_text = ''.join(reverse_mapping.get(char, char) for char in encrypted_text)
return decrypted_text
# our cipher text
encrypted_text = "ifmmp, efbs gsjfoe!"
key = "bcdefghijklmnopqrstuvwxyza" # Example substitution key
decrypted_text = substitution_decrypt(encrypted_text, key)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)
以下是上述示例的輸出:
輸入/輸出
Cipher Text: ifmmp, efbs gsjfoe! Decrypted text: hello, dear friend!
方法 3:直接使用 ASCII 值
ASCII 是一種字元編碼標準,我們為字母、數字和其他字元分配數值。在加密過程中,明文的每個字母都會使用 ASCII 值偏移固定數量。現在我們必須解密,因此我們將透過從每個字元中減去相同的固定數量來反轉偏移過程。
例如,如果我們正在解密偏移了 3 的“d”(ASCII 值 100),我們將減去 3 以返回到“a”(ASCII 值 97)。
因此,以下是使用 ASCII 值直接實現簡單替換密碼解密的 Python 程式碼:
# function for decryption
def substitution_decrypt(encrypted_text, shift):
decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
# Use ASCII values directly to reverse the shift for decryption
ascii_val = ord(char)
shifted_ascii = ascii_val - shift
if char.isupper():
if shifted_ascii < 65:
shifted_ascii += 26
elif shifted_ascii > 90:
shifted_ascii -= 26
elif char.islower():
if shifted_ascii < 97:
shifted_ascii += 26
elif shifted_ascii > 122:
shifted_ascii -= 26
decrypted_text += chr(shifted_ascii)
else:
decrypted_text += char
return decrypted_text
# our cipher text
encrypted_text = "Khoor, pb ghdu froohdjxh!"
shift = 3
decrypted_text = substitution_decrypt(encrypted_text, shift)
print("Cipher Text: ", encrypted_text)
print("Decrypted text:", decrypted_text)
以下是上述示例的輸出:
輸入/輸出
Cipher Text: Khoor, pb ghdu froohdjxh! Decrypted text: Hello, my dear colleague!
使用 Java 實現
在此示例中,我們將使用 Java 實現簡單替換密碼的解密。因此,我們將使用 Java 的 Hashmap 和 Map 庫來實現此功能。decryptMessage() 方法的工作原理與加密方法類似,但在此我們將使用解密對映。它將每個加密的字母映射回字母表中的實際字母。我們將使用此方法解密我們在上一章中生成的密文並獲取原始明文。
示例
因此,使用 Java 的 Hashmap 和 Map 庫的實現如下:
import java.util.HashMap;
import java.util.Map;
public class SSC {
private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";
private static final String encrypted_alphabet = "bcdefghijklmnopqrstuvwxyza";
private static final Map<Character, Character> encryptionMap = new HashMap<>();
private static final Map<Character, Character> decryptionMap = new HashMap<>();
static {
for (int i = 0; i < alphabet.length(); i++) {
encryptionMap.put(alphabet.charAt(i), encrypted_alphabet.charAt(i));
decryptionMap.put(encrypted_alphabet.charAt(i), alphabet.charAt(i));
}
}
public static String decryptMessage(String ciphertext) {
StringBuilder plaintext = new StringBuilder();
for (char c : ciphertext.toLowerCase().toCharArray()) {
if (decryptionMap.containsKey(c)) {
plaintext.append(decryptionMap.get(c));
} else {
plaintext.append(c);
}
}
return plaintext.toString();
}
public static void main(String[] args) {
String et = "ifmmp uvupsjbmtqpjou";
System.out.println("The Encrypted text: " + et);
String dt = decryptMessage(et);
System.out.println("The Decrypted text: " + dt);
}
}
以下是上述示例的輸出:
輸入/輸出
The Encrypted text: ifmmp uvupsjbmtqpjou The Decrypted text: hello tutorialspoint
使用 C++ 實現
現在我們將使用 C++ 來實現簡單替換密碼的解密過程。因此,我們將反轉我們在上一章中討論過的加密過程。因此,在此我們使用 C++ 的“unordered_map”容器。它用於將每個加密的字母映射回字母表中的實際形式。我們將使用此方法解密密文並獲取明文訊息。
示例
請參閱以下 C++ 程式語言中簡單替換密碼解密函式的程式碼:
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
class SSC {
private:
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const string encrypted_alphabet = "bcdefghijklmnopqrstuvwxyza";
unordered_map<char, char> encryptionMap;
unordered_map<char, char> decryptionMap;
public:
SSC() {
for (int i = 0; i < alphabet.length(); i++) {
encryptionMap[alphabet[i]] = encrypted_alphabet[i];
decryptionMap[encrypted_alphabet[i]] = alphabet[i];
}
}
string decryptMessage(string ciphertext) {
string plaintext = "";
for (char c : ciphertext) {
if (decryptionMap.find(tolower(c)) != decryptionMap.end()) {
plaintext += decryptionMap[tolower(c)];
} else {
plaintext += c;
}
}
return plaintext;
}
};
int main() {
SSC cipher;
string et = "ifmmp uifsf ipx bsf zpv!";
cout << "The Encrypted text: " << et << endl;
string dt = cipher.decryptMessage(et);
cout << "The Decrypted text: " << dt << endl;
return 0;
}
以下是上述示例的輸出:
輸入/輸出
The Encrypted text: ifmmp uifsf ipx bsf zpv! The Decrypted text: hello there how are you!
總結
本章介紹了在 Python、Java 和 C++ 中解密簡單替換密碼的不同方法。我們已經看到了使用指定偏移量、字典對映、直接使用 ASCII 值以及透過對映的方法。這些方法解釋了使用簡單替換密碼解密加密文字的不同方法,這些方法提供了使用不同程式語言的靈活性。