密码学 - 凯撒密码



下一个密码算法是凯撒密码。本章我们将了解凯撒密码究竟是什么,它是如何工作的,以及它与不同技术的实现。让我们深入研究一下。

什么是凯撒密码?

凯撒密码算法是一种简单易用的加密技术。它是一种简单的替换密码,其中字母通过使用选定的位数来移动以创建编码的消息。使用位移为 2 的凯撒密码,A 可以编码为 C,M 为 O,Z 为 B,依此类推。此技术以罗马领导人朱利叶斯·凯撒的名字命名。它用于他的私人信件中。它是加密消息最简单和最古老的方法之一。

Caesar cipher

算法

以下是凯撒密码的加密和解密算法:

加密算法

对于加密算法,步骤如下:

  • 选择一个数字作为你的“位移”值。此数字决定每个字母在字母表中移动多少。

  • 从你的消息开始。查看消息中的每个字母。

  • 将每个字母在字母表中向前移动选择的位移值。例如,如果位移值为 3,则“A”将变为“D”,“M”将变为“P”,依此类推。

  • 保存新字母而不是旧字母。

  • 对消息中的每个字母都这样做。

解密算法

对于解密算法,请参见以下步骤:

  • 从加密的消息开始。

  • 知道用于加密的位移值。

  • 查看加密消息中的每个字母。
  • 将每个字母在字母表中向后移动位移值以将其解密。例如,如果位移值为 3,则“D”将变为“A”,“P”将变为“M”,依此类推。

  • 保存解密的字母而不是加密的字母。

  • 对加密消息中的所有字母都这样做。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

使用 Python 实现

因此,使用各种 Python 模块和方法,我们可以通过多种方式实现此算法。在下面的部分中,让我们分别探讨每种方法:

  • 使用字符串模块

  • 使用列表推导式

使用字符串模块

在这种方法中,我们将使用 Python 的字符串模块。此模块用于处理字符串。此模块具有一些常量、实用程序函数和用于字符串操作的类。由于它是一个内置模块,因此我们必须在使用其任何常量和类之前导入它。

因此,字符串模块用于访问小写字母并执行创建凯撒密码算法所需的字符串操作。

加密示例

以下是使用 Python 字符串模块实现凯撒密码的示例:

Open Compiler
import string def caesar_cipher(text, shift): letters = string.ascii_lowercase shifted_letters = letters[shift:] + letters[:shift] table = str.maketrans(letters, shifted_letters) return text.translate(table) # function execution message = "hello" shift_value = 3 encrypted_msg = caesar_cipher(message, shift_value) print("Encrypted message:", encrypted_msg)

以下是上述示例的输出:

输入/输出
I/P -> Plain text : hello
O/P -> Encrypted message: khoor

解密示例

要解密上述文本消息,我们可以在 Python 中使用以下代码:

def caesar_decipher(text, shift): # Decryption is just like encryption but with a negative shift return caesar_cipher(text, -shift) # Decryption encrypted_msg = khoor decrypted_msg = caesar_decipher(encrypted_msg, shift_value) print("Decrypted message:", decrypted_msg)

以下是上述示例的输出:

输入/输出
I/P -> Cipher Text: khoor
O/P -> Decrypted message: hello

使用列表推导式

现在,我们将使用列表推导式通过迭代输入文本中的每个人来创建一个新字符串。在列表中,有一个条件表达式检查每个字母是大写、小写还是非字母。我们将基本上加密输入文本中的每个字母,并将非字母字符原样保留。

加密示例

以下是使用 Python 中的列表推导式实现凯撒密码算法的示例:

Open Compiler
def caesar_cipher(text, shift): encrypted_text = '' for char in text: if 'A' <= char <= 'Z': encrypted_text += chr((ord(char) - 65 + shift) % 26 + 65) elif 'a' <= char <= 'z': encrypted_text += chr((ord(char) - 97 + shift) % 26 + 97) else: encrypted_text += char return encrypted_text # function execution message = "hello everyone" shift_value = 3 encrypted_msg = caesar_cipher(message, shift_value) print("Encrypted message:", encrypted_msg)

以下是上述示例的输出:

输入/输出
I/P -> Plain text: hello everyone
O/P -> Encrypted message: khoor hyhubrqh

解密示例

要为凯撒密码加密消息创建解密程序,我们可以反转加密过程。以下是使用列表推导式实现上述凯撒密码加密函数的解密代码:

Open Compiler
def caesar_decipher(text, shift): decrypted_text = '' for char in text: if 'A' <= char <= 'Z': decrypted_text += chr((ord(char) - 65 - shift) % 26 + 65) elif 'a' <= char <= 'z': decrypted_text += chr((ord(char) - 97 - shift) % 26 + 97) else: decrypted_text += char return decrypted_text # Function execution encrypted_msg = "khoor hyhubrqh" shift_value = 3 # Decryption decrypted_msg = caesar_decipher(encrypted_msg, shift_value) print("Decrypted message:", decrypted_msg)

以下是上述示例的输出:

输入/输出
I/P -> Cipher text: khoor hyhubrqh
O/P -> Decrypted message: hello everyone

使用 C++ 实现

此方法将消息和位移值作为输入。然后,它迭代消息中的每个字符并将其按给定数量移动以创建凯撒密码算法。它将加密的消息作为字符串返回。因此,以下是使用 C++ 编程语言实现凯撒密码的示例:

示例

Open Compiler
#include <iostream> #include <string> using namespace std; // Function to encrypt a message string encrypt(string message, int shift) { string encrypted = ""; for (char& c : message) { // Shift each character by the given spaces if (isalpha(c)) { if (isupper(c)) { encrypted += char(int(c + shift - 65) % 26 + 65); } else { encrypted += char(int(c + shift - 97) % 26 + 97); } } else { encrypted += c; // Keep non-alphabetic characters unchanged } } return encrypted; } int main() { string message = "Hello, Tutorialspoint!"; int shift = 3; string encrypted_msg = encrypt(message, shift); cout << "The Original Message: " << message << endl; cout << "The Encrypted Message: " << encrypted_msg << endl; return 0; }

以下是上述示例的输出:

输入/输出

The Original Message: Hello, Tutorialspoint!
The Encrypted Message: Khoor, Wxwruldovsrlqw!

使用 Java 实现

此方法的输入是位移值和明文消息。然后,借助凯撒密码方法,我们将迭代地将消息中的每个字符按给定数量移动。加密的消息作为字符串返回。因此,Java 中凯撒密码的实现如下:

示例

Open Compiler
public class CaesarCipherClass { // Method to encrypt a message public static String encryptFunc(String message, int shift) { StringBuilder encrypted = new StringBuilder(); for (char c : message.toCharArray()) { // Shift each character by the given amount if (Character.isLetter(c)) { char base = Character.isUpperCase(c) ? 'A' : 'a'; encrypted.append((char) ((c - base + shift) % 26 + base)); } else { encrypted.append(c); // Keep non-alphabetic characters unchanged } } return encrypted.toString(); } public static void main(String[] args) { String message = "Hello, Everyone there!"; int shift = 3; String encryptedMsg = encryptFunc(message, shift); System.out.println("The Original Message: " + message); System.out.println("The Encrypted Message: " + encryptedMsg); } }

以下是上述示例的输出:

输入/输出

The Original Message: Hello, Everyone there!
The Encrypted Message: Khoor, Hyhubrqh wkhuh!

凯撒密码的特性

  • 凯撒密码是一种简单且古老的加密方法。它使用一种技术,我们将明文中的每个字母按固定的位置数移动以生成密文。

  • 使用编程语言中的基本字符串操作和模运算,很容易实现凯撒密码。

  • 可以通过更改位移值轻松自定义它,并且它允许不同级别的加密。

  • 使用凯撒密码的加密和解密速度快且效率高,但仅适用于短消息。

  • 由于英语字母表中只有 26 个字母,因此凯撒密码的密钥大小很小,最多只有 26 个可能的密钥。

凯撒密码的缺点

  • 凯撒密码的安全性非常弱,因为它只有 26 个可能的密钥。因此,这使得黑客很容易尝试所有选项并解密消息。

  • 固定的字母大小也使其容易受到频率分析攻击,在频率分析攻击中,黑客使用字母的常见性来猜测密钥。

  • 对密文的修改也可能未被检测到,因为它缺乏身份验证。

由于这些弱点,它不适合现代加密要求。

总结

凯撒密码是一种简单的隐藏信息的方法。它通过使用固定的位移数量来移动消息中的每个字母。使用它时,我们可以选择一个位移数字,并将每个字母移动该数字来加密消息。但它并不安全,因为只有26个可能的密钥,攻击者很容易猜测出密码。因为他们可以使用字母频率来猜测实际的字母。因此,出于这个原因,它现在已经不再使用了。

广告