Vigenere密码(维吉尼亚密码)c语言实现

Vigenere密码(维吉尼亚密码)c语言实现

简介

Vigenere密码是基于关键词的加密系统。

算法解释

Vigenere密码技术使用一个词组作为密钥,词组中的每一个字母都作为移位替换密码的密钥并确定一个替换表,然后循环地使用每一个替换表完成明文字母到密文字母的转换.

加解密函数

加密函数:

Ci=Pi+Ki(mod 26)

解密函数:

Pi=Ci-Ki(mod 26)

c语言实现
#include<stdio.h>

//加密
int encrypt(char *text,char *result,char *k)
{
    int l,i,j=0,z=0;
    for(l=0;text[l]!='\0';l++);
    for(i=0;i<l;i++)
    {
        result[z]=(text[i]-'a'+k[j]-'a')%26+'a';
        j++;
        z++;
    }
    return 0;
}

//解密
int decrypt(char *text,char *result,char *k)
{
    int l,i,j=0,z=0;
    for(l=0;text[l]!='\0';l++);
    for(i=0;i<l;i++)
    {
        result[z]=(text[i]-k[j]+26)%26+'a';
        j++;
        z++;
    }
    return 0;
}

int main()
{
    char text[50]="";
    char result[50]="";
    char k[50]="";
    int type;
    /**欢迎**/
    printf("--------欢迎使用Vigenere密码-----------\n");
    printf("请填写明文或者密文\n");
    scanf("%[^\n]",text);
    printf("请选择加密方式,输入1加密,输入2解密\n");
    scanf("%d",&type);
    printf("请输入密钥k\n");
    scanf("%s",k);
    if(type == 1){
        /**加密**/
        encrypt(text,result,k);
        printf("明文%s的密文为:%s\n",text,result);
    }else if(type == 2){
        /**解密**/
        decrypt(text,result,k);
        printf("密文%s的明文为:%s\n",text,result);
    }
    return 0;
}

验证

加密

明文=“hello”,密钥winwl

在这里插入图片描述

解密

密文=“dmyhz”,密钥winwl

在这里插入图片描述

加强版(支持大小写,支持明文空格,支持明文长度大于密钥长度)

实现代码

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

//加密
int encrypt(char *text,char *result,char *k)
{
    int i,j=0,z=0;
    int m = strlen(k); //获取密钥的长度
    int l = strlen(text); //获取明文的长度
    for(i=0;i<l;i++)
    {
        //判断大小写
        if (text[i] >= 'A' && text[i] <= 'Z'){
            if(j==m){
                j=0;   //循环密钥
                result[z]=(text[i]-'A'+k[j]-'A')%26+'A';
            } else {
                result[z]=(text[i]-'A'+k[j]-'A')%26+'A';
            }
            j++;
        } else if (text[i] >= 'a' && text[i] <= 'z'){
            if(j==m){
                j=0;   //循环密钥
                result[z]=(text[i]-'a'+k[j]-'a')%26+'a';
            } else {
                result[z]=(text[i]-'a'+k[j]-'a')%26+'a';
            }
            j++;
        } else{  //判断是否是空格
            result[z] = text[i];
        }
        z++;
    }
    return 0;
}

//解密
int decrypt(char *text,char *result,char *k)
{
    int i,j=0,z=0;
    int m = strlen(k); //获取密钥的长度
    int l = strlen(text); //获取密文的长度
    for(i=0;i<l;i++)
    {
        //判断是否是空格
        if (text[i] >= 'A' && text[i] <= 'Z'){
            if(j==m){
                j=0;   //循环密钥
                result[z]=(text[i]-k[j]+26)%26+'A';
            } else {
                result[z]=(text[i]-k[j]+26)%26+'A';
            }
            j++;
        } else if (text[i] >= 'a' && text[i] <= 'z'){
            if(j==m){
                j=0;   //循环密钥
                result[z]=(text[i]-k[j]+26)%26+'a';
            } else {
                result[z]=(text[i]-k[j]+26)%26+'a';
            }
            j++;
        } else{
            result[z] = text[i];
        }
        z++;
    }
    return 0;
}

int main()
{
    char text[50]="";
    char result[50]="";
    char k[50]="";
    int type;
    /**欢迎**/
    printf("--------欢迎使用Vigenere密码-----------\n");
    printf("请填写明文或者密文\n");
    scanf("%[^\n]",text);
    printf("请选择加密方式,输入1加密,输入2解密\n");
    scanf("%d",&type);
    printf("请输入密钥k\n");
    scanf("%s",k);
    if(type == 1){
        /**加密**/
        encrypt(text,result,k);
        printf("明文%s的密文为:%s\n",text,result);
    }else if(type == 2){
        /**解密**/
        decrypt(text,result,k);
        printf("密文%s的明文为:%s\n",text,result);
    }
    return 0;
}

验证(加强版)

加密

在这里插入图片描述

解密

在这里插入图片描述

  • 65
    点赞
  • 318
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
以下是C语言实现Vigenere密码算法的示例代码: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 100 char* encrypt(char* plaintext, char* key); char* decrypt(char* ciphertext, char* key); int main() { char plaintext[MAX_LEN]; char key[MAX_LEN]; char ciphertext[MAX_LEN]; printf("Enter plaintext: "); fgets(plaintext, MAX_LEN, stdin); plaintext[strcspn(plaintext, "\n")] = 0; // remove newline character printf("Enter key: "); fgets(key, MAX_LEN, stdin); key[strcspn(key, "\n")] = 0; // remove newline character strcpy(ciphertext, encrypt(plaintext, key)); printf("Ciphertext: %s\n", ciphertext); strcpy(plaintext, decrypt(ciphertext, key)); printf("Decrypted plaintext: %s\n", plaintext); return 0; } char* encrypt(char* plaintext, char* key) { int plaintext_len = strlen(plaintext); int key_len = strlen(key); char* ciphertext = malloc(plaintext_len + 1); for (int i = 0; i < plaintext_len; i++) { ciphertext[i] = ((plaintext[i] - 'A') + (key[i % key_len] - 'A')) % 26 + 'A'; } ciphertext[plaintext_len] = '\0'; return ciphertext; } char* decrypt(char* ciphertext, char* key) { int ciphertext_len = strlen(ciphertext); int key_len = strlen(key); char* plaintext = malloc(ciphertext_len + 1); for (int i = 0; i < ciphertext_len; i++) { plaintext[i] = ((ciphertext[i] - 'A') - (key[i % key_len] - 'A') + 26) % 26 + 'A'; } plaintext[ciphertext_len] = '\0'; return plaintext; } ``` 该程序从用户输入获取明文和密钥,并使用`encrypt()`函数加密明文并返回密文,使用`decrypt()`函数解密密文并返回明文。在加密和解密过程中,程序将明文和密文中的字母从'A'到'Z'映射到0到25,执行Vigenere算法,然后将结果映射回字母形式。请注意,该程序假定输入的明文和密钥都是大写字母。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

归子莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值