C # Principle and Implementation of the Caesar Password

Source: Internet
Author: User

Principles and implementation of passwords
 
I. Introduction to basic knowledge
Caesar Password History
Caeser is created by Julius Caesar during the expansion of Rome and is used to encrypt combat commands passed through the messenger. It moves the letters in the alphabet to a certain position for encryption.
In his work, the author of ancient Rome, Xiu tonius, revealed that Caesar often used a "Secret table" to write to his friends. The Secret table mentioned here is known as the "Caesar Secret table" in cryptography ". From a modern perspective, the Caesar table is a simple encryption transformation, that is, replacing each letter in the plain text with the third letter after its position in the alphabet. Ancient Rome is now referred to as Latin, and its letters are the 26 Latins we know from English. Therefore, the Secret table of Caesar uses d to a, and E to B ,......, Use z to represent w. These replace rules can also be expressed in a table, so they are called "Secret tables ".


 
When the offset is 3, all letters A are replaced with D, B is changed to E, and so on.
 
Basic Principles
There are various replacement methods in cryptography, but all different replacement methods contain two identical elements. Key and Protocol (algorithm ). The secret key of the Caesar password is 3, and the algorithm is to replace the letters in the ordinary alphabet with the letters corresponding to the key. The advantage of replacement encryption is that it is easy to implement but difficult to crack. it is easy for the sender and receiver to discuss a key in advance, and then generate ciphertext from the plaintext through the key, that is, if the enemy obtains the ciphertext, it is impossible to directly guess its meaning through the ciphertext.
The encryption algorithm of the Caesar password is extremely simple. The encryption process is as follows:
Here, we make this Convention: the plaintext is recorded as m, the ciphertext is recorded as c, the encryption is converted into E (k1, m) (where k1 is the key), and the decryption is converted into D (k2, m) (k2 is the decryption key) (Here we assume k1 = k2, as k ). The encryption process of the Caesar password can be recorded as the following Transformation:
C branch m + k mod n (where n is the number of basic characters)
Similarly, the decryption process can be expressed:
M + c + k mod n (where n is the number of basic characters)
N can be 256 or 128 for computers. m, k, and c are all 8-bit binary numbers. Obviously, this encryption algorithm is extremely insecure. Even if we use the brute-force method, we can decrypt it for up to 255 times. Of course, in itself, it is still a single table replacement, so the frequency analysis method is still effective for it.
 
Encryption and decryption algorithms
The replacement method of the Caesar password is to arrange the plaintext and ciphertext alphabet, and the ciphertext letter represents a fixed number of locations by moving the plaintext alphabet to the left or right. For example, when the offset is shifted to 3 (the decryption key is 3 ):
Plaintext alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Ciphertext alphabet: DEFGHIJKLMNOPQRSTUVWXYZABC
When used, the encryptor searches for the location of each letter in the message to be encrypted in the plaintext alphabet, and writes down the corresponding letters in the ciphertext alphabet. The person who needs to decrypt the key will reverse the operation based on the known key to obtain the original plaintext. For example:
Plaintext: THE QUICK BROWN FOX JUMPSOVER THE LAZY DOG
Ciphertext: WKH TXLFN EURZQ IRA MXPSVRYHU WKH ODCB GRJ
The encryption and decryption methods of Caesar passwords can also be calculated using the same remainder mathematical method. First, replace the letter with A number, A = 0, B = 1,..., Z = 25. The encryption method with the offset of n is:
E (x) = (x + n) mod 2
Decryption is:
D (x) = (x-n) mod 2
 
II. Environment Introduction
Programming Language
C # Language
Compiling environment
Visual Studio 2010
. NET 4.0
Operating System
Windows 7
 
Iii. Ideas and algorithm analysis
Define two character arrays, char ch [] and byte array [], to store the source text and Character Shift processing.
Define a key to indicate the number of shifts. This is the key to encryption and decryption.
Considering that letter encryption or decryption at the end of a table will be cyclically moved to the header of the table, the encryption and decryption statements are as follows:
Move the key backward:
View plainprint?
Int offset = (AscII (sou) + key-AscII ("a") % (AscII ("z")-AscII ("a") + 1 );
Tar = Convert. ToChar (offset + AscII ("a"). ToString ();
Move the key forward:
View plainprint?
Int offset = (AscII ("z") + key-AscII (sou) % (AscII ("z")-AscII ("a") + 1 );
Tar = Convert. ToChar (AscII ("z")-offset). ToString ();
AscII is the character conversion method.
 
Iv. program functions and result analysis
 
1. program functions
A. Encryption or decryption of uppercase/lowercase letters in a specified text file or string;
B. The execution result is displayed in the content area of the target file and saved,
C. Key parameters that can be specified
 
2. Initial Program Interface

3. Interface in Program Execution

4. Result Analysis
Try to encrypt plaintext security and decrypt it. The key parameter is 5:

After execution, the encrypted ciphertext is output: xjhzwnyd

Enter the security ciphertext xjhzwnyd to obtain the plaintext security.

V. Main Code
 
View plainprint?
Private int key = 0;
Private string str = "";

 
// Encryption Algorithm
For (int I = 0; I <str. Length; I ++)
{
String sou = ch [I]. ToString ();
String tar = "";
Bool isChar = "abcdefghijklmnopqrstuvwxyz". Contains (sou. ToLower ());
BoolisToUpperChar = isChar & (sou. ToUpper () = sou );
Sou = sou. ToLower ();
If (isChar)
{
Int offset = (AscII (sou) + key-AscII ("a") % (AscII ("z")-AscII ("a") + 1 );
Tar = Convert. ToChar (offset + AscII ("a"). ToString ();
If (isToUpperChar)
{
Tar = tar. ToUpper ();
}
}
Else
{
Tar = sou;
}
StrCaesar + = tar;
}
 
// Decryption algorithm
 
For (int I = 0; I <str. Length; I ++)
{
String sou = ch [I]. ToString ();
String tar = "";
Bool isChar = "abcdefghijklmnopqrstuvwxyz". Contains (sou. ToLower ());
BoolisToUpperChar = isChar & (sou. ToUpper () = sou );
Sou = sou. ToLower ();
If (isChar)
{
Int offset = (AscII ("z") + key-AscII (sou) % (AscII ("z")-AscII ("a") + 1 );
Tar = Convert. ToChar (AscII ("z")-offset). ToString ();
If (isToUpperChar)
{
Tar = tar. ToUpper ();
}
}
Else
{
Tar = sou;
}
StrCaesar + = tar;
}
 
// Character conversion
 
Private int AscII (stringstr)
{
Byte [] array = new byte [1];
Array = System. Text. Encoding. ASCII. GetBytes (str );
Int asciicode = (short) (array [0]);
Return asciicode;
 
}

 
Vi. Summary and shortcomings
The program was successfully tested and run within several hours. The Caesar password is the simplest Encryption Algorithm in cryptography. The principle is not complex. As long as you understand it, it is not difficult to implement it. The program is not long and basically implements the encryption and decryption process for the Caesar password. However, there are still many shortcomings. For example, this Code does not provide a solution for handling space or other symbols. If the key is unknown, you need to manually specify the key for cracking. Therefore, there are still many improvements: 1. you can set a "brute-force cracking" method to implement automatic cracking. 2. algorithm Improvement: encryption and decryption can be performed using the same remainder mathematical method. In this way, the Caesar password can become more flexible. In general, it was quite smooth from the process of writing and modifying the password of Caesar to the final successful debugging, and I also realized some cryptography interests.

Author "utopia"

Related Article

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.