凯撒加解密算法C语言实现

凯撒加密(Caesarcipher)是一种简单的消息编码方式:它根据字母表将消息中的每个字母移动常量位k。
举个例子如果k等于3,则在编码后的消息中,每个字母都会向前移动3位:
a会被替换为d;b会被替换成e;依此类推。字母表末尾将回卷到字母表开头。
于是,w会被替换为z,x会被替换为a。

如果是将移动的位数用随机数进行代替,并且记录下该随机数,则破解密码的难度将大大增加。

//linux c 代码

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
//加密
int kaisa_encrypt(const char *str1,char *str2,const  int len)
{
    int i;
    if(str1 == NULL || str2 == NULL || len <= 0){
        return -1;
    }
    int m = strlen(str1);
    if(m <= 0){
        return -1;
    }
    for(i=0;i<m;i++)
    {
        if(str1[i]>='a'&&str1[i]<='z')
            str2[i]='a'+(str1[i]-'a'+len)%26;
        else
            str2[i]=str1[i];                                                                                 
    }
    return 0;
}
//解密
int kaisa_decrypt(const char *str2,char *str3, const int len)
{
    int i;
    char all_letter[26]="abcdefghijklmnopqrstuvwxyz";
    if(str3 == NULL || str2 == NULL || len <=0){
        return -1;
    }
    int m = strlen(str2);
    if(m <= 0){
        return -1;
    }
    for(i=0;i<m;i++)
    {
        if(str2[i]>=all_letter[len]&&str2[i]<='z')
            str3[i]=str2[i]-len;
        else if(str2[i]>='a'&&str2[i]<=all_letter[len-1])
            str3[i]=str2[i]+(26-len);
        else
            str3[i]=str2[i];
    }
    return 0;
}

int main()
{
    char str1[20]="binbin";
    char str2_encrypt[20]="";
    char str3_decrypt[20]="";
    /***加密****/
    kaisa_encrypt(str1,str2_encrypt,3);
    printf("原文%s-->密文%s\n",str1,str2_encrypt);
    /***解密****/
    kaisa_decrypt(str2_encrypt,str3_decrypt,3);
    printf("密文%s-->原文%s\n",str2_encrypt,str3_decrypt);

    return 0;
} 
相关推荐
c语言编写,欢迎扔板砖 //移位算法 #include<stdio.h> #include<stdlib.h> #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]<=122&&cipher[j]>99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]<65) { decryption[j]=91-(65-plain[j]); printf("character %d = %c\n",j,decryption[j]); } else if(decryption[j]>90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else { printf("character %d = %c\n",j,decryption[j]); } } printf("finish decryption!\n"); if((fpd=fopen("decryption.txt","w+"))==NULL) { printf("cann't create decryption file!\n"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&decryption[j],sizeof(char),1,fpd)!=1) { printf("saving decryption file error!"); exit(0); } } printf("sucess saving decrypiton file!\n"); return 0; }
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页