C语言编程题目,各位大神帮帮我吧。

文件移位加密与解密(1人)要求实现1)文件加密;2)文件解密。备注:将某一已知文件的内容(仅限于英文字母)以字符形式读出,根据密钥(用户从键盘输入)将对应字符进行移位操作... 文件移位加密与解密(1人)
要求实现
1)文件加密;
2)文件解密。
备注:将某一已知文件的内容(仅限于英文字母)以字符形式读出,根据密钥(用户从键盘输入)将对应字符进行移位操作即可,解密时移动相反。
例如:加密:设原文为abcdef,密钥为5,则有abcdef每个字母按字母表向后移动5们(注:z后接a)可得到密文(乱码)fghijkl;对该文件解密:文件内容为fghijk1,密钥为5,则有fghijk1每个字母向前移动5位(注a后接z),可得到原文abcdef。
展开
 我来答
MACIP
2015-06-09 · 超过36用户采纳过TA的回答
知道小有建树答主
回答量:56
采纳率:0%
帮助的人:57.4万
展开全部
/*
备注:将某一已知文件的内容(仅限于英文字母)以字符形式读出,
根据密钥(用户从键盘输入)将对应字符进行移位操作即可,解密时移动相反。 
例如:加密:设原文为abcdef,密钥为5,则有abcdef每个字母按字母表向后移动5们
(注:z后接a)可得到密文(乱码)fghijkl;
对该文件解密:文件内容为fghijk1,密钥为5,
则有fghijk1每个字母向前移动5位(注a后接z),可得到原文abcdef。
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1024    //字符串最大长度
char readtext[MAX];//读取的未加密字符
char tmp[MAX];//加密后字符
//encrypt-加密  decrypt-解密  genKey-输入数字密匙
int genKey()
{
int a=0;
puts("enter key<int>:");
scanf("%d",&a);
return a;
}
void file()
{
FILE *fp=fopen("test.txt","r");//自行选择文本文件
    if( fp ==NULL)
printf("file not exsit");
int i=0;
char ch;
while( !feof(fp))
{
ch=fgetc(fp);
readtext[i] =ch;
i++;
}
fclose(fp);
}
void decrypt(char *text, int key)
{
puts("to decrypt:");
puts(text);
char tmp[MAX]="";
strcat(tmp,text);
for(int i=0; i< strlen(text)+1; i++)
if( tmp[i]>='a' && tmp[i]<='z' )
{
tmp[i] -= key ;
if( tmp[i] <'a' )
  tmp[i] = 'z'+1 + tmp[i]-'a';
}
puts("decrypted:");
puts(tmp);

}
void emcrypt(char *text, int key)
{
puts("to emcrypt:");
puts(text);
//char tmp[MAX]="";
strcat(tmp,text);
for(int i=0; i<strlen(text)+1; i++)
if( tmp[i]>='a' && tmp[i]<='z' )
{
tmp[i] += key ;
if( tmp[i] >'z' )
  tmp[i] = 'a'-1 + tmp[i]-'z';
}
puts("emcrypted:");
puts(tmp);
}
 int main()
 {
 int a =genKey();
 file();
 emcrypt(readtext,a);
 decrypt(tmp,a);
 return 0;
 }
更多追问追答
追问
运行不了,编译出错
追答
//注意此行代码
//FILE *fp=fopen("test.txt","r");//自行选择文本文件
//在.cpp文件相同目录下新建文件test.txt
//------------------test.txt 文件实例-------------------//

The Impact of Electronic Products on Family Reunion。The picture depicts an authentic scene of family get-together. Everyone sits around the dinner table for the family reunion; however, oddly, both children and young adults immerse themselves in electronic products like iPhones or iPads, leaving their old parents looking at each other speechlessly.
//------------------test.txt 文件实例-------------------//
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式