Free考研资料 - 免费考研论坛

 找回密码
 注册
打印 上一主题 下一主题

凯撒加密算法C语言实现

[复制链接]
跳转到指定楼层
楼主
sparc 发表于 06-12-23 22:25:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
凯撒加密算法C语言实现

凯撒加密算法C语言实现
#include<stdio.h>
#include<conio.h>
char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
  return ('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
  return ('a'+(ch-'a'+n)%26);
}
return ch;
}

void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n===============================================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("===============================================================================\n");
printf("Please select a item:");
return;
}

void logo()/*显示版权信息*/
{
printf("\nctu_85 Encryption [Version:1.0.0]");
printf("\nCopyright (C) ctu_85.\n");
return;
}


main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];

textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();

logo();
sleep(3);/*等待3秒*/

menu();
ch0=getch();

while(ch0!='4')
{
  if(ch0=='1')
  {
   clrscr();
   printf("\nPlease input the infile:");
   scanf("%s",infile);/*输入需要加密的文件名*/

   if((in=fopen(infile,"r"))==NULL)
   {
    printf("Can not open the infile!\n");
    printf("Press any key to exit!\n");
    getch();
    exit(0);
   }

   printf("Please input the key:");
   scanf("%d",&n);/*输入加密密码*/

   printf("Please input the outfile:");
   scanf("%s",outfile);/*输入加密后文件的文件名*/

   if((out=fopen(outfile,"w"))==NULL)
   {
    printf("Can not open the outfile!\n");
    printf("Press any key to exit!\n");
    fclose(in);
    getch();
    exit(0);
   }

   while(!feof(in))/*加密*/
   {
    fputc(encrypt(fgetc(in),n),out);
   }

   printf("\nEncrypt is over!\n");
   fclose(in);
   fclose(out);
   sleep(1);
  }

  if(ch0=='2')
  {
   clrscr();
                     printf("\nPlease input the infile:");
   scanf("%s",infile);/*输入需要解密的文件名*/

                        if((in=fopen(infile,"r"))==NULL)
   {
    printf("Can not open the infile!\n");
    printf("Press any key to exit!\n");
    getch();
    exit(0);
   }

   printf("Please input the key:");
   scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/

   n=26-n;

   printf("Please input the outfile:");
   scanf("%s",outfile);/*输入解密后文件的文件名*/

   if((out=fopen(outfile,"w"))==NULL)
   {
    printf("Can not open the outfile!\n");
    printf("Press any key to exit!\n");
    fclose(in);
    getch();
    exit(0);
   }

   while(!feof(in))
   {
    fputc(encrypt(fgetc(in),n),out);
   }
   printf("\nDecrypt is over!\n");
   fclose(in);
   fclose(out);
   sleep(1);
  }

  if(ch0=='3')
  {
   clrscr();
   printf("\nPlease input the infile:");
   scanf("%s",infile);/*输入需要解密的文件名*/

   if((in=fopen(infile,"r"))==NULL)
   {
    printf("Can not open the infile!\n");
    printf("Press any key to exit!\n");
    getch();
    exit(0);
   }

   printf("Please input the outfile:");
   scanf("%s",outfile);/*输入解密后文件的文件名*/

   if((out=fopen(outfile,"w"))==NULL)
   {
    printf("Can not open the outfile!\n");
    printf("Press any key to exit!\n");
    fclose(in);
    getch();
    exit(0);
   }

   for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
   {
    rewind(in);
    rewind(out);
    clrscr();
    printf("===============================================================================\n");
    printf("The outfile is:\n");
    printf("===============================================================================\n");
    while(!feof(in))
    {
     ch1=encrypt(fgetc(in),26-i);
     putch(ch1);
     fputc(ch1,out);
    }
    printf("\n===============================================================================\n");
    printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
    printf("Press 'Q' to quit and other key to continue......\n");
    printf("===============================================================================\n");
    ch1=getch();
    if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
    {
     clrscr();
     logo();
                                        printf("\nGood Bye!\n");
     fclose(in);
     fclose(out);
     sleep(3);
     exit(0);
    }
   }

   printf("\nForce decrypt is over!\n");
   fclose(in);
   fclose(out);
   sleep(1);
  }
  menu();
  ch0=getch();
}
clrscr();
logo();
        printf("\nGood Bye!\n");
sleep(3);
}
沙发
wmwscola2 发表于 09-1-22 17:10:54 | 只看该作者
thank you
您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系我们|Free考研资料 ( 苏ICP备05011575号 )

GMT+8, 24-11-19 01:53 , Processed in 0.813844 second(s), 12 queries , Gzip On, Xcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表