【C语言】单表交换密码的加密解密

单表交换密码是一种简单但不够安全的加密方法,它适用于对简单文本进行加密,但不适用于对于保密性要求较高的数据。使用时需要注意,密钥(即密钥表)应为字母表的一个排列,它将明文中的每个字母映射到一个不同的字母上。用户输入密钥时,应保证其长度为26,并且包含字母表的每个字母,且每个字母仅出现一次。在实际应用中,应使用更为复杂和安全的加密算法来保护数据的安全 

解题思路:

先接收用户输入的明文和密钥表,创建一个加密函数monoalphabetic_encrypt,该函数有两个参数:plainText是待解密的密文字符串,key是密钥表。加密思路为:用户输入明文和加密密钥,密钥是一个字母表的排列-->根据密钥,将明文中的每个字母替换为密钥表中对应位置的字母-->加密完成后,输出密文。

#include <stdio.h>
#include <string.h>

// 单表交换加密函数
void monoalphabetic_encrypt(char *plaintext, char *key) {
    int i;
    for (i = 0; i < strlen(plaintext); i++) {
        // 加密大写字母
        if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
            plaintext[i] = key[plaintext[i] - 'A'];
        }
        // 加密小写字母
        else if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {
            plaintext[i] = tolower(key[plaintext[i] - 'a']);
        }
    }
}

// 单表交换解密函数
void monoalphabetic_decrypt(char *ciphertext, char *key) {
    int i;
    for (i = 0; i < strlen(ciphertext); i++) {
        // 解密大写字母
        if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') {
            int j;
            for (j = 0; j < 26; j++) {
                if (key[j] == ciphertext[i]) {
                    ciphertext[i] = 'A' + j;
                    break;
                }
            }
        }
        // 解密小写字母
        else if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') {
            int j;
            for (j = 0; j < 26; j++) {
                if (tolower(key[j]) == ciphertext[i]) {
                    ciphertext[i] = 'a' + j;
                    break;
                }
            }
        }
    }
}

int main() {
    char plaintext[100], ciphertext[100], key[26];
    int i;

    printf("Enter the plaintext: ");
    fgets(plaintext, sizeof(plaintext), stdin);

    printf("Enter the key (a permutation of the alphabet): ");
    fgets(key, sizeof(key), stdin);

    // 去除密钥中的换行符
    key[strcspn(key, "\n")] = '\0';

    // 加密过程
    strcpy(ciphertext, plaintext);
    monoalphabetic_encrypt(ciphertext, key);
    printf("Encrypted text: %s\n", ciphertext);

    // 解密过程
    monoalphabetic_decrypt(ciphertext, key);
    printf("Decrypted text: %s\n", ciphertext);

    return 0;
}

相关推荐

  1. C语言交换密码加密解密

    2024-04-05 10:32:04       35 阅读
  2. C语言】对称密码——栅栏加密解密

    2024-04-05 10:32:04       40 阅读
  3. 基于通讯录C语言实现

    2024-04-05 10:32:04       45 阅读
  4. 排序,使用冒泡排序【c语言

    2024-04-05 10:32:04       38 阅读
  5. C语言实现

    2024-04-05 10:32:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 10:32:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 10:32:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 10:32:04       87 阅读
  4. Python语言-面向对象

    2024-04-05 10:32:04       96 阅读

热门阅读

  1. C语言关于随机数知识点的总结

    2024-04-05 10:32:04       35 阅读
  2. 01 计算机网络发展与分类

    2024-04-05 10:32:04       31 阅读
  3. 快速排序和归并排序(递归实现)

    2024-04-05 10:32:04       39 阅读
  4. 如何正确使用reflect:Go反射规范与最佳实践

    2024-04-05 10:32:04       35 阅读
  5. VUE实现下一页的功能

    2024-04-05 10:32:04       33 阅读
  6. 使用generator实现async函数

    2024-04-05 10:32:04       31 阅读
  7. go中的常用的关键字

    2024-04-05 10:32:04       32 阅读
  8. Linux系统下tomcat服务自动重启

    2024-04-05 10:32:04       34 阅读