c加密解密
❶ 如何用c语言对文件进行加密和解密急求......................
文件分为文本文件和二进制文件。加密方法也略有不同。
1、文本文件
加密的主要是文本的内容,最简单的方法就是修改文档的内容,比如1.txt中的文件内容:
abcd
只要给每一个字符+1,就可以实现加密。文件内容即会变为
bcde
2、二进制文件加密
二进制文件加密也就是对应用程序加密,需要理解可执行文件格式,比如Windows平台的Exe文件它是PE结构,Linux上的可执行文件是ELF结构,要对这样的程序进行加密,实际上是开发一种叫做“壳”的程序,这种程序的开发,需要将扎实的底层基础,同时也需要对软件加密解密有细致的理解,比如流行的vmprotect、z壳以及早些年的upx壳、aspack等等。
3、无论哪种加密都牵涉到文件操作的问题,使用C语言进行文件操作时,极少使用C标准库中的I/O函数,大多数使用操作系统提供的内存文件映射相关的API函数,有兴趣,可以搜索相关的资料。
❷ C语言(文件的移位与加密解密)
这道题,并不难,只是楼主,没有说清,是就字母移位吗?
但是看你的例子,有不全是。
程序如下:
#include <stdio.h>
#include <stdlib.h>
FILE *source;//源文件
FILE *destination;//目标文件
int key;//密钥
char file[100];//文件名
void encryption()//加密
{
char ch;
printf("请输入要加密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
printf("请输入加密后的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("无法创建文件!\n");
exit(0);
}
printf("请输入密钥\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch+=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
void decrypt()//解密
{
char ch;
printf("请输入要解密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
printf("请输入加密后的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("无法创建文件!\n");
exit(0);
}
printf("请输入密钥\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch-=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
int main()//主函数提供菜单
{
int choice=0;
printf("******************\n");
printf("1 文件加密\n");
printf("2 文件解密\n");
printf("3 退出\n");
printf("******************\n");
printf("请输入1 2 3选择操作\n");
scanf("%d",&choice);
switch(choice)
{
case 1:encryption();break;
case 2:decrypt();break;
case 3:break;
}
return 0;
}
❸ C语言 文件加密解密
根据你的需要,修改了之前的代码。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
constunsignedintMAX_KEY_LENGTH=1000;
intencode(charconst*datafile,charconst*keyfill);
intdecode(charconst*datafile,charconst*keyfile);
intloadKey(charconst*keyfile,int*keys,unsignedintsize);
intsaveKey(charconst*keyfile,int*keys,unsignedintsize);
intgenerateKey(int*keys,unsignedintsize);
intmain(intargc,charconst*argv[])
{
chardatafile[]="encrypted.txt";
charkeyfile[]="key.txt";
intretcode,choice,loop=1;
charch[5]={'