C语言Vigenere密码如何实现与优化?

99ANYc3cd6
预计阅读时长 12 分钟
位置: 首页 C语言 正文

C语言实现维吉尼亚密码(Vigenère Cipher)

维吉尼亚密码是一种多表替换密码,使用一个关键词对明文进行加密和解密,下面是一个完整的C语言实现:

c语言vigenere
(图片来源网络,侵删)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// 函数声明
void vigenereEncrypt(const char *plaintext, const char *key, char *ciphertext);
void vigenereDecrypt(const char *ciphertext, const char *key, char *plaintext);
char charToShiftedChar(char c, char keyChar, int encrypt);
int main() {
    char plaintext[100], ciphertext[100], key[100], decrypted[100];
    printf("维吉尼亚密码实现\n");
    // 获取用户输入
    printf("请输入明文: ");
    fgets(plaintext, sizeof(plaintext), stdin);
    plaintext[strcspn(plaintext, "\n")] = '\0'; // 移除换行符
    printf("请输入密钥: ");
    fgets(key, sizeof(key), stdin);
    key[strcspn(key, "\n")] = '\0'; // 移除换行符
    // 加密
    vigenereEncrypt(plaintext, key, ciphertext);
    printf("加密结果: %s\n", ciphertext);
    // 解密
    vigenereDecrypt(ciphertext, key, decrypted);
    printf("解密结果: %s\n", decrypted);
    return 0;
}
// 加密函数
void vigenereEncrypt(const char *plaintext, const char *key, char *ciphertext) {
    int keyLen = strlen(key);
    int keyIndex = 0;
    for (int i = 0; plaintext[i] != '\0'; i++) {
        if (isalpha(plaintext[i])) {
            ciphertext[i] = charToShiftedChar(plaintext[i], key[keyIndex % keyLen], 1);
            keyIndex++;
        } else {
            ciphertext[i] = plaintext[i]; // 非字母字符保持不变
        }
    }
    ciphertext[strlen(plaintext)] = '\0';
}
// 解密函数
void vigenereDecrypt(const char *ciphertext, const char *key, char *plaintext) {
    int keyLen = strlen(key);
    int keyIndex = 0;
    for (int i = 0; ciphertext[i] != '\0'; i++) {
        if (isalpha(ciphertext[i])) {
            plaintext[i] = charToShiftedChar(ciphertext[i], key[keyIndex % keyLen], 0);
            keyIndex++;
        } else {
            plaintext[i] = ciphertext[i]; // 非字母字符保持不变
        }
    }
    plaintext[strlen(ciphertext)] = '\0';
}
// 核心字符转换函数
char charToShiftedChar(char c, char keyChar, int encrypt) {
    // 确定密钥字符的偏移量 (A=0, B=1, ..., Z=25)
    int keyShift = toupper(keyChar) - 'A';
    if (encrypt) {
        // 加密: (明文字母 + 密钥字母) mod 26
        if (isupper(c)) {
            return 'A' + (c - 'A' + keyShift) % 26;
        } else {
            return 'a' + (c - 'a' + keyShift) % 26;
        }
    } else {
        // 解密: (密文字母 - 密钥字母 + 26) mod 26
        if (isupper(c)) {
            return 'A' + (c - 'A' - keyShift + 26) % 26;
        } else {
            return 'a' + (c - 'a' - keyShift + 26) % 26;
        }
    }
}

代码说明

  1. 主要函数:

    • vigenereEncrypt(): 使用维吉尼亚密码加密明文
    • vigenereDecrypt(): 使用维吉尼亚密码解密密文
    • charToShiftedChar(): 核心字符转换函数,处理单个字符的加密/解密
  2. 工作原理:

    • 密钥重复使用,直到覆盖整个明文
    • 每个字母根据密钥字母进行偏移
    • 大小写字母分别处理
    • 非字母字符保持不变
  3. 使用示例:

    请输入明文: Hello World
    请输入密钥: KEY
    加密结果: Rijvs Vhwny
    解密结果: Hello World

扩展功能

如果需要更完善的实现,可以考虑添加以下功能:

c语言vigenere
(图片来源网络,侵删)
  • 处理更长的输入文本
  • 添加输入验证
  • 支持文件输入输出
  • 实现自动密钥生成
  • 添加频率分析攻击功能

这个实现提供了维吉尼亚密码的基本功能,可以根据需要进行扩展和修改。

c语言vigenere
(图片来源网络,侵删)
-- 展开阅读全文 --
头像
dede提交表单插件如何使用?
« 上一篇 04-17
织梦列表页两种模板
下一篇 » 04-17

相关文章

取消
微信二维码
支付宝二维码

目录[+]