Cryptography-Virginia Code


Preface

This content is the blogger's notes for learning the Virginia Code. If there are any errors, please correct me.


1. Introduction to Virginia Password

The Virginia cipher is a substitution cipher that uses multi-table substitution. It is a multi-table cipher expanded on the basis of the Caesar cipher.
Virginia Cipher introduces the concept of "key", according to the key to determine which row of the secret table to replace, in order to counter the word frequency statistics.
Examples are as follows (the key is deceptive):
example

Two, Virginia algorithm

1. Encryption algorithm

The Virginia cipher is a key ki (i∈[1,d]) given by a sequence of d letters, and ki determines the number of shifts of the i+td (t is an integer) letter.
The modern Virginia cipher substitution table is as follows, the first line is the key, the first column is the plaintext, and the ciphertext generated by the encryption of a certain plaintext corresponding to the key is the letter in that column.
Password table
Import header files

#include <stdio.h>
#include <string.h>

The following encryption example distinguishes uppercase and lowercase letters, unified encryption is uppercase letters, the key defaults to lowercase letters, and the encryption core algorithm is a shift operation. The calculation of the relative displacement starts from'A' and moves to the right. At the same time, pay attention to the circulation of the key. If there are other needs, if the key is mixed case or encrypted as lowercase letters, it can be modified. The encryption function code is as follows:

//加密函数:统一加密为大写字母
void encrypt(char *text, char *result, char *k)
{
    
    
	int i,j=0,z=0;
	for(i=0;i<strlen(text);i++)//在明文长度内循环
	{
    
    
		if(text[i]>='a' && text[i] <= 'z')  //小写字母
			result[z]=(text[i]-'a'+k[j]-'a')%26 +'A';
		else								//大写字母
			result[z]=(text[i]-'A'+k[j]-'a')%26 +'A';
		j++;
		if(j>=strlen(k)) j=0;//以密钥长度为一个周期循环
		z++;
	}
}

2. Decryption algorithm

The decryption algorithm can be moved back against the encryption method, here unified decryption is in lowercase letters.

//解密函数:统一解密为小写字母
void decrypt(char *text, char *result, char *k)
{
    
    
	int i,j=0,z=0;
	for(i=0;i<strlen(text);i++)
	{
    
    
		if(text[i]>='a' && text[i] <= 'z')//小写字母
			result[z]=(text[i]-k[j]+26)%26 +'a';
		else                              //大写字母
			result[z]=(text[i]-k[j]+58)%26 +'a';
		j++;
		if(j>=strlen(k)) j=0;//以密钥长度为一个周期循环
		z++;
	}
}

3. Main function

The test main function code is as follows:

void main()
{
    
    
	int type;
	printf("欢迎来到维吉尼亚密码系统!");
	while(1)
	{
    
    
		char text[99]="";
		char result[99]="";
		char k[99]="";
		printf("\n0:退出 1:加密 2:解密\n");
		printf("请输入你的选择:");
		scanf("%d",&type);
		if(type == 0)
		{
    
    
			printf("系统退出。\n");
			break;
		}
		printf("请输入一段英文字母:");//默认输入为一段连续英文字母串
		scanf("%s",text);
		printf("请输入密钥:");//密钥默认小写
		scanf("%s",k);
		if(type ==1)
		{
    
    	
			encrypt(text,result,k);
			printf("密文为:%s\n",result);//输出密文
		}
		else if(type == 2)
		{
    
    
			decrypt(text,result,k);
			printf("明文为:%s\n",result);//输出明文
		}
		else
			printf("输入有误!请重新输入!\n");
	}
}

The test case is as follows:
test


to sum up

The advantage of the Virginia cipher is that this cipher is assumed to encrypt different letters in different positions. For many years, it has been considered that the Virginia cipher cannot be cracked. However, in the 1850s, Charles, a rich Englishman · Babbage cracked the password system by looking for repeated letter segments. In any encrypted information that is much longer than the key, there will inevitably be such duplication. And how can a decryptor reveal the true face of an encrypted file? For example, if the encrypted text "UPK" appears twice, the first "U" to the next "U" will count 21 letters, then he can It is concluded that the length of the key is an aliquot of 21. Or put another way, he can infer that 21 is a multiple of the key. If enough similar clues are obtained, the decryptor can know the exact length of the key. Once he knows the key length, he can perform daily frequency analysis on the encrypted information.
After learning, programming has gotten the Virginia cipher algorithm, which can be regarded as a relatively simple encryption cipher algorithm, but in the process, there are still bugs due to the logic problem of encryption and decryption, but it can be solved with more attempts and calculations.

Guess you like

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