Crack the Caesar Password

Source: Internet
Author: User

First, let's look at the following Caesar password.

A homework question of discrete mathematics.

As the oldest symmetric encryption system, the Caesar password has become popular in ancient Rome. His basic idea is to encrypt and decrypt letters by moving a certain number of digits. For example, if the key is to move the digits of the plain text letter to three digits, then the plain text letter B is converted to the ciphertext E, and so on, and X is changed to a and Y to B, Z becomes C. It can be seen that the number of digits is the key to encrypt and decrypt the Caesar password.

The questions are as follows:

It is known that the following crypted paragraph is encryted using a slighted enhanced Caesar encryption method of the Form F (p) = (AP + B) mod29.

JGC !. CHR, dhdw, NBN bn kdncy Oh uxc jdru uxdul bh dh, qbfch running OJ mgbuuch wdhq. dqcl rcgudbh wcuucgn dhy running OJ running Orr. g mbuxfdge, bhq JGC !. Chrbc3aogcofcgl uxcgc bn D rxdgdrubnubr ybnugbk. uboh OJ wcuucgn uxdu bn go. qxw, uxc NDAC jog dwaonu
Dww ndavwcn OJ uxdu wdhq. dqcs

What is also known is the two most frequently used letters in the paragraph are T and E. The reference alphabet is an array of length 29 "abcdefghijklmnopqrstuvwxyz ,.! ", Indicating that space is not handled during encryption (space remains space). Try to de-Cypher
The message and give the plaintext.

In simple terms, it is to decrypt a piece of text. The encryption method is f (p) = (AP + B) the Caesar encryption method of mod29, which is an enhanced version of the Caesar password. The alphabet is "abcdefghijklmnopqrstuvwxyz ,.! ", And according to statistics, T and E are the most frequently used English letters.

The general idea of cracking is to obtain the corresponding key through the frequency of letters and then decrypt the text. The solution process is as follows.

Statistical letter appearance

void analyze(char *c){int a[26]={0};int i=0,j;while(c[i]!='\0'){if(c[i]!='.'||c[i]!='!'||c[i]!=',')a[c[i]-'a']++;printf("%d\t",i++);}for(j=0;j<26;j++){printf("%c:%d\n",j+'a',a[j]);}}

In Main.

int main(){char ch1[10000];char hash[29];printf("Input paragragh:\n");gets(ch1);analyze(ch1);return 1;}

Expected result:

Changzhi U and C are the two most frequently occurring letters.

Order in the alphabet:

T-19 u-20 E-4 C-2

The following sub-statements can be listed:

(19a + B) mod29 = 20

(4A + B) mod29 = 2

However, the values of A and B cannot be obtained, and they must be obtained through a program enumeration.

#include<stdio.h>void getA(){int a,b;for(a=0;a<20;a++){b=20-(19*a)%29;if((4*a+b)%29==2)printf("%d,%d\n",a,b);}}int main(){getA();return 1;}

Obtain a = 7, B = 3.

Since f (p) satisfies the two-shot relationship, that is, the relationship between two pairs can be put in the hash table.

char alphabet[]="abcdefghijklmnopqrstuvwxyz,.!";void createHash(char *c,int a,int b,int d){int i;char encode[29];for(i=0;i<29;i++){encode[i]=alphabet[(a*i+b)%d];printf("%c",encode[i]);}for(i=0;i<29;i++){c[encode[i]-'a']=alphabet[i];}}

In this way, the ciphertext can be directly hashed to the plaintext.

The decryption function is the easiest.

void decypher(char *c,char *hashmap,int a,int b,int d){int i=0;int char_pos;//record the caractor's positionwhile(c[i]!='\0'){    char_pos=c[i]-'a';    //printf("char_pos:%d\n",char_pos);if(c[i]!=' '){printf("%c",hashmap[char_pos]);}else{printf(" ");}i++;}}

The complete procedure is as follows:

#include<stdio.h>void analyze(char *c);void decypher(char *c,char *hashmap,int a,int b,int d);void createHash(char *c,int a,int b,int d);char alphabet[]="abcdefghijklmnopqrstuvwxyz,.!";int main(){char ch1[10000];char hash[29];printf("Input paragragh:\n");gets(ch1);analyze(ch1);printf("\nAfter decyphered:\n");createHash(hash,7,3,29);decypher(ch1,hash,7,3,29);printf("\n%s\n",ch1);return 1;}void analyze(char *c){int a[26]={0};int i=0,j;while(c[i]!='\0'){if(c[i]!='.'||c[i]!='!'||c[i]!=',')a[c[i]-'a']++;printf("%d\t",i++);}for(j=0;j<26;j++){printf("%c:%d\n",j+'a',a[j]);}}void decypher(char *c,char *hashmap,int a,int b,int d){int i=0;int char_pos;//record the caractor's positionwhile(c[i]!='\0'){    char_pos=c[i]-'a';    //printf("char_pos:%d\n",char_pos);if(c[i]!=' '){printf("%c",hashmap[char_pos]);}else{printf(" ");}i++;}}void createHash(char *c,int a,int b,int d){int i;char encode[29];for(i=0;i<29;i++){encode[i]=alphabet[(a*i+b)%d];printf("%c",encode[i]);}for(i=0;i<29;i++){c[encode[i]-'a']=alphabet[i];}printf("\n%s\n",alphabet);}

Final running result:

Decryption result:

Frequency analysis is based on the fact that, in any given stretch of written language, certain letters and combinations of letters occur withvarying frequencies. moreover, there is a charactistic distribution of letters that is roughly the same for almost
All samples of that language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.