您的位置: 我要加密首页 >> 文件夹加密 >> 阅读文档:简单文字加密代码

简单文字加密代码

[ 作者: | 更新日期:2007-8-3 10:49:54 | 阅读次数: | 评论 0 条 | 我要投稿 ]

#include<dos.h>
#include<graphics.h>
#include<alloc.h>
#include<string.h>
#include<sys\stat.h>
#include<fcntl.h>
#include<sys\types.h>
#include<stdio.h>
#include<stdlib.h>

字串5

 

字串6

void displaymenu(char **menu, int count, int x1, int y1);
void savemenu(char **menu, char **buffer, int width, int count, int x1, int y1);
int getresponse(char **menu, char **buffer, int width, int count, int x1, int y1);
void highlight(char **buffer, int ch, int h, int x1, int y1);
void dehighlight(char **buffer, int ch, int h, int x1, int y1);
void getmousepos(int *button, int *x, int *y);
void initmouse();
void hidemouseptr();
void showmouseptr();
unsigned long encrypt();
void setmousepos(int x, int y);

字串3


char *menu[]={"Encrypt","Decrypt","Help","Exit"};
char source[50],pass[20],dest[50]; 字串4

void main()
{
 int gd=DETECT,gm,choice=1,width=0,i,count,ret_code;
 char **buffer;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 initmouse();
 showmouseptr();
 settextstyle(0,0,5);
 outtextxy(300,150,"EnCrYpT");
 gotoxy(38,13);
 printf(" CoNcEpT:  mayank2cool@yahoo.com");
 count=sizeof(menu)/sizeof(char *);
 settextstyle(TRIPLEX_FONT,0,3);
 displaymenu(menu,count,100,300);
 for(i=0;i<count;i++)
 {
  if(textwidth(menu[i])>width)
 width=textwidth(menu[i]);
 }
 buffer=malloc(sizeof(menu));
 savemenu(menu,buffer,width,count,100,100);
 while(choice!=4)
 { // 1-encrypt   2-decrypt
  choice=getresponse(menu,buffer,width,count,100,100);
  switch(choice)
  {
   case 1: setmousepos(400,150);  // just for fine adjustment

字串5

    setfillstyle(SOLID_FILL,0);
    bar(50,250,600,450);
    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter the path of file to be encrypted:\n ");
    gotoxy(10,22);
    printf("Path: ");
    scanf("%s",source); 字串1

    bar(50,300,600,375); 字串7

    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter password for the file to be encrypted:\n ");
    gotoxy(10,22);
    printf("Password: ");
    scanf("%s",pass); 字串1

    bar(50,300,600,375); 字串8

    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter path of the output encrypted file:\n ");
    gotoxy(10,22);
    printf("Path: ");
    scanf("%s",dest);

字串3

    ret_code=encrypt();
    if(ret_code!=0)
    {
     bar(50,300,600,375);
     rectangle(50,300,600,375);
     gotoxy(35,20);
     printf("SUCCESS: ");
     gotoxy(10,22);
     printf("%s encrypted to %s using the key: %s",source,dest,pass);
    }
    else
    {
     bar(50,300,600,375);
     rectangle(50,300,600,375);
     gotoxy(35,20);
     printf("FAILURE: ");
     gotoxy(10,22);
     printf("The selected file(s) couldn't be opened, please verify the path");
    }
    break;

字串2

   case 2: setmousepos(400,150);
    setfillstyle(SOLID_FILL,0);
    bar(50,250,600,450);
    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter the path of already encrypted file:\n ");
    gotoxy(10,22);
    printf("Path: ");
    scanf("%s",source);

字串7

    bar(50,300,600,375); 字串5

    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter password of the ecrypted file:\n ");
    gotoxy(10,22);
    printf("Password: ");
    scanf("%s",pass); 字串1

    bar(50,300,600,375); 字串6

    rectangle(50,300,600,375);
    gotoxy(20,20);
    printf("Enter path of the output dencrypted file:\n ");
    gotoxy(10,22);
    printf("Path: ");
    scanf("%s",dest); 字串2

    ret_code=encrypt();
    if(ret_code!=0)
    {
     bar(50,300,600,375);
     rectangle(50,300,600,375);
     gotoxy(35,20);
     printf("SUCCESS: ");
     gotoxy(10,22);
     printf("%s decrypted to %s using the key: %s",source,dest,pass);
    }
    else
    {
     bar(50,300,600,375);
     rectangle(50,300,600,375);
     gotoxy(35,20);
     printf("FAILURE: ");
     gotoxy(10,22);
     printf("The selected file(s) couldn't be opened, please verify the path");
    }
    break;

字串6

   case 3: setmousepos(400,150);
    setfillstyle(SOLID_FILL,0);
    bar(50,250,600,450);
    rectangle(50,250,600,450);
    gotoxy(37,17);
    printf("HELP: ");
    gotoxy(10,19);
    printf("Encrypt: Enter the full path name of both, the source file and");
    gotoxy(19,20);
    printf("the destination file. Enter a numerical password only");
    gotoxy(19,21);
    printf("as the encrypting/decrypting algorithm can only use");
    gotoxy(19,22);
    printf("numerical values.");
    gotoxy(10,23);
    printf("Decrypt: Only a previously encrypted file can be decrypted using");
    gotoxy(19,24);
    printf("the same password as was used during encryption. Usage");

字串3


    gotoxy(19,25);
    printf("of wrong password will again encrypt the file.");
    break; 字串6

  }
 }
} 字串1

void displaymenu(char **menu, int count, int x1, int y1)
{
 int i,h;
 h=textheight(menu[0]);
 for(i=0;i<count;i++)
 {
  outtextxy(x1,97+i*(h+5),menu.[i]);
 }
}

字串9

void savemenu(char **menu, char **buffer, int width, int count, int x1, int y1)
{
 int i,x2,yy1,yy2,area,h;
 h=textheight(menu[0]);
 for(i=0;i<count;i++)
 {
  x2=x1+width;
  yy1=y1+i*(h+5);
  yy2=y1+(i+1)*(h+5);
  area=imagesize(x1,yy1,x2,yy2);
  buffer[i]=malloc(area);
  getimage(x1,yy1,x2,yy2,buffer[i]);
 }
}

字串7

int getresponse(char **menu, char **buffer, int width, int count, int x1, int y1)
{
 int choice=1, prevchoice=0, x,y,x2,y2,button;
 int in,i,h;

字串6

 h=textheight(menu[0]);
 y2=y1+count*(h+5);
 x2=x1+width;
 rectangle(x1-5, y1-5, x2+5, y2+5);
 while(1)
 {
  getmousepos(&button,&x,&y);
  if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
  {
   in=1;
   for(i=1;i<=count;i++)
   {
    if(y<=y1+i*(h+5))
    {
     choice=i;
     break;
    }
   }
   if(prevchoice!=choice)
   {
    hidemouseptr();
    highlight(buffer,choice,h,x1,y1);
    if(prevchoice)
 dehighlight(buffer,prevchoice,h,x1,y1);

字串7

    prevchoice=choice;
    showmouseptr();
   }
   if((button&1)==1)
   {
    while((button&1)==1)
 getmousepos(&button,&x,&y);
    if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
 return(choice);
   }
  }
  else
  {
   if(in==1)
   {
    in=0;
    prevchoice=0;
    hidemouseptr();
    dehighlight(buffer,choice,h,x1,y1);
    showmouseptr();
   }
  }
 }
}

字串6

void highlight(char **buffer, int ch, int h, int x1, int y1)
{
 putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],NOT_PUT);
}

字串5


void dehighlight(char **buffer, int ch, int h, int x1, int y1)
{
 putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],COPY_PUT);
}

字串4

void initmouse()
{
 _AX=0;
 geninterrupt(0x33);
} 字串6

void showmouseptr()
{
 _AX=1;
 geninterrupt(0x33);
}

字串3

void hidemouseptr()
{
 _AX=2;
 geninterrupt(0x33);
}

字串5

void getmousepos(int *button, int *x, int *y)
{
 _AX=3;
 geninterrupt(0x33);
 *button=_BX;
 *x=_CX;
 *y=_DX;
}

字串2

void setmousepos(int x, int y)
{
 _AX=4;
 _CX=x;
 _DX=y;
 geninterrupt(0x33);
}

字串8

unsigned long encrypt()
{ 字串3

 int in,out;
 char buffer[512];
 int bytes=0,i;
 unsigned long key=0,num_of_bytes=0;
 char *endptr;

字串1

  key=strtoul(pass,&endptr,10);

字串4

  in=open(source,O_RDONLY|O_BINARY); 字串4


  if(in==-1)
  {
   return(0);
  }
  else
  {
   out=open(dest,O_CREAT|O_BINARY|O_WRONLY,S_IWRITE);
   if(out==-1)
 return 0;
  }
  while(1)
  {
   bytes=read(in,buffer,512);
   if(bytes>0)
   {
    num_of_bytes+=bytes;
    for(i=0;i<bytes;i++)
    {
     buffer[i]=buffer[i]^key;
    }
    write(out,buffer,bytes);
   }
   else break;
  }
  close(in);
  close(out);

字串2

 return num_of_bytes;
} 字串5

  字串4


Tags:文字 加密 代码
来源:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为