图片加密解密程序2

图片加密解密c语言实现

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
typedef unsigned char BYTE;
typedef unsigned short WORD;
void ECBencrypto(char *in_fname,char   *password,char *out_fname);
void CBCencrypto(char *in_fname,char   *password,char *out_fname);
int main()
{
 char in_fname[30];
 char out_fname[30];
 char  password[10];
printf("-------------welcome to use zsh program -------------/n");
 printf("/nplease input infile name/n");
 gets(in_fname);
 printf("/t/t请选择要加密的方式:1.EBC  2.CBC 0.退出/t/n");
    char ch;
 ch=getchar();
 while(!(ch>='0'&&ch<='2'))ch=getchar();
 while(1)
 {
  switch(ch)
  {
  case '1':
   printf("/nplease input outfile name/n");
   fflush(stdin);
         gets(out_fname);
   printf("/nplease input password /n");
   fflush(stdin);
   gets(password);
   ECBencrypto(in_fname,password,out_fname);
   break;
  case '2':
   printf("/nplease input outfile name/n");
   fflush(stdin);
         gets(out_fname);
   printf("/nplease input password /n");
   fflush(stdin);
   gets(password);
   CBCencrypto(in_fname,password,out_fname);
   break;
  case '0':
   return 0;
  }
  printf("/t/t请选择要加密的方式:1.EBC  2.CBC 0.退出/t/n");
        ch=getchar();
 // fflush(stdin);
  while(!(ch>='0'&&ch<='2'))ch=getchar();
 }
 printf("end/n");
 return 0;
}
long Getfilesize(FILE *stream)
{
 long curpos1,curpos2,length;
 curpos1=ftell(stream);//测定文件当前指向
 fseek(stream,0L,SEEK_END);
 curpos2=ftell(stream);//测定文件当前指向
 length=curpos2-curpos1;
 fseek(stream,curpos1,SEEK_SET);//恢复文件开始位置
 //printf("%ld/n",length);
 return length;
 // fclose(stream);
}
void ECBencrypto(char *in_fname,char   *password,char *out_fname)
{
 FILE *fp1,*fp2;
 if(NULL==(fp1=fopen(in_fname,"rb")))//以只读的方式打开二进制文件
 {
  printf("can not open the file/n");
  exit(1);
 }
 if(NULL==(fp2=fopen(out_fname,"wb")))//以只写的方式打开一个二进制文件,如果文件不存在,则创建该文件
 {
  printf("can not create file /n");
  exit(1);
 }
 //printf("file bkwood.bmp open success./n");
 WORD fileType;
 fread(&fileType,1,sizeof(WORD),fp1);
 fwrite(&fileType,1,sizeof(WORD),fp2);
 if(fileType != 0x4d42)
 {
  printf("file is not .bmp file!");
  return;
 }
 long length=Getfilesize(fp1);
 printf("此文件的大小为:%ld/n/n",length);
 int len=0;
 while(password[++len]);//获取密码长度
 BYTE buf;
 fread(&buf,1,sizeof(buf),fp1);
 for(int i=0;i<50;i++)
 {
  fwrite(&buf,1,sizeof(buf),fp2);
  fread(&buf,1,sizeof(buf),fp1);
 }
 int j=0;
 while(!feof(fp1))
 {
  buf^=password[j>=len?j=0:j++];//以EBC方式加密
  fwrite(&buf,1,sizeof(buf),fp2);
  fread(&buf,1,sizeof(buf),fp1);
 }
 printf("do Ebc/n");
 fclose(fp1);
 fclose(fp2); 
}
void CBCencrypto(char *in_fname,char   *password,char *out_fname)
{
 FILE *fp1,*fp2;
 if(NULL==(fp1=fopen(in_fname,"rb")))//以只读的方式打开二进制文件
 {
  printf("can not open the file/n");
  exit(1);
 }
 if(NULL==(fp2=fopen(out_fname,"wb")))//以只写的方式打开一个二进制文件,如果文件不存在,则创建该文件
 {
  printf("can not create file /n");
  exit(1);
 }
 //printf("file bkwood.bmp open success./n");
 WORD fileType;
 fread(&fileType,1,sizeof(WORD),fp1);
 fwrite(&fileType,1,sizeof(WORD),fp2);
 if(fileType != 0x4d42)
 {
  printf("file is not .bmp file!");
  return;
 }
 long length=Getfilesize(fp1);
 printf("此文件的大小为:%ld/n/n",length);
 int len=0;
 while(password[++len]);//获取密码长度
 BYTE buf;
 fread(&buf,1,sizeof(buf),fp1);
 for(int i=0;i<50;i++)
 {
  fwrite(&buf,1,sizeof(buf),fp2);
  fread(&buf,1,sizeof(buf),fp1);
 }
 int j=0;
 char temp;
    while(!feof(fp1))
 {
  buf^=password[j>=len?j=0:j++];
  temp=buf^=password[j>=len?j=0:j++];//以CBC方式加密
  buf^=temp;
  fwrite(&buf,1,sizeof(buf),fp2);
  fread(&buf,1,sizeof(buf),fp1);
 }
 printf("do Cbc/n");
 fclose(fp1);
 fclose(fp2);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值