凯撒密码加密没考虑大写字母的问题,参考实例6的思路,完善恺撒密码,实现对英文大小写字母都可以加密。 要求 算法改为顺序加5; 明文如果为小写字母要转换为大写字母,即a加密之后转换为F;反之,A转换之后为f。
时间: 2024-06-12 11:07:27 浏览: 18
```python
def caesar_cipher(text):
result = ''
for char in text:
if char.isalpha():
if char.isupper():
result += chr((ord(char) + 5 - 65) % 26 + 65).lower()
else:
result += chr((ord(char) + 5 - 97) % 26 + 97).upper()
else:
result += char
return result
```
解析:
- `char.isalpha()` 判断字符是否为字母;
- `char.isupper()` 判断字符是否为大写字母;
- `chr()` 将 ASCII 码转换为字符;
- `ord()` 将字符转换为 ASCII 码;
- `%` 取模运算,保证结果在 0~25 的范围内;
- 65 和 97 分别为大写字母 A 和小写字母 a 的 ASCII 码;
- `lower()` 和 `upper()` 分别将字符转换为小写和大写字母。
相关问题
凯撒密码加密没考虑大写字母的问题,所以用python 写一段完善恺撒密码,实现对英文大小写字母都可以加密的问题
好的,非常感谢您的提问。以下是一个简单的 Python 代码实现,它可以完善凯撒密码,使其能够支持加密英文大小写字母:
```python
def encrypt_caesar(plain_text, shift):
cipher_text = ''
for i in plain_text:
if i.isalpha():
if i.islower():
index = (ord(i) - 97 + shift) % 26 + 97
cipher_text += chr(index)
else:
index = (ord(i) - 65 + shift) % 26 + 65
cipher_text += chr(index)
else:
cipher_text += i
return cipher_text
```
这个函数接受两个参数:要加密的明文字符串 `plain_text` 和一个加密的偏移量 `shift`。它会遍历明文字符串中的每个字符,如果是英文字母,则根据其是否大写来确定 ASCII 码的范围,并使用偏移量进行加密。否则,直接将该字符添加到密文字符串中。
您可以按照以下方式调用该函数:
```python
plaintext = 'This is a secret message.'
shift = 3
ciphertext = encrypt_caesar(plaintext, shift)
print(ciphertext)
```
上面的代码将输出:
```
Wklv lv d vhfuhw phvvdjh.
```
这就是使用 Caesar 密码加密后的密文。要解密此消息,请将偏移量变为负数即可。
c语言凯撒密码、字母倒排序、单表置换密码、维吉利亚密码、转换加密方法实现加密解密
1. 凯撒密码(Caesar Cipher)
凯撒密码是一种简单的替换加密方法,是最古老的加密技术之一。它是通过将字母按照一定的偏移量进行替换来实现加密的。例如,将字母表中的每个字母向右偏移3个位置,A变成D,B变成E,以此类推,最终得到一个新的密文。
加密方法:
```c
void caesar_encrypt(char *plaintext, int shift) {
int i = 0;
while (plaintext[i] != '\0') {
if (isalpha(plaintext[i])) {
if (isupper(plaintext[i]))
plaintext[i] = ((plaintext[i] - 'A' + shift) % 26) + 'A';
else
plaintext[i] = ((plaintext[i] - 'a' + shift) % 26) + 'a';
}
i++;
}
}
```
解密方法:
```c
void caesar_decrypt(char *ciphertext, int shift) {
int i = 0;
while (ciphertext[i] != '\0') {
if (isalpha(ciphertext[i])) {
if (isupper(ciphertext[i]))
ciphertext[i] = ((ciphertext[i] - 'A' - shift + 26) % 26) + 'A';
else
ciphertext[i] = ((ciphertext[i] - 'a' - shift + 26) % 26) + 'a';
}
i++;
}
}
```
2. 字母倒排序(Reverse Alphabet Cipher)
字母倒排序是一种替换加密方法,它通过将字母表中的每个字母倒序排列,然后将明文中的每个字母替换成其对应的倒序字母,从而实现加密。例如,将字母A替换成Z,将B替换成Y,以此类推。
加密方法:
```c
void reverse_alphabet_encrypt(char *plaintext) {
int i = 0;
while (plaintext[i] != '\0') {
if (isalpha(plaintext[i])) {
if (isupper(plaintext[i]))
plaintext[i] = 'Z' - (plaintext[i] - 'A');
else
plaintext[i] = 'z' - (plaintext[i] - 'a');
}
i++;
}
}
```
解密方法:
```c
void reverse_alphabet_decrypt(char *ciphertext) {
int i = 0;
while (ciphertext[i] != '\0') {
if (isalpha(ciphertext[i])) {
if (isupper(ciphertext[i]))
ciphertext[i] = 'A' + ('Z' - ciphertext[i]);
else
ciphertext[i] = 'a' + ('z' - ciphertext[i]);
}
i++;
}
}
```
3. 单表置换密码(Monoalphabetic Substitution Cipher)
单表置换密码是一种替换加密方法,它通过将明文中的每个字母替换成一个固定的密文字母,从而实现加密。例如,将字母A替换成密文字母X,将B替换成密文字母Y,以此类推。
加密方法:
```c
void monoalphabetic_encrypt(char *plaintext, char *key) {
int i = 0;
while (plaintext[i] != '\0') {
if (isalpha(plaintext[i])) {
if (isupper(plaintext[i]))
plaintext[i] = toupper(key[plaintext[i] - 'A']);
else
plaintext[i] = tolower(key[plaintext[i] - 'a']);
}
i++;
}
}
```
解密方法:
```c
void monoalphabetic_decrypt(char *ciphertext, char *key) {
int i = 0;
while (ciphertext[i] != '\0') {
if (isalpha(ciphertext[i])) {
int j = 0;
if (isupper(ciphertext[i])) {
while (toupper(key[j]) != ciphertext[i])
j++;
ciphertext[i] = 'A' + j;
}
else {
while (tolower(key[j]) != ciphertext[i])
j++;
ciphertext[i] = 'a' + j;
}
}
i++;
}
}
```
4. 维吉利亚密码(Vigenère Cipher)
维吉利亚密码是一种多表置换加密方法,它通过使用一系列不同的单表置换密码来实现加密。具体地,将明文中的每个字母按照一个密钥字符串中的字母进行偏移,然后使用对应位置的单表置换密码进行替换,最终得到一个新的密文。
加密方法:
```c
void vigenere_encrypt(char *plaintext, char *key) {
int i = 0;
int j = 0;
int key_len = strlen(key);
while (plaintext[i] != '\0') {
if (isalpha(plaintext[i])) {
int shift;
if (isupper(key[j % key_len]))
shift = key[j % key_len] - 'A';
else
shift = key[j % key_len] - 'a';
if (isupper(plaintext[i]))
plaintext[i] = ((plaintext[i] - 'A' + shift) % 26) + 'A';
else
plaintext[i] = ((plaintext[i] - 'a' + shift) % 26) + 'a';
j++;
}
i++;
}
}
```
解密方法:
```c
void vigenere_decrypt(char *ciphertext, char *key) {
int i = 0;
int j = 0;
int key_len = strlen(key);
while (ciphertext[i] != '\0') {
if (isalpha(ciphertext[i])) {
int shift;
if (isupper(key[j % key_len]))
shift = key[j % key_len] - 'A';
else
shift = key[j % key_len] - 'a';
if (isupper(ciphertext[i]))
ciphertext[i] = ((ciphertext[i] - 'A' - shift + 26) % 26) + 'A';
else
ciphertext[i] = ((ciphertext[i] - 'a' - shift + 26) % 26) + 'a';
j++;
}
i++;
}
}
```
5. 转换加密方法(Transposition Cipher)
转换加密方法是一种基于重新排列明文中的字符来实现加密的方法。具体地,将明文中的字符按照一定的规则重新排列,然后得到一个新的密文。
加密方法:
```c
void transposition_encrypt(char *plaintext, char *key) {
int i, j, k, len;
int key_len = strlen(key);
char **matrix;
// 计算矩阵的行数
len = strlen(plaintext);
i = len / key_len;
if (len % key_len != 0)
i++;
// 分配矩阵的内存
matrix = (char **)malloc(i * sizeof(char *));
for (j = 0; j < i; j++)
matrix[j] = (char *)malloc(key_len * sizeof(char));
// 将明文填充进矩阵
k = 0;
for (j = 0; j < i; j++) {
for (k = 0; k < key_len; k++) {
if (plaintext[j * key_len + k] == '\0')
matrix[j][k] = ' ';
else
matrix[j][k] = plaintext[j * key_len + k];
}
}
// 对矩阵的每一列进行置换
for (k = 0; k < key_len; k++) {
int index = strchr(key, key[k]) - key;
for (j = 0; j < i; j++) {
char tmp = matrix[j][k];
matrix[j][k] = matrix[j][index];
matrix[j][index] = tmp;
}
}
// 将矩阵中的字符按行顺序输出
k = 0;
for (j = 0; j < i; j++) {
for (k = 0; k < key_len; k++)
printf("%c", matrix[j][k]);
}
}
```
解密方法:
```c
void transposition_decrypt(char *ciphertext, char *key) {
int i, j, k, len;
int key_len = strlen(key);
char **matrix;
// 计算矩阵的行数
len = strlen(ciphertext);
i = len / key_len;
// 分配矩阵的内存
matrix = (char **)malloc(i * sizeof(char *));
for (j = 0; j < i; j++)
matrix[j] = (char *)malloc(key_len * sizeof(char));
// 将密文填充进矩阵
k = 0;
for (j = 0; j < i; j++) {
for (k = 0; k < key_len; k++)
matrix[j][k] = ciphertext[j * key_len + k];
}
// 对矩阵的每一列进行置换
for (k = key_len - 1; k >= 0; k--) {
int index = strchr(key, key[k]) - key;
for (j = 0; j < i; j++) {
char tmp = matrix[j][k];
matrix[j][k] = matrix[j][index];
matrix[j][index] = tmp;
}
}
// 将矩阵中的字符按列顺序输出
k = 0;
for (k = 0; k < key_len; k++) {
for (j = 0; j < i; j++)
printf("%c", matrix[j][k]);
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)