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]={'