古典加密算法的C实现

1 凯撒加密

//由于凯撒加密不需要手动输入密钥,因此采用文件操作能大量提升加密算法的实用性
#include<stdio.h>
#include<string.h>
#define PASSWORD 3
 main()
{
    char c;
    FILE *source = fopen("source.txt","r");
    FILE *result = fopen("result.txt","w");
       
    while((c=fgetc(source))!=EOF)
    {      
             if(c>='a'&&c<='z')
                          c='a'+(c-'a'+PASSWORD)%26;
             else if(c>='A'&&c<='Z')
                          c='A'+(c-'A'+PASSWORD)%26;
              fputc(c,result);
    }
  
 
    close(source);
    close(result);
    system("pause");
}
 
 
 
//同理,解密算法也使用文件操作
#include<stdio.h>
#include<string.h>
#define PASSWORD 3
 main()
{
    char c;
    FILE *source = fopen("source.txt","r");
    FILE *result = fopen("result.txt","w");
       
 
  
    while((c=fgetc(source))!=EOF)
    {      
             if(c>='a'&&c<='z')
                          c='a'+(c-'a'+26-PASSWORD)%26;
             else if(c>='A'&&c<='Z')
                          c='A'+(c-'A'+26-PASSWORD)%26;
              fputc(c,result);
    }
  
    close(source);
    close(result);
    system("pause");
}

2 vernam加密

//由于vernam表是一个非常有规则的表格,只要找到规律就可实现
#include <stdio.h>
 
main(){
    char sourse[50],buf[50],key[50];
    int i=0;
    int j=0;
    printf("input ming wen:\n");
    scanf("%s",sourse);
    printf("input key:\n");
    scanf("%s",key);
    while(sourse[i]!='\0'){
        if(sourse[i]>= 'A' && sourse[i] <= 'Z')
            sourse[i]-=64;
        else{
            printf("the mingwen you input is wrong!\n");
            return;
        }
        i++;
    }
    while(key[j]!='\0'){
        if(key[j]>='A' && key[j]<='Z')
            key[j]-=64;
        else{
            printf("the key you input is wrong!\n");
            return;
        }
        j++;
    }
     
    if(i != j){
        printf("length not equal\n");
        return;
    }
    for(i=0;i<50;i++){
        buf[i]='\0';
    }
    for(i = 0; i < j;i++){
        buf[i] = (sourse[i]+key[i])%26;
    }
    printf("the result is:\n")  ;
    i=0;
    while(buf[i] != '\0'){
        printf("%c",buf[i]+64);
        i++;
    }
    printf("\n");
    system("pause");
}
 
//解密算法
#include <stdio.h>
 
main(){
    char sourse[50],buf[50],key[50];
    int i=0;
    int j=0;
    printf("input miWen:\n");
    scanf("%s",sourse);
    printf("input key:\n");
    scanf("%s",key);
    while(sourse[i]!='\0'){
        if(sourse[i]>= 'A' && sourse[i] <= 'Z')
            sourse[i]-=64;
        else{
            printf("the miWen you input is wrong!\n");
            return;
        }
        i++;
    }
    while(key[j]!='\0'){
        if(key[j]>='A' && key[j]<='Z')
            key[j]-=64;
        else{
            printf("the key you input is wrong!\n");
            return;
        }
        j++;
    }
     
    if(i != j){
        printf("length not equal\n");
        return;
    }
    for(i=0;i<50;i++){
        buf[i]='\0';
    }
    for(i = 0; i < j;i++){
        buf[i] = sourse[i] - key[i];
        if(buf[i] <=0)
            buf[i] += 26;
        if(buf[i] > 26)
            buf[i] -= 26;
    }
    printf("the result is:\n")  ;
    i=0;
    while(buf[i] != '\0'){
        printf("%c",buf[i]+64);
        i++;
    }
    printf("\n");
    system("pause");
}

3 vigenere加密

//本算法的表格看起来极为庞大,但是规律性很强
#include <stdio.h>
 
main(){
    char sourse[50],buf[50],key[50],key1[50];
    int i=0;
    int j=0;
    int k;
    printf("input mingWen:\n");
    scanf("%s",sourse);
    printf("input key:\n");
    scanf("%s",key);
    while (sourse[i] != '\0')
        i++;
    while (key[j] != '\0')
        j++;
    for(k=0;k<50;k++){
        key1[k]='\0';
    }
    for(k=0;k<50;k++){
        buf[k]='\0';
    }
    for(k=0;k<i;k++){
        key1[k] = key[k%j];
    }
    for(j=0;j<i;j++){
        buf[j]=sourse[j] +(key1[j]-65);
        if(buf[j] > 'Z')
            buf[j] = buf[j] -90+64;
    }
    printf("%s\n",buf);
    system("pause");
}
 
//解密算法
#include <stdio.h>
 
main(){
    char sourse[50],buf[50],key[50],key1[50];
    int i=0;
    int j=0;
    int k;
    printf("input miWen:\n");
    scanf("%s",sourse);
    printf("input key:\n");
    scanf("%s",key);
    while (sourse[i] != '\0')
        i++;
    while (key[j] != '\0')
        j++;
    for(k=0;k<50;k++){
        key1[k]='\0';
    }
    for(k=0;k<50;k++){
        buf[k]='\0';
    }
    for(k=0;k<i;k++){
        key1[k] = key[k%j];
    }
    for(j=0;j<i;j++){
        buf[j]=sourse[j] -(key1[j]-65);
        if(buf[j] < 'A')
            buf[j] = 90-(64-buf[j]);
    }
    printf("%s\n",buf);
    system("pause");
}


你可能感兴趣的:(c,加密,算法,解密)