- 密码学教程
- 密码学 - 首页
- 密码学 - 起源
- 密码学 - 历史
- 密码学 - 原理
- 密码学 - 应用
- 密码学 - 优点与缺点
- 密码学 - 现代密码学
- 密码学 - 传统密码
- 密码学 - 加密的需求
- 密码学 - 双重强度加密
- 密码系统
- 密码系统
- 密码系统 - 组成部分
- 密码系统攻击
- 密码系统 - 彩虹表攻击
- 密码系统 - 字典攻击
- 密码系统 - 暴力攻击
- 密码系统 - 密码分析技术
- 密码学类型
- 密码系统 - 类型
- 公钥加密
- 现代对称密钥加密
- 密码学散列函数
- 密钥管理
- 密码系统 - 密钥生成
- 密码系统 - 密钥存储
- 密码系统 - 密钥分发
- 密码系统 - 密钥撤销
- 分组密码
- 密码系统 - 流密码
- 密码学 - 分组密码
- 密码学 - 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 密码
- 密码学 - 对称加密与非对称加密
- 密码学有用资源
- 密码学 - 快速指南
- 密码学 - 讨论
密码学 - 简单替换密码的加密
在上一章中,我们了解了简单替换密码究竟是什么,它是如何工作的,基本实现及其缺点。现在我们将学习使用 Python、Java 和 C++ 实现简单替换密码的不同方法。
Python 实现
因此,我们可以使用不同的方法来实现简单替换密码的加密,例如:
使用指定的移位值
使用字典映射
直接使用 ASCII 值
因此,我们将在下面的章节中尝试详细介绍每种方法的概念,以便您可以更好地理解简单替换密码的加密。
方法 1:使用指定的移位值
在替换密码中,明文中的每个字母都会被替换为字母表中固定数量位置后的字母。这种方法的主要概念是,我们将使用指定的移位值对明文进行加密。移位值是字母表中每个字母应移动的位置数。
以下是使用 Python 实现此方法:
示例
# encryption function
def substitution_encrypt(plain_text, shift):
encrypted_text = ""
for char in plain_text:
# if the character is a letter
if char.isalpha():
# find the ASCII value of the character
ascii_val = ord(char)
# if the character is uppercase or lowercase
if char.isupper():
# shift the character within the range (65-90)
shifted_ascii = ((ascii_val - 65 + shift) % 26) + 65
else:
# shift the character within the range (97-122)
shifted_ascii = ((ascii_val - 97 + shift) % 26) + 97
# change back to a character
encrypted_char = chr(shifted_ascii)
encrypted_text += encrypted_char
else:
encrypted_text += char
return encrypted_text
#our plain text example
plaintext = "Hello, everyone!"
shift = 5
encrypted_text = substitution_encrypt(plaintext, shift)
print("Plaintext: ", plaintext)
print("Encrypted text:", encrypted_text)
以下是上述示例的输出:
输入/输出
Plaintext: Hello, everyone! Encrypted text: Mjqqt, jajwdtsj!
方法 2:使用字典映射
现在我们将使用字典映射来实现简单替换密码的加密。因此,我们创建一个“映射”或“字典”,借助它我们将字母表中的每个字母与另一个字母匹配。例如,'a' 可以映射到 'b','b' 可以映射到 'c',依此类推。然后,我们将使用此映射将明文中的每个字母替换为其相应的映射字母。如果映射中找不到字母(例如标点符号或数字),我们将保持不变。
以下是使用字典映射实现简单替换密码加密的 Python 代码。请参见下面的程序:
示例
# encryption function
def substitution_encrypt(plain_text, key):
# a dictionary mapping for substitution
mapping = {chr(97 + i): key[i] for i in range(26)}
# Encrypt the plaintext
encrypted_text = ''.join(mapping.get(char.lower(), char) for char in plain_text)
return encrypted_text
# our plain text example
plaintext = "Hello, dear friend!"
key = "bcdefghijklmnopqrstuvwxyza" # substitution key
encrypted_text = substitution_encrypt(plaintext, key)
print("Plaintext: ", plaintext)
print("Encrypted text:", encrypted_text)
以下是上述示例的输出:
输入/输出
Plaintext: Hello, dear friend! Encrypted text: ifmmp, efbs gsjfoe!
方法 3:直接使用 ASCII 值
您可能知道,每个字符或字母在计算机中都有一个与其相关的唯一数字,称为“ASCII 值”。在这种方法中,我们将直接操作这些 ASCII 值以移动明文中的每个字母。例如,'a' 的 ASCII 值可能为 97,因此如果我们将它移动 5 位,它将变为 102,即字母 'f'。
因此,以下是使用上述方法实现简单替换密码加密的 Python 代码:
示例
# encryption function
def substitution_encrypt(plain_text, shift):
encrypted_text = ""
for char in plain_text:
if char.isalpha():
# use ASCII values directly to shift characters
ascii_val = ord(char)
shifted_ascii = ascii_val + shift
if char.isupper():
if shifted_ascii > 90:
shifted_ascii -= 26
elif shifted_ascii < 65:
shifted_ascii += 26
elif char.islower():
if shifted_ascii > 122:
shifted_ascii -= 26
elif shifted_ascii < 97:
shifted_ascii += 26
encrypted_text += chr(shifted_ascii)
else:
encrypted_text += char
return encrypted_text
# function execution
plaintext = "Hello, my dear colleague!"
shift = 3
encrypted_text = substitution_encrypt(plaintext, shift)
print("PlainText: ", plaintext)
print("Encrypted text:", encrypted_text)
以下是上述示例的输出:
输入/输出
PlainText: Hello, my dear colleague! Encrypted text: Khoor, pb ghdu froohdjxh!
Java 实现
在这个 Java 实现中,我们将创建一个简单的替换密码算法。在这里,我们使用一个概念,其中我们必须将明文中的每个字母替换为加密字母表中的相应字母。我们将借助 HashMap 创建常规字母表和加密字母表之间的映射。然后,对于明文中的每个字母,我们将查看其相应的加密字母,并将其添加到密文中。如果字符不在映射中(例如,标点符号或空格),我们将保持其在密文中不变。
示例
因此,使用 Java 实现简单替换密码如下:
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<>();
static {
for (int i = 0; i < alphabet.length(); i++) {
encryptionMap.put(alphabet.charAt(i), encrypted_alphabet.charAt(i));
}
}
public static String encrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder();
for (char c : plaintext.toLowerCase().toCharArray()) {
if (encryptionMap.containsKey(c)) {
ciphertext.append(encryptionMap.get(c));
} else {
ciphertext.append(c);
}
}
return ciphertext.toString();
}
public static void main(String[] args) {
String plaintext = "Hello Tutorialspoint";
String encryptedText = encrypt(plaintext);
System.out.println("Our Plaintext: " + plaintext);
System.out.println("The Encrypted text: " + encryptedText);
}
}
以下是上述示例的输出:
输入/输出
Our Plaintext: Hello Tutorialspoint The Encrypted text: ifmmp uvupsjbmtqpjou
C++ 实现
这是使用 C++ 实现简单替换密码的代码。因此,我们将使用与上述实现中相同的方法。
示例
使用 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;
public:
SSC() {
for (int i = 0; i < alphabet.length(); i++) {
encryptionMap[alphabet[i]] = encrypted_alphabet[i];
}
}
string encrypt(string plaintext) {
string ciphertext = "";
for (char c : plaintext) {
if (encryptionMap.find(tolower(c)) != encryptionMap.end()) {
ciphertext += encryptionMap[tolower(c)];
} else {
ciphertext += c;
}
}
return ciphertext;
}
};
int main() {
SSC cipher;
string plaintext = "hello there how are you!";
string encryptedText = cipher.encrypt(plaintext);
cout << "Our Plaintext: " << plaintext << endl;
cout << "The Encrypted text: " << encryptedText << endl;
return 0;
}
以下是上述示例的输出:
输入/输出
Our Plaintext: hello there how are you! The Encrypted text: ifmmp uifsf ipx bsf zpv!
总结
本章讨论了实现简单替换密码加密的不同方法。这些方法提供了多种方法来在 Python、Java 和 C++ 中实现简单替换密码加密,每种方法都有其自身的优势和实现细节。