3、对输入的密码进行加密。 请从键盘输入一个8位密码。该密码可以包含空格键,但不能包含回车键。输入的密码必须包含大写字母、小写字母、数字和特殊符号。 加密规则为: 1)将数字全部挑选出来,将所有数字在原密码中所处位置(位置分别为1-8)和数字对应的ASCII码值组合成为一个新的字符串1(举例:输入密码SC1a2***,则新字符串为“349550”,3表示字符‘1’在密码中的位置为3,49表示字符‘1’的ASCII码值)。 2)将原密码中的字母和特殊符号取出,组成一个新的字符串,该字符串每个字符前需增加该字符在原密码中的位置信息。(举例:输入密码SC1a2***,则新字符串为“1S2C4a6*7*8*”)。将新字符串中的大写字母转换成小写字母,并循环后移3个位置。(如A转换为d,Z转换为c);将小写字母转换成大写字母,并循环前移5个位置,形成一个新的字符串2。 3)将转换后的两个字符串(1和2)进行连接(先是数字对应新的字符串1,然后字母特殊符号对应新的字符串2),形成加密后的密码。用c++编写
时间: 2023-05-28 08:05:29 浏览: 110
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char password[9]; // 存储输入密码的数组,最多8位
char num_str[9]; // 存储数字对应的字符串
char char_str[17]; // 存储字母和特殊符号对应的字符串
char encrypt[26]; // 存储加密后的密码
printf("请输入一个8位密码(不能包含回车键):");
scanf("%8s", password); // 读取最多8个字符,不包括空字符
int num_count = 0; // 记录数字个数
int char_count = 0; // 记录字母和特殊符号个数
// 遍历密码,提取数字和字母特殊符号,并记录它们的位置信息
for (int i = 0; i < strlen(password); i++) {
if (isdigit(password[i])) {
num_str[num_count++] = i + '0'; // 将数字位置转换成字符
num_str[num_count++] = password[i]; // 将数字加入字符串
} else if (isalpha(password[i]) || ispunct(password[i])) {
char_str[char_count++] = i + '0'; // 将字符位置转换成字符
char_str[char_count++] = password[i]; // 将字符加入字符串
}
}
// 对字符字符串进行大小写转换和循环移位操作
for (int i = 0; i < char_count; i++) {
if (isupper(char_str[i])) {
char_str[i] = tolower(char_str[i]); // 转换成小写字母
char_str[i] = (char_str[i] - 'a' + 3) % 26 + 'a'; // 右移3位,循环移位
} else if (islower(char_str[i])) {
char_str[i] = toupper(char_str[i]); // 转换成大写字母
char_str[i] = (char_str[i] - 'A' + 21) % 26 + 'A'; // 左移5位,循环移位
}
}
// 将数字字符串和字符字符串连接成加密后的密码
strncpy(encrypt, num_str, num_count);
strncat(encrypt, char_str, char_count);
printf("加密后的密码为:%s\n", encrypt);
return 0;
}