根据凯撒密码\Playfair密码的原理使用c++编写程序,对输入的符号串能够根据设置的密钥分别正确实现加密和解密功能。
时间: 2023-05-22 14:02:44 浏览: 55
非常感谢您的问题。根据凯撒密码和Playfair密码的原理,使用C编写程序可以对输入的符号串进行加密和解密。该程序需要设置正确的密钥,才能实现正确的加密和解密功能。如果您有特定的加密或解密算法要求,可以提供更多细节,以便我更好地回答您的问题。
相关问题
根据凯撒密码\Playfair密码的原理使用c++写代码,对输入的符号串能够根据设置的密钥分别正确实现加密和解密功能
以下是凯撒密码的实现代码,用于加密和解密一个输入字符串,使用给定的密钥:
```c
#include <stdio.h>
void caesar_encrypt(char plaintext[], int shift);
void caesar_decrypt(char ciphertext[], int shift);
int main() {
char message[100];
int key;
printf("Enter message: ");
fgets(message, sizeof(message), stdin);
printf("Enter key: ");
scanf("%d", &key);
caesar_encrypt(message, key);
printf("Encrypted message: %s\n", message);
caesar_decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}
void caesar_encrypt(char plaintext[], int shift)
{
char c;
int i;
for (i = 0; plaintext[i] != '\0'; i++) {
c = plaintext[i];
if (c >= 'a' && c <= 'z') {
c = (c - 'a' + shift) % 26 + 'a';
} else if (c >= 'A' && c <= 'Z') {
c = (c - 'A' + shift) % 26 + 'A';
}
plaintext[i] = c;
}
}
void caesar_decrypt(char ciphertext[], int shift)
{
caesar_encrypt(ciphertext, 26 - shift);
}
```
以下是 Playfair 密码的实现代码。它使用一个 5x5 的矩阵,该矩阵由一个关键词和其他字母组成,其中 J 被忽略:
```c
#include <stdio.h>
#include <string.h>
#define MATRIX_DIM 5
#define MATRIX_SIZE (MATRIX_DIM * MATRIX_DIM)
#define ENCRYPT 1
#define DECRYPT -1
void playfair_encrypt(char plaintext[], char key[]);
void playfair_decrypt(char ciphertext[], char key[]);
int main() {
char message[100];
char key[100];
printf("Enter message: ");
fgets(message, sizeof(message), stdin);
printf("Enter key: ");
fgets(key, sizeof(key), stdin);
playfair_encrypt(message, key);
printf("Encrypted message: %s\n", message);
playfair_decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}
char* remove_duplicates(char s[]) {
int len = strlen(s);
char *result = malloc(len + 1);
memset(result, '\0', len+1);
int i = 0;
while (i < len) {
int j = i+1;
while (j < len) {
if (s[j] == s[i]) {
break;
}
j++;
}
if (j == len) {
strncat(result, &s[i], 1);
}
i++;
}
return result;
}
void fill_matrix(char key[], char matrix[]) {
const char* alphabet = "abcdefghiklmnopqrstuvwxyz";
int len = strlen(key);
// Remove duplicate letters from key
char* no_duplicates = remove_duplicates(key);
len = strlen(no_duplicates);
// Fill key into matrix
int i;
for (i = 0; i < len; i++) {
matrix[i] = no_duplicates[i];
}
// Fill remaining letters from alphabet
int j;
for (i = len, j = 0; j < 25 - len; j++) {
if (strchr(matrix, alphabet[j]) == NULL) {
matrix[i] = alphabet[j];
i++;
}
}
free(no_duplicates);
}
void print_matrix(char matrix[]) {
int i;
for (i = 0; i < MATRIX_SIZE; i++) {
printf("%c ", matrix[i]);
if ((i+1) % MATRIX_DIM == 0) {
printf("\n");
}
}
}
void get_positions(char c, char matrix[], int positions[]) {
int i;
for (i = 0; i < MATRIX_SIZE; i++) {
if (matrix[i] == c) {
positions[0] = i % MATRIX_DIM;
positions[1] = i / MATRIX_DIM;
return;
}
}
// Letter not in matrix
positions[0] = -1;
positions[1] = -1;
}
void playfair_transform(char plaintext[], int direction, char key[]) {
char matrix[MATRIX_SIZE];
fill_matrix(key, matrix);
print_matrix(matrix);
char c1, c2;
int p1[2], p2[2], i;
for (i = 0; plaintext[i] != '\0'; i++) {
c2 = plaintext[i];
// Ignore J
if (c2 == 'j') {
c2 = 'i';
}
if (c2 >= 'a' && c2 <= 'z') {
if (i > 0 && c2 == c1) {
// Special case: replace last character if it is the same
if ((i+1) % 2 == 0) {
c1 = plaintext[i-1];
} else {
c1 = plaintext[i+1];
}
c2 = 'x';
}
c1 = plaintext[i-1];
if (i == 0 || c1 == 'j') {
c1 = c2;
}
// Ignore J
if (c1 == 'j') {
c1 = 'i';
}
get_positions(c1, matrix, p1);
get_positions(c2, matrix, p2);
if (p1[0] == -1 || p2[0] == -1) {
// Letter not in matrix
plaintext[i] = '?';
} else {
if (p1[0] == p2[0]) {
// Same column, shift down or up
p1[1] = (p1[1] + direction) % MATRIX_DIM;
p2[1] = (p2[1] + direction) % MATRIX_DIM;
} else if (p1[1] == p2[1]) {
// Same row, shift left or right
p1[0] = (p1[0] + direction) % MATRIX_DIM;
p2[0] = (p2[0] + direction) % MATRIX_DIM;
} else {
// Rectangle, switch rows
int tmp = p1[0];
p1[0] = p2[0];
p2[0] = tmp;
}
plaintext[i-1] = matrix[p1[1]*MATRIX_DIM + p1[0]];
plaintext[i] = matrix[p2[1]*MATRIX_DIM + p2[0]];
}
}
}
}
void playfair_encrypt(char plaintext[], char key[]) {
playfair_transform(plaintext, ENCRYPT, key);
}
void playfair_decrypt(char ciphertext[], char key[]) {
playfair_transform(ciphertext, DECRYPT, key);
}
```
根据凯撒密码\Playfair密码的原理写代码,对输入的符号串能够根据设置的密钥分别正确实现加密和解密功能
非常感谢您的提问,我可以为您提供以下代码实现凯撒密码和Playfair密码的加密和解密功能:
凯撒密码:
```python
def caesar_cipher(plain_text, key):
cipher_text = ""
for ch in plain_text:
if ch.isalpha():
new_ch = chr((ord(ch) - ord('a') + key) % 26 + ord('a'))
cipher_text += new_ch
else:
cipher_text += ch
return cipher_text
def caesar_decipher(cipher_text, key):
return caesar_cipher(cipher_text, 26 - key)
```
Playfair密码:
```python
def gen_playfair_table(key):
key = key.lower().replace(" ", "")
key_set = set(key)
alphabet = "".join([chr(ord('a') + i) for i in range(26)])
table_list = []
for ch in key:
if ch in alphabet:
alphabet = alphabet.replace(ch, "")
table_list.append(ch)
for ch in alphabet:
table_list.append(ch)
table_str = "".join(table_list)
table = []
for i in range(5):
row = table_str[i * 5 : (i + 1) * 5]
table.append(row)
return table
def playfair_cipher(plain_text, key):
table = gen_playfair_table(key)
cipher_text = ""
plain_text = plain_text.lower().replace(" ", "")
if len(plain_text) % 2 == 1:
plain_text += "x"
for i in range(0, len(plain_text), 2):
ch1, ch2 = plain_text[i], plain_text[i + 1]
row1, col1 = 0, 0
row2, col2 = 0, 0
for j in range(5):
if ch1 in table[j]:
row1, col1 = j, table[j].find(ch1)
if ch2 in table[j]:
row2, col2 = j, table[j].find(ch2)
if row1 == row2:
cipher_ch1 = table[row1][(col1 + 1) % 5]
cipher_ch2 = table[row2][(col2 + 1) % 5]
elif col1 == col2:
cipher_ch1 = table[(row1 + 1) % 5][col1]
cipher_ch2 = table[(row2 + 1) % 5][col2]
else:
cipher_ch1 = table[row1][col2]
cipher_ch2 = table[row2][col1]
cipher_text += cipher_ch1 + cipher_ch2
return cipher_text
def playfair_decipher(cipher_text, key):
table = gen_playfair_table(key)
plain_text = ""
cipher_text = cipher_text.lower().replace(" ", "")
for i in range(0, len(cipher_text), 2):
ch1, ch2 = cipher_text[i], cipher_text[i + 1]
row1, col1 = 0, 0
row2, col2 = 0, 0
for j in range(5):
if ch1 in table[j]:
row1, col1 = j, table[j].find(ch1)
if ch2 in table[j]:
row2, col2 = j, table[j].find(ch2)
if row1 == row2:
plain_ch1 = table[row1][(col1 - 1) % 5]
plain_ch2 = table[row2][(col2 - 1) % 5]
elif col1 == col2:
plain_ch1 = table[(row1 - 1) % 5][col1]
plain_ch2 = table[(row2 - 1) % 5][col2]
else:
plain_ch1 = table[row1][col2]
plain_ch2 = table[row2][col1]
plain_text += plain_ch1 + plain_ch2
return plain_text
```
请注意,这里的加密和解密函数使用了称之为“求模算术”的技巧,可以防止加密结果大于字母表中最后一个字母的 ASCII 码或小于字母表中第一个字母的 ASCII 码。同时,playfair_cipher 和 playfair_decipher 函数还处理了明文长度为奇数的情况,添加了一个"X"来填充。