Caesar (caesar) password encryption and decryption method

Caesar Cipher (Caesarcipher) message encoding is a simple way: It is based on the message alphabet letter in each movement position constant k.
For example, if k is equal to 3, then the message is encoded, each letter will be moved forward 3:
A is replaced by d; b is replaced with E; and so on. The end of the alphabet will be rolled back to the beginning of the alphabet.
Thus, W may be replaced with z, x a is replaced
if the number of bits will be replaced by moving the random number, the random number and the record it is difficult to break the password will be greatly increased.

First, encryption and decryption methods

Write pictures described here

Two, C language program to achieve

#include <stdio.h> 
#include <stdlib.h>
int main (){
char small_letter[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char big_letter[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char text[1000],result[1000];
int c,count=0,k,p;
char function;
printf("Insert Text:");
c=getchar();
while(1) //读取字符串
    {
        if(c == '\n') break;
                text[count]=c;
        printf("%c",text[count]);
        count++;
        c=getchar();
    }

printf("\n");
printf("Encrypt or Decrypt? E or D :");
scanf("%c",&function);
if (function == 'E'){

    printf("Insert Key :" );
    scanf("%d",&k);
        for(int i=0;i<count;i++){
        if(text[i]>='A'&&text[i]<='Z')
        {
            result[i]=big_letter[((text[i]-'A')+k)%26];
        }
        //找出加密后字符在字符数组里的对应位置
        else if (text[i]>='a'&&text[i]<='z')
        {
            result[i]=small_letter[((text[i]-'a')+k)%26];
        }
        else result[i]=text[i];
        printf("%c",result[i]);
    }
}

else {
    printf("Insert Key :" );
    scanf("%d",&k);
        for(int i=0;i<count;i++){
        if(text[i]>='A'&&text[i]<='Z')
        {
            p=((text[i]-'A')-k);
            while(p<0)p+=26;
            result[i]=big_letter[p];
        }
        //找出解密后字符在字符数组里的对应位置
        //这里要注意不要让它超出范围(下表位置为负数)
        else if (text[i]>='a'&&text[i]<='z')
        {
            p=((text[i]-'a')-k);
            while(p<0)p+=26;
            result[i]=small_letter[p];
        }
        else result[i]=text[i];
        printf("%c",result[i]);
    }
    printf("\n");
}
return 0;
}

Third, the results of the proceedings

Write pictures described here

IV References

Wikipedia [1]
[2] Caesar cipher
[3] Caesar cipher c language

Published 32 original articles · won praise 32 · views 40000 +

Guess you like

Origin blog.csdn.net/Evan_love/article/details/80360212