用C或是C++实现明文到密文的转换

来源:百度知道 编辑:UC知道 时间:2024/06/26 01:09:22
这是信息安全的一道题目 我编程时不能实现 所起请大侠帮帮忙啦 高分悬赏 好的话再加
密钥k采用一个词假如是cipher,密钥k相对应的数字为k=2,8,15,7,4,17,假设明文是meet me after party。加密过程如下,见表2-2。
1)首先把明文字母转换为对应的数字。
2)把密钥字母转换为对应的数字,然后周期性延长,与明文数字对齐。
3)明文数字与密钥数字相加,如果相加的结果大于26,那么将该结果减26,以此作为对应的密文数字。
4)这样明文转换成密文为omta qv cnilv gczif

给,已经在VC上编译运行确认了:
#include<conio.h>
#include<stdio.h>

#define N 20 //暂定密钥词最长为20个字符
#define M 100 //暂定输入明文最长为100个字符

void main()
{
int key[N]={NULL},keylength=0;
int beforeStr[M]={NULL},strlength=0;
int afterStr[M]={NULL};
int i,j;
char ch;

printf("请输入密钥单词: ");
while((ch=getchar())!='\n') key[keylength++]=ch-'a';

printf("请输入明文: ");
while((ch=getchar())!='\n')
{
if(ch>='a'&&ch<='z')
beforeStr[strlength++]=ch-'a';
else beforeStr[strlength++]=ch-'\0';
}

for(i=0,j=0;i<strlength;i++)
{
if(beforeStr[i]>=0&&beforeStr[i]<=25)
{
afterStr[i]=(beforeStr[i]+key[j%keylength])%26;
j++;
}
else afterStr[i]=beforeStr[i];
}

printf("加密后为: ");
for(i=0