密码加密原理描述如下:
将明文(一个字符串)的每个字符按照字母表后移,每个字符的后移个数由秘
钥值列表确定,如果
明文比密钥值列表长,可以从头再使用这个密钥值列表。
如明文:student,秘钥:2345,
则加密方法是:s(向后移动2位)变成u,t(向后移动3位)变成w,u(向
后移动4位)变成y,d(向后移动5位) 变成i,此时秘钥用完,所
以从头开始用2进行移位加密,依此类推可以得到加密后的密文。而
解密时,只需将密文根据秘钥反推就可以得到明文。
密码加密原理描述如下:
将明文(一个字符串)的每个字符按照字母表后移,每个字符的后移个数由秘
钥值列表确定,如果
明文比密钥值列表长,可以从头再使用这个密钥值列表。
如明文:student,秘钥:2345,
则加密方法是:s(向后移动2位)变成u,t(向后移动3位)变成w,u(向
后移动4位)变成y,d(向后移动5位) 变成i,此时秘钥用完,所
以从头开始用2进行移位加密,依此类推可以得到加密后的密文。而
解密时,只需将密文根据秘钥反推就可以得到明文。
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXSIZE 100
using namespace std;
typedef struct QueueModel
{
int SIZE;
char* data;
int front, rear;
}Queue;
void enqueue(Queue &que, char data)
{
if (que.rear + 1 == que.SIZE)
return;
que.data[que.rear++] = data;
}
void dequeue(Queue &que, char &data)
{
if (que.rear == que.front)
que.front = 0;
data = que.data[que.front++];
}
void encode(char* text, char* keys)
{
Queue que;
que.front = que.rear = 0;
int len = strlen(keys);
que.data = new char[len];
int i;
for (i = 0; i < len; i++)
enqueue(que, keys[i]);
for (i = 0; i < strlen(text); i++)
{
char ch;
dequeue(que, ch);
text[i] = (char)(text[i] + (ch - '0'));
}
cout << "加密结果:" << text << endl;
}
void decode(char* cipher, char* keys)
{
Queue que;
que.front = que.rear = 0;
int len = strlen(keys);
que.data = new char[len];
int i;
for (i = 0; i < len; i++)
enqueue(que, keys[i]);
for (i = 0; i < strlen(cipher); i++)
{
char ch;
dequeue(que, ch);
cipher[i] = (char)(cipher[i] - (ch - '0'));
}
cout << "解密结果:" << cipher << endl;
}
int main()
{
char text[MAXSIZE];
cout << "输入明文:";
scanf_s("%s", text, MAXSIZE);
getchar();
char keys[MAXSIZE];
cout << "输入密钥:";
scanf_s("%s", keys, MAXSIZE);
getchar();
encode(text, keys);
decode(text, keys);
getchar();
}