根据凯撒密码\Playfair密码的原理使用c++写代码,对输入的符号串能够根据设置的密钥分别正确实现加密和解密功能
时间: 2023-05-22 22:02:48 浏览: 188
凯撒密码的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);
}
```
阅读全文