大一时学习C++,觉得密码学的一些东西挺有意思,因此为了练习c++,实现了一些简单的加密解密过程(非专业,目的仅仅是当时为了熟悉c++)。
1.实现一个简单的单表置换加密与解密功能。单表置换加密即让原符号与新符号一一对应,进而对用原符号组成的符号串进行加密。
下面实现了让abcdefghijklmnopqrstuvwxyz中的元素依次对应qazwsxedcrfvtgbyhnujmikolp中的元素,进而加密与解密
输入:abcde
加密后输出:qazws
- 加密函数
- 解密函数
- 主函数
双表置换加密即让一串原文与两张表进行映射,并按照某种规律进行加密。
原符号:Abcdefghijklmnopqrstuvwxyz
对应的置换表:
(1)Qwertyuiopasdfghjklzxcvbnm(句子中的某一字母第1.3.5……次出现用该加密方式)
(2)Lkjhgfdsaqwertyuiopmnbzxcv(句子中的某一字母第2.4.6……次出现用该加密方式)
- 加密方式1函数
- 加密方式2函数
- 选择加密方式函数
- 实现加密函数
- 对应解密方式一函数
- 对应解密方式二函数
- 解密函数
- 主函数
输入:AAbbcc
加密后输出:QLwkej
单表加密代码
//单表置换密码
、
//加密
int first_change(string& sentence)
{
int location;
string key1 = "qazwsxedcrfvtgbyhnujmikolp";
string alphabet = "abcdefghijklmnopqrstuvwxyz";//对应的置换单表,可进行修改
for (int i = 0;i < sentence.length();i++)
{
if (sentence[i] != ' ')
{
location = alphabet.find_first_of(sentence[i]);
sentence[i] = key1[location];
}
}
return 1;
}
//解密
int first_decode(string& sentence)
{
int location;
string key1 = "qazwsxedcrfvtgbyhnujmikolp";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0;i < sentence.length();i++)
{
if (sentence[i] != ' ')
{
location = key1.find_first_of(sentence[i]);
sentence[i] = alphabet[location];
}
}
return 1;
}
//单表置换密码
int main()
{
string sentence;
cout << "please input what you want to encrypt:" << endl;
getline(cin, sentence);
first_change(sentence);
cout << "加密后" << endl;
cout << sentence << endl;
first_decode(sentence);
cout << "解密后" << endl;
cout << sentence << endl;
}
运行结果:
双表加密代码
//双表置换加密
//第一种加密
int first_change( char &letter)
{
int location;
string key1 = "qwertyuiopasdfghjklzxcvbnm";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
if (letter!= ' ')
{
location = alphabet.find_first_of(letter);
letter = key1[location];
}
return 1;
}
//第二种加密
int second_change(char& letter)
{
int location;
string key2 = "lkjhgfdsaqwertyuiopmnbzxcv";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
if (letter != ' ')
{
location = alphabet.find_first_of(letter);
letter = key2[location];
}
return 1;
}
//加密时:判断该字母第几次出现在句子当中
int judeg_encryption(const char letter)
{
static map letter_nums
{
{'a',0},{'b',0},{'c',0},{'d',0},{'e',0},{'f',0},
{'g',0},{'h',0},{'i',0},{'g',0},{'k',0},{'l',0},
{'m',0},{'n',0},{'o',0},{'p',0},{'q',0},{'r',0},
{'s',0},{'t',0},{'u',0},{'v',0},{'w',0},{'x',0},
{'y',0},{'z',0}
};
if (letter != ' ')
{
letter_nums[letter]++;
}
return letter_nums[letter]%2;
//若返回值为1,对应第一次加密方法
//若返回值为二,对应第二次加密方法
}
//加密
string encryption(string& sentence,vector &record )
{
char letter;
int option;//接受选择判断函数的返回值
for (int i = 0;i < sentence.length();i++)
{
if (sentence[i] != ' ')
{
letter = sentence[i];
option = judeg_encryption(letter);
if (option ==1)
{
first_change(letter);
sentence[i] = letter;
record.push_back(1);
}
if (option == 0)
{
second_change(letter);
sentence[i] = letter;
record.push_back(0);
}
}
}
return sentence;
}
//重载第一种解密
int first_decode(char& letter)
{
int location;
string key1 = "qwertyuiopasdfghjklzxcvbnm";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
if (letter != ' ')
{
location = key1.find_first_of(letter);
letter = alphabet[location];
}
return 1;
}
//第二种解密
int second_decode(char& letter)
{
int location;
string key2 = "lkjhgfdsaqwertyuiopmnbzxcv";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
if (letter != ' ')
{
location = key2.find_first_of(letter);
letter = alphabet[location];
}
return 1;
}
string decode(string& sentence, const vector record)
{
char letter;
int j = 0;
for (int i = 0;i < sentence.length();i++)
{
if (sentence[i] != ' ')
{
if (record[j] == 1)
{
letter = sentence[i];
first_decode(letter);
sentence[i] = letter;
}
if (record[j] == 0)
{
letter = sentence[i];
second_decode(letter);
sentence[i] = letter;
}
j++;
}
}
return sentence;
}
//双表置换加密
int main()
{
static vector record;
string sentence;
cout << "please input what you want to encrypt:" << endl;
getline(cin, sentence);
encryption(sentence,record);
cout << "加密后" << endl;
cout << sentence << endl;
cout << "解密后" << endl;
cout << decode(sentence, record);
}
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)