凱撒密碼(加密與解密)

jopen 9年前發布 | 12K 次閱讀 C/C++ 加密

    /**/
/caeser.c / 凱撒密碼是把明文字符按照相同的位移量向后移動 /比如明文can,位移量為3,密文為fdq /本程序僅對英文字母和數字有效 /*使用時將文件置于caeser.c同目錄下,密文默認名字為cipher.txt /**/

#include<stdio.h>  
#include<stdlib.h>  
#define ITEM 2  
void encipher();  
void decipher();  
int main()  
{  
    int flag;  
    void (*menu[ITEM])()={encipher,decipher};  
    printf("加密文件——1\n");  
    printf("解密文件——2\n");  
    printf("退出程序——0\n");  
    while(1)  
    {  
        scanf("%d",&flag);  
        if(!flag)  
            exit(1);  
        else if(flag==1||flag==2)  
            menu[flag-1]();  
        else  
            break;  
    }  
    return 0;  
}  
void encipher()  
{  
    int key;  
    char ch,c;  
    char file_name[50];  
    FILE *infile,*outfile;  
    printf("輸入欲加密文件名:");  
    scanf("%s",file_name);  
    printf("輸入加密密鑰:");  
    scanf("%d",&key);  
    if((infile=fopen(file_name,"r"))==NULL)  
    {  
        printf("Cannot open file!\n");  
        exit(1);  
    }  
    if((outfile=fopen("cipher.txt","w"))==NULL)  
    {  
        printf("Cannot open file!\n");  
        exit(1);  
    }  
    ch=fgetc(infile);  
    while(!feof(infile))  
    {  
        if(ch>=48&&ch<=57)//數字加密  
        {  
            c=((ch-48)+key)%10;  
            fputc((char)(c+48),outfile);  
        }  
        else if(ch>=65&&ch<=90)//大寫英文字母加密  
        {  
            c=((ch-65)+key)%26;  
            fputc((char)(c+65),outfile);  
        }  
        else if(ch>=97&&ch<=122)//小寫英文字母加密  
        {  
            c=((ch-97)+key)%26;  
            fputc((char)(c+97),outfile);  
        }  
        else  
            fputc(ch,outfile);  
        ch=fgetc(infile);  
    }  
    if(fclose(infile)||fclose(outfile))  
        printf("File cannot close!\n");  
    else  
        printf("加密成功");  
}  
void decipher()  
{  
    int key;  
    char ch,c;  
    FILE *infile,*outfile;  
    printf("輸入解密密鑰:");  
    scanf("%d",&key);  
    if((infile=fopen("cipher.txt","r"))==NULL)  
    {  
        printf("Cannot open file!\n");  
        exit(1);  
    }  
    if((outfile=fopen("a.txt","w"))==NULL)  
    {  
        printf("Cannot open file!\n");  
        exit(1);  
    }  
    ch=fgetc(infile);  
    while(!feof(infile))  
    {  
        if(ch>=48&&ch<=57)//數字解密  
        {  
            c=((ch-48)-key+10)%10;//此處+10是防止,c==負數  
            fputc((c+48),outfile);  
        }  
        else if(ch>=65&&ch<=90)//大寫英文字母解密  
        {  
            c=((ch-65)-key+26)%26;  
            fputc((char)(c+65),outfile);  
        }  
        else if(ch>=97&&ch<=122)//小寫英文字母解密  
        {  
            c=((ch-97)-key+26)%26;  
            fputc((char)(c+97),outfile);  
        }  
        else  
            fputc(ch,outfile);  
        ch=fgetc(infile);  
    }  
    if(fclose(infile)||fclose(outfile))  
        printf("File cannot close!\n");  
    else  
        printf("解密成功");  
}  </pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!