📄 vigenere.cpp
字号:
#include <iostream.h>
using namespace std;
#define MINCHAR 33
#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<<"Initialize error!"<< endl;
return 1;
}
char key[256];
char str1[256];
char str2[256];
int coption;
while(1)
{
do
{
cout << "Please select an option\n1 = Encryption\n2 = Decryption\n0 = Exit\n\nOption : ";
cin >> coption;
}while(coption != -1 && coption != 1 && coption != 2);
if(coption == 0)
exit(0);
else if(coption == 1) //Encryption
{
cout << "\nEnter key : ";
cin >> key;
cout << "String to encrypt (without spacing) : ";
cin >> str1;
Encode(key, str1, str2);
cout << "String after encrypt : " << str2 << endl;
}
else if(coption == 2) //Decryption
{
cout << "\nEnter key :";
cin >> key;
cout << "String to decrypt (without spacing): ";
cin >> str1;
Dncode(key, str1, str2);
cout << "String after decrypt : " << str2 << endl;
}
cout << endl;
}
return 0;
}
//Initialize Vigenere array
bool Init()
{
int i, j;
for(i = 0; i < CHARSUM; i++)
{
for(j = 0; j < CHARSUM; j++)
{
table[i][j] = MINCHAR + (i + j) % CHARSUM;
}
}
return true;
}
bool Encode(char* key, char* source, char* 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;
}
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -