Traditional Culture Encyclopedia - Traditional virtues - The Virginia cipher is encrypted in plaintext as helloeverybodygoodafternoon,key as how are you ,corresponding ciphertext as ?
The Virginia cipher is encrypted in plaintext as helloeverybodygoodafternoon,key as how are you ,corresponding ciphertext as ?
Programming the Virginia Cipher
Requirements: The user can enter the key
#include
using namespace std;
#define MINCHAR 32
# define CHARSUM 94
char table[CHARSUM][CHARSUM];
bool Init();
bool Encode(char* key, char* source, char* dest);
bool Dncode(char* key, char* source, char* dest);
int main()
{
if(!Init())
{
cout << "Initialization error!" << endl;
return 1;
}
char key[256];
char str1[256];
char str2[256];
int operation;
while (1)
{
do
{
cout << "Please choose an operation: 1. encrypt; 2. decrypt; -1. exit\n";
cin >> operation;
}while(operation ! = -1 && operation ! = 1 && operation ! = 2);
if(operation == -1)
return 0;
else if(operation == 1)//encryption
{
cout << "Please enter the key:";
cin >>. key;
cout << "Please enter the string to be encrypted:";
cin >> str1;
Encode(key, str1, str2);
cout << "Encrypted string:" << str2 & lt;< endl;
}
else if(operation == 2)//decrypt
{
cout << "Please enter the key:";
cin >> key;
cout << "Please Enter the string to be decrypted:";
cin >> str1;
Dncode(key, str1, str2);
cout << "Decrypted string:" << str2 << endl;
}
< p>cout << endl;}
return 0;
}
// Initialize Virginia Square
bool Init()
{
int i, j;
for(i = 0; i <. CHARSUM; i++)
{
for(j = 0; j < CHARSUM; j++)
{
table[j] = MINCHAR + (i + j) % CHARSUM;
}
}
Return true;
}
// Encryption
// key: key
// source: string to be encrypted
// dest: encrypted string
bool Encode(char* key, char* source, char* dest)
bool Encode(char* key, char* source, char* dest)
bool Encode(char* key, char* source, char* dest) dest)
{
char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
do
{
*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];
tempDest++;
if(! (*(++tempKey)))
tempKey = key;
}while(*tempSource++);
dest[strlen(source)] = 0;
return true;
}
// Decryption
// key: the key
// source: the string to be decrypted
// dest: the decrypted string
bool Dncode(char* key, char* source, char* dest)
{
char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
char offset;
do
{
offset = (*tempSource) - (*tempKey);
offset = offset >= 0 ? offset : offset + CHARSUM;
*tempDest = MINCHAR + offset;
tempDest++;
if(! (*(++tempKey)))
tempKey = key;
}while(*++tempSource);
dest[strlen(source)] = 0;
return true;
}
- Related articles
- Yuanling traditional ancient village
- Does Tang Jian's calligraphy have a collector's value
- Specialty Food of Zhuang
- What are the traditional songs of China's Got Talent Show?
- How to develop blockchain How to do blockchain development
- Time to set off fireworks and firecrackers in South Korea to apply for UNESCO World Heritage
- What is the efficacy and role of the twelve styles of Yi Jin Jing?
- How to make Cantonese Sugar Noodle
- Derived from the Chinese nation's more than 5,000 years of civilization history nurtured by what
- Characteristics and functions of deposit business in commercial banks