这是通过把阿拉伯数字进行乱序的原理,将其中的所有英文字母替换成为字母表中的后一个字母,其他字母原样输出,符号不改变
#include
#include
#include
#include
#define MAXN 1000
using namespace std;
void simple(int n);
void middle(int n);
void hard(int n);
void encrypt1();
void encrypt2();
void unencrypt();
void Create();
static char array3[100];
static int num1, count2;
int main() {
srand(time(NULL));
int b;
cout << "1:输入加密 enter to encrypt\n2:输入解密 enter to unencrypt\n3:生成秘钥 Create a password" << endl;
cin >> b;
if (b > 0 && b < 4) {
switch (b) {
case 1:
encrypt1();
break;
case 2:
unencrypt();
break;
case 3:
Create();
break;
}
} else cout << "输入错误\nInput error" << endl;
}
void simple(int n) {
int a[n], y, count = 0;
for (int i = 0; count <= n; i++) {
y = rand() % 123;
if (y >= 48 && y <= 57) {
a[count] = y;
count++;
}
}
cout << "生成的密码是 ";
for (int i = 0; i <= (n - 1); i++) {
cout.put(a[i]);
count2++;
array3[count2] = a[i];
}
cout << "\n";
}
void middle(int n) {
int a[n], y, count = 0;
for (int i = 0; count <= n; i++) {
y = rand() % 123;
if ((y >= 48 && y <= 57) || (y >= 65 && y <= 90) || (y >= 97 && y <= 122)) {
a[count] = y;
count++;
}
}
cout << "生成的密码是 ";
for (int i = 0; i <= (n - 1); i++) {
cout.put(a[i]);
count2++;
array3[count2] = a[i];
}
cout << "\n";
}
void hard(int n) {
int a[n], y, count = 0;
for (int i = 0; count <= n; i++) {
y = rand() % 123;
if (y > 33 && y < 126 && y != 32) {
a[count] = y;
count++;
}
}
cout << "生成的密码是 ";
for (int i = 0; i <= (n - 1); i++) {
cout.put(a[i]);
count2++;
array3[count2] = a[i];
}
cout << "\n";
}
void encrypt1() {
char array1[MAXN];
char c;
scanf("%c", &c);
cout << "请输入需要加密的密码 please enter you passcode" << endl;
int count = 0;
for (int i = 0; i <= MAXN; i++) {
array1[i] = getchar();
if (array1[i] == '\n') {
break;
}
count++;
}
for (int i = 0; i <= count; i++) {
if (array1[i] == '0') {
array1[i] = '6';
} else if (array1[i] == '1') {
array1[i] = '4';
} else if (array1[i] == '2') {
array1[i] = '5';
} else if (array1[i] == '3') {
array1[i] = '7';
} else if (array1[i] == '4') {
array1[i] = '3';
} else if (array1[i] == '5') {
array1[i] = '0';
} else if (array1[i] == '6') {
array1[i] = '1';
} else if (array1[i] == '7') {
array1[i] = '2';
} else if (array1[i] == '8') {
array1[i] = '9';
} else if (array1[i] == '9') {
array1[i] = '8';
} else if (array1[i] >= 'a' && array1[i] <= 'y') {
array1[i] = array1[i] + 1;
} else if (array1[i] == 'z') {
array1[i] = 'a';
} else if (array1[i] >= 'A' && array1[i] <= 'Y') {
array1[i] = array1[i] + 1;
} else if (array1[i] == 'Z') {
array1[i] = 'A';
} else {
array1[i] = array1[i];
}
}
for (int i = 0; i <= count; i++) {
cout.put(array1[i]);
}
}
void encrypt2() {
for (int i = 0; i <= count2; i++) {
if (array3[i] == '0') {
array3[i] = '6';
} else if (array3[i] == '1') {
array3[i] = '4';
} else if (array3[i] == '2') {
array3[i] = '5';
} else if (array3[i] == '3') {
array3[i] = '7';
} else if (array3[i] == '4') {
array3[i] = '3';
} else if (array3[i] == '5') {
array3[i] = '0';
} else if (array3[i] == '6') {
array3[i] = '1';
} else if (array3[i] == '7') {
array3[i] = '2';
} else if (array3[i] == '8') {
array3[i] = '9';
} else if (array3[i] == '9') {
array3[i] = '8';
} else if (array3[i] >= 'a' && array3[i] <= 'y') {
array3[i] = array3[i] + 1;
} else if (array3[i] == 'z') {
array3[i] = 'A';
} else if (array3[i] >= 'A' && array3[i] <= 'Y') {
array3[i] = array3[i] + 1;
} else if (array3[i] == 'Z') {
array3[i] = 'A';
} else {
array3[i] = array3[i];
}
}
cout << "加密后的的密码是 ";
for (int i = 0; i <= count2; i++) {
cout.put(array3[i]);
}
}
void unencrypt() {
char array2[MAXN];
char c;
scanf("%c", &c);
cout << "请输入需要解密的密码 please enter you passcode" << endl;
int count = 0;
for (int i = 0; i <= MAXN; i++) {
array2[i] = getchar();
if (array2[i] == '\n') {
break;
}
count++;
}
for (int i = 0; i <= count; i++) {
if (array2[i] == '6') {
array2[i] = '0';
} else if (array2[i] == '4') {
array2[i] = '1';
} else if (array2[i] == '5') {
array2[i] = '2';
} else if (array2[i] == '7') {
array2[i] = '3';
} else if (array2[i] == '3') {
array2[i] = '4';
} else if (array2[i] == '0') {
array2[i] = '5';
} else if (array2[i] == '1') {
array2[i] = '6';
} else if (array2[i] == '2') {
array2[i] = '7';
} else if (array2[i] == '9') {
array2[i] = '8';
} else if (array2[i] == '8') {
array2[i] = '9';
} else if (array2[i] >= 'b' && array2[i] <= 'z') {
array2[i] = array2[i] - 1;
} else if (array2[i] == 'a') {
array2[i] = 'z';
} else if (array2[i] >= 'B' && array2[i] <= 'Z') {
array2[i] = array2[i] - 1;
} else if (array2[i] == 'A') {
array2[i] = 'Z';
} else {
array2[i] = array2[i];
}
}
cout << "解密后的的密码是 ";
for (int i = 0; i <= count; i++) {
cout.put(array2[i]);
}
}
void Create() {
cout << "请输入需要多少位数的密码\nHow many digits do you want a passcode?" << endl;
cin >> num1;
if (num1 > 0 && num1 < MAXN) {
cout << "1:安全性弱的密码\n1:Weaker passwords\n2:安全性较弱的密码\n2:Strong passwords\n3:安全性强的密码\n3:Passwords with strong security\n其他:退出\nother:ESC" << endl;
int a;
cin >> a;
if (a > 0 && a < 4) {
switch (a) {
case 1:
simple(num1);
break;
case 2:
middle(num1);
break;
case 3:
hard(num1);
break;
default:
break;
}
int y;
cout << "1:输入加密\nenter to encrypt\n2:把生成的秘钥加密\nEncrypt the generated key" << endl;
cin >> y;
switch (y) {
case 1:
encrypt1();
break;
case 2:
encrypt2();
break;
default:
break;
}
} else cout << "输入错误\nInput error" << endl;
} else cout << "输入错误\nInput error" << endl;
}