怎么用C语言进行vigenere加密与解密

Python018

怎么用C语言进行vigenere加密与解密,第1张

#include <stdio.h>

int main()

{

char s[50],key[50],e[50]

char choice

int l,m,i,j=0,k=0

printf("please choice: 1)jiami 2)jiemi\n")

scanf("%d",&m)

if(m==1)

{

printf("please input the key:\n")

scanf("%s",&key)

printf("please input the 明文:\n")

scanf("%s",&s)

for(l=0s[l]!='\0'l++)

for(i=0i<li++)

{

e[k]=(s[i]-'a'+key[j]-'a')%26+'a'

j++

k++

}

puts(e)

system("pause")

}

if(m==2)

{

printf("please input the key:\n")

scanf("%s",&key)

printf("please input the 密文:\n")

scanf("%s",&s)

for(l=0s[l]!='\0'l++)

for(i=0i<li++)

{

e[k]=(s[i]-key[j]+26)%26+'a'

j++

k++

}

puts(e)

system("pause")

}

}

我希望下载的你们看一下思想!其实很简单的!定义3个数组,分别放明文,密钥,密文!加密的思想方法是用户输入明文和密钥分别放入两个定义好的数组里面!接下去就是用循环,是该两个数组从数组中第一个存放的数开始进行加密!加密的公式上面有,稍微想一下就应该想的通的,至于解密和加密差不多的,就是公式不一样而已!不过我的那个公式也不是唯一的

没有看到Vigenère cipher 的要求 所以ciphertext 无法计算

只实现了检查key和preprocessing的功能

不过从介绍上看,Vigenère cipher 算法应该是上一题的内容 你应该做过了吧?

加过来就可以了

#include <stdio.h>

#include <string.h>

int check_key(char *key)

{

while(*key)

{

if(*key < 'a' || *key > 'z') return 1

key ++

}

return 0

}

void convert(char *src, char *dst)

{

int i, j

char *number[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"}

for(i = j = 0 src[i] i ++)

{

if(src[i] <= 'Z' && src[i] >= 'A')

dst[j++] = src[i] - 'A' + 'a'

else if(src[i] <='z' && src[i] >='a')

dst[j++] = src[i]

else if(src[i] <= '9' && src[i] >= '0')

{

strcpy(&dst[j], number[(int)src[i] - '0'])

j += strlen(number[(int)src[i] - '0'])

}

}

dst[j] = 0

}

int main()

{

char key[21]

char text[31]

char pre_text[151]

while(1)

{

printf("Please input the key: ")

gets(key)

if(check_key(key))

{

printf("Invalid key\n")

continue

}

printf("Please input the text: ")

gets(text)

convert(text, pre_text)

printf("The preprocessing: %s\n", pre_text)

}

}

#include <stdio.h>

#define MINCHAR 32

#define CHARSUM 94

int encode(char* key, char* source, char* dest)

int decode(char* key, char* source, char* dest)

char table[CHARSUM][CHARSUM]

int main()

{

int i, j

char key[256]

char source[256]

char destination[256]

int operation

FILE *fp

/* Initial the Vigenere table */

for(i = 0i <CHARSUMi++)

for(j = 0j <CHARSUMj++)

table[i][j] = MINCHAR + (i + j) % CHARSUM

printf("please choose one operation code:\n")

printf("1. Encode2. DecodeOthers. Exit.\n")

scanf("%d", &operation)

fflush(stdin)

switch (operation)

{

case 1:

printf("please input the key code:\n")

gets(key)

fflush(stdin)

printf("please input the source code you want to encode:\n")

gets(source)

fflush(stdin)

encode(key, source, destination)

printf("after encode is: \n")

printf("%s\n", destination)

fp=fopen("key.txt", "w")

fprintf(fp, "%s", key)

fclose(fp)

fp=fopen("source.txt", "w")

fprintf(fp, "%s", source)

fclose(fp)

fp=fopen("destination.txt", "w")

fprintf( fp, "%s",destination)

fclose(fp)

break

case 2:

printf("please input the key code:\n")

gets(key)

fflush(stdin)

printf("please input the source code you want to decode:\n")

gets(source)

fflush(stdin)

decode(key, source, destination)

printf("after decode is: \n")

printf("%s\n", destination)

fp=fopen("key.txt", "w")

fprintf(fp, "%s", key)

fclose(fp)

fp=fopen("source.txt", "w")

fprintf(fp, "%s", source)

fclose(fp)

fp=fopen("destination.txt", "w")

fprintf(fp, "%s", destination)

fclose(fp)

break

default:

return 0

}

return 0

}

int encode(char* key, char* source, char* dest)

{

char* tempSource = source

char* tempKey = key

char* tempDest = dest

do

{

*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR]

tempDest++

if (!(*(++tempKey)))

tempKey = key

} while(*tempSource++)

dest[strlen(source)] = '\0'

return 1

}

int decode(char* key, char* source, char* dest)

{

char* tempSource = source

char* tempKey = key

char* tempDest = dest

char offset

do

{

offset = (*tempSource) - (*tempKey)

offset = offset >= 0 ? offset : offset + CHARSUM

*tempDest = MINCHAR + offset

tempDest++

if (!(*(++tempKey)))

tempKey = key

} while(*++tempSource)

dest[strlen(source)] = '\0'

return 1

}