把下面那个《恺撒加密解密小程序》修改了下

这是一个在VC6.0环境下编译通过的C语言程序,实现了对文本文件的恺撒加密和解密功能。程序读取a.txt进行加密,生成kaiser.txt,再解密生成out.txt。同时,程序会统计并打印密文中每个字母的出现次数,用于辅助解密。
摘要由CSDN通过智能技术生成
把昨天写的那个恺撒加密解密的简单木模拟程序稍微改了下,对目标文件 a.txt 加密,然后生成密文 kaiser.txt,接着对其解密,生成 out.txt。
 
在程序输出过程中,把密文中的各个字母的统计信息也打印出来了,数组元素num[0]----num[25]对应与字母A---Z。
 
以下程序在VC6.0下编译通过。
 
源程序:
// 恺撒加密解密小程序
// 作者:kvew    www.smatrix.org
//
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
void   kaiser(int n){
       unsigned char ch;
        FILE *fp1,*fp2;
        if((fp1=fopen("a.txt","r"))==NULL){  //以读的方式打开文件a.txt
            printf("can not open the file a.txt!/n");
            exit(0);
        }
        if((fp2=fopen("kaiser.txt","w"))==NULL){   //以写的方式打开b.txt
            printf("can not open the file b.txt!/n");
            exit(0);
        }
        while(!feof(fp1)){
        ch = fgetc(fp1);
        if(ch>=65&&ch<=90){  //如果是大写字母
            ch = ch + n;      //移位
            if(ch>=91)         //移位后仍然在大写字母集中,直接返回
               ch = ch%90+64;   //超出则转换下
        }
        else if(ch>=97&&ch<=122){    //如果是小写字母
            ch = ch + n;               //移位
            if(ch>=123)                   //移位后仍然在小写字母集,直接返回
                ch = ch%122+96;     //超出则转换下
            }
        fputc(ch,fp2);                          //如果是其他字符,则不加密照常输出
        }
           if(fclose(fp1)){                     //关闭文件a.txt
            printf("Can not close the file!/n");
   
        }
              if(fclose(fp2)){                     //关闭文件kaiser.txt
            printf("Can not close the file!/n");
   
        }
}
 
void    break_kaiser(){
        int     i,count,k;
        int     key;
        int     num[26] = {0};          //初始化数组,该数组用来统计密文中各个字母的出现次数
        unsigned  char    ch,ch_b;      // num[0]-----num[25]对应字母A--Z
        FILE    *fp1,*fp2;

        if((fp1=fopen("kaiser.txt","r"))==NULL){  //以读的方式打开文件a.txt
            printf("can not open the file a.txt!/n");
            exit(0);
        }
        if((fp2=fopen("out.txt","w"))==NULL){   //以写的方式打开b.txt
            printf("can not open the file b.txt!/n");
            exit(0);
        }
        while(!feof(fp1)){
            ch = fgetc(fp1);
                    if(ch>=65&&ch<=90){
                    num[ch-65]++;
                       }
                else if(ch>=97&&ch<=122){
                    num[ch-97]++;
                       }
        }
        if(fclose(fp1)){                     //关闭目标文件a.txt
            printf("Can not close the file!/n");
   
        }
        count = num[0];
        for(i=0;i<26;i++){
            printf("the num[%d] is %d/n",i,num[i]); //打印出统计信息
            if(count<num[i]) {
                count = num[i];    
                k   = i;    //记录下最大值的下标   
            }
        }
       
                
        printf("/n/nthe max is %d  and its index is %d/n",count,k);
        ch_b  = 65 + k;             // 记录下出现次数最多的字符,以大写方式记录
  printf("the ch_b is %d/n",ch_b);
        key   = ch_b - 69;        //和 E 值相减,计算出key
        if(key<0)
            key = 26 + key;
   
        printf("the key is: %d/n",key);
        if((fp1=fopen("kaiser.txt","r"))==NULL){  //再次以读的方式打开文件a.txt
            printf("can not open the file a.txt!/n");
            exit(0);
        }
       
       
        while(!feof(fp1)){   //如果没有读到文件结尾,那么继续读取
            ch = fgetc(fp1);
              
                if(ch>=65&&ch<=90){
                    ch = ch - key;
                    if(ch < 65)
                        ch = 90 - (64 - ch);
                   
                }
                if(ch>=97&&ch<=122){
                    ch = ch - key;
                    if(ch < 97)
                        ch = 122 - (96 - ch);
                }
           
                fputc(ch,fp2);
        }
        if(fclose(fp2)){                     //关闭文件b.txt
            printf("Can not close the file!/n");
   
        }
}
 
int main(){
    int n;
 
   
    printf("***********************************************************/n");
    printf("*                                                                                               */n");
    printf("* this program will encrypt the file a.txt to kaiser.txt!      */n");
    printf("* and then decrypt kaiser.txt to out.txt                                */n");
    printf("*                                                                                               */n");
    printf("***********************************************************/n/n");
   
 printf("/nplease input the key number (0--25):/n");
    scanf("%d",&n);
 
 kaiser(n);
 printf("encrypted success!/n/n"); 
// printf("密文中各字母出现次数的统计信息入下:/n");
 
    break_kaiser();
 printf("creaked success!/n");
    return 0;
}

程序有很多不成熟的地方,如果有朋友能提出来的话,非常感谢了:)
凯撒密码加解密程序(C语言) 2009年09月30日 星期三 13:21 1、程序结构化,用函数分别实现 2、对文件的加密解密输出到文件 #include #include void menu()/*菜单,1.加密 2.解密 3.退出*/ { clrscr(); printf("\n==============================================================================="); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf("\n3.Quit\n"); printf("===============================================================================\n"); printf("Please select a item:"); return; } char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch>='A'&&ch='a'&&ch<='z') { return ('a'+(ch-'a'+n)%26); } return ch; } main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[10],outfile[10]; textbackground(RED); textcolor(LIGHTGREEN); clrscr(); menu(); ch0=getch(); while(ch0!='3') { 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); } clrscr(); printf("\nGood Bye!\n"); sleep(3); getch(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值