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;
}

验证(加强版)

加密

在这里插入图片描述

解密

在这里插入图片描述

发布了69 篇原创文章 · 获赞 96 · 访问量 5940

猜你喜欢

转载自blog.csdn.net/qq_45163122/article/details/104476590