Cryptography-Caesar Code


Preface

This content is the blogger's notes for learning Caesar's password. Please correct me if there are any errors.


1. Introduction to Caesars Password

The Caesar cipher is the earliest substitution cipher, using single table substitution. The basic idea is to realize encryption and decryption by moving letters by a certain number of digits. All letters in the plaintext are replaced by ciphertext after offsetting backward (or forward) by a fixed number in the alphabet. For example, when the offset is 3, all letters A will be replaced with D, B will become E, and so on, X will become A, Y will become B, and Z will become C. The number of bits is the key for Caesar cipher encryption and decryption.
Examples are as follows:
Caesar Code

Second, the Caesar cipher algorithm

1. Encryption algorithm

For a certain plaintext letter m, assuming that its ASCII code is x and i bits are shifted backward, the ASCII code of the newly obtained ciphertext c is x + i. Considering that the encryption is a periodic encryption of 26 letters, you need Take the ASCII code modulo 26, for example, the lowercase letters are as follows:

c = (m - 'a' + i) % 26 + 'a';

2. Decryption algorithm

In the same way, the decryption algorithm also has the same meaning. For a certain ciphertext letter m, suppose its ASCII code is x, and the ASCII code of the newly obtained ciphertext c is x-i. Consider encryption For periodic encryption of 26 letters, the ASCII code needs to be modulo 26, and considering that (xi) may be a negative number, use'z' for calculation. For example, the lowercase letters are as follows:

m = (c - 'z' - i) % 26 + 'z';

Three, code implementation

The following code implements the Caesar cipher encryption algorithm whose shift is i, which is only valid for letters and is case sensitive.

#include <stdio.h>

void main(){
    
    
	char plaintext[100],ciphertext[100];
	char m;
	int i,k,j=0;
	while(1)
	{
    
    
		printf("请选择模式:1.加密 2.解密 0.退出\n");
		scanf("%d", &k);
		if(k == 0) break;
		else if(k == 1)
		{
    
    
			printf("请输入加密位数:");
			scanf("%d", &i);
			getchar();
			printf("请输入明文:\n");
			gets(plaintext);
			for(j = 0; plaintext[j] != '\0'; j++)
			{
    
    
				m = plaintext[j];
				if(m <= 'Z' && m >='A')//往后移
					ciphertext[j] = (m - 'A' + i) % 26 + 'A';
				else if(m <= 'z' && m >='a')
					ciphertext[j] = (m - 'a' + i) % 26 + 'a';
				else ciphertext[j] = m;
			}
			ciphertext[j] = '\0';//添加结束符
			printf("加密后的密文如下:\n");
			for(j = 0; ciphertext[j] != '\0'; j++)
				printf("%c", ciphertext[j]);
			printf("\n");
		}
		else if(k == 2)
		{
    
    
			printf("请输入解密位数:");
			scanf("%d", &i);
			getchar();
			printf("请输入密文:\n");
			gets(ciphertext);
			for(j = 0; ciphertext[j] != '\0'; j++)
			{
    
    
				m = ciphertext[j];
				if(m <= 'Z' && m >='A')//往前移
					plaintext[j] = (m - 'Z' - i) % 26 + 'Z';
				else if(m <= 'z' && m >='a')
					plaintext[j] = (m - 'z' - i) % 26 + 'z';
				else plaintext[j] = m;
			}
			plaintext[j] = '\0';
			printf("加密后的密文如下:\n");
			for(j = 0; plaintext[j] != '\0'; j++)
				printf("%c", plaintext[j]);
			printf("\n");
		}
		else
			printf("输入错误!\n");
		memset(plaintext, 0, 100);//清空字符数组
		memset(ciphertext, 0, 100);
	}
}

The sample results are as follows
result


to sum up

The Caesar password is relatively simple. It is only a single table substitution. It is easier to implement and not difficult to crack. It is a very insecure password.
The Caesar cipher does not have a key. Even if there is no key, it can be cracked. Because the Caesar shift cipher has only 25 types of keys, the most is to check these 25 possibilities one by one. This is what we call Brute force method.
Of course, the frequency analysis method can also be used to analyze the frequency of each letter, so as to know the shift number of the Caesar cipher.
"If we know the language of an encrypted message, the way to decipher this encrypted message is to find another article written in the same language, about one page long, and then we calculate the frequency of each letter in it. We mark the letter with the highest frequency as No. 1, the second with the frequency as No. 2, and the third as No. 3, and so on, until we have counted all the letters in the sample article. Then we observe the ciphertext that needs to be deciphered, the same Categorize all the letters, find the most frequent letters, and replace all the letters with the highest frequencies in the sample article. Replace the second most frequent letters with the number 2 in the sample, and replace the third with the number 3, until all of the ciphertext The letters have been replaced by the letters in the sample."——(360Wiki)

Guess you like

Origin blog.csdn.net/weixin_47585015/article/details/109853825