Code implementation for the cracking of Caesar ciphertext

Source: Internet
Author: User
How to Implement the password cracking program of Caesar

How to Implement the password cracking program of Caesar
With the recent development of security, the study of cryptography is becoming increasingly important.
Asymmetric password system, but due to processing and other important reasons
It is widely used, such as shift. The basic idea of substitution remains unchanged. I personally think that in the future
Within a long time, we will certainly spend a lot of time researching cryptography to promote
We are going to talk about the healthy development of e-government and e-commerce -----
The decryption of the Kaiser password is to find out its encryption key for decryption.
It is a symmetric password system, and the encryption and decryption keys are the same. The following is a brief description of encryption and decryption.
Encryption process:
Ciphertext: c = m + K (mod 26)
Decryption process:
Plaintext: M = C-K (mod 26)
For detailed procedures, please refer to the relevant materials
The probability statistics feature is mainly used for cracking. e letters have the highest probability.

I will not talk about the encryption program implementation,
The program implementation for decryption is as follows: I wrote it in C, and the test run in vc6.0 is correct.

# Include "stdio. H"
# Include "ctype. H"
# Include "stdlib. H"

Main (INT argc, char * argv [])
{

File * fp_ciper, * fp_plain; // ciphertext and plaintext file pointer
Char ch_ciper, ch_plain;
Int I, temp = 0; // I is used to store the subscript of the maximum number of times
// Temp is used to calculate the maximum number of times
Int key; // key
Int J;
Int num [26]; // The number of times the letter appears in the specified password.

For (I = 0; I <26; I ++)
Num = 0; // initialize the num [] Array

Printf ("========================================== ========================= N ");
Printf ("------------------ by ameihong design -------------------- N ");
Printf ("========================================== ========================= N ");

If (argc! = 3)
{
Printf ("This Is Kaiser decryption usage: [file name] [ciphertext path] [plaintext path] n ");
Printf ("For example: decryption F: ciper_2_1.txt F: plain.txt N ");
} // Determine whether the input parameters of the program are correct

If (fp_ciper = fopen (argv [1], "R") = NULL)
{
Printf ("An error occurred while enabling the ciphertext! Decryption failed N ");
Exit (0 );
}
While (ch_ciper = fgetc (fp_ciper ))! = EOF)
Switch (ch_ciper)
{
Case 'A': num [0] = num [0] + 1; break; // count the occurrences of each letter in the ciphertext
Case 'B': num [1] = num [1] + 1; break; // same as upper and lower
Case 'C': num [2] = num [2] + 1; break;
Case 'D': num [3] = num [3] + 1; break;
Case 'E': num [4] = num [4] + 1; break;
Case 'F': num [5] = num [5] + 1; break;
Case 'G': num [6] = num [6] + 1; break;
Case 'H': num [7] = num [7] + 1; break;
Case 'I': num [8] = num [8] + 1; break;
Case 'J': num [9] = num [9] + 1; break;
Case 'K': num [10] = num [10] + 1; break;
Case 'l': num [11] = num [11] + 1; break;
Case 'M': num [12] = num [12] + 1; break;
Case 'N': num [13] = num [13] + 1; break;
Case '0': num [14] = num [14] + 1; break;
Case 'p': num [15] = num [15] + 1; break;
Case 'q': num [16] = num [16] + 1; break;
Case 'r': num [17] = num [17] + 1; break;
Case's ': num [18] = num [18] + 1; break;
Case 'T': num [19] = num [19] + 1; break;
Case 'U': num [20] = num [20] + 1; break;
Case 'V': num [21] = num [21] + 1; break;
Case 'W': num [22] = num [22] + 1; break;
Case 'X': num [23] = num [23] + 1; break;
Case 'y': num [24] = num [24] + 1; break;
Case 'Z': num [25] = num [25] + 1; break;

}
Fclose (fp_ciper );

For (I = 0; I <26; I ++)
If (Num> temp)
{
J = I; // obtain the maximum number of subscripts.
Temp = num;
}
If (j <5)
Key = (J + 1 + 26)-5; // The number of digits in the alphabet.
// Instead of pressing the subscript, add 1
// 5 indicates the order of E in the alphabet
Else
Key = (J + 1)-5;

If (fp_ciper = fopen (argv [1], "R") = NULL)
{
Printf ("An error occurred while opening the ciphertext again! Decryption failed N ");
Exit (0 );
} // Re-open the ciphertext for decryption
If (fp_plain = fopen (argv [2], "W") = NULL)
{
Printf ("An error occurred while opening or creating a plaintext file! Decryption failed N ");
Exit (0 );
} // Save the plaintext to this file
While (ch_ciper = fgetc (fp_ciper ))! = EOF)
{
If (ch_ciper> 'E ')
Ch_plain = (ch_ciper-'A'-key) % 26) + 'A'); // decrypt
Else
Ch_plain = (ch_ciper-'A'-Key + 26) % 26) + 'A'); // decrypt
Ch_plain = tolower (ch_plain); // converts uppercase plain text to lowercase
Fputc (ch_plain, fp_plain); // writes plain text to the file Plain
}
Fclose (fp_ciper );
Fclose (fp_plain );
Printf ("decryption successful, key = % d, plaintext has been saved to the file. Thank you! N ", key );
}

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.