#include<iostream>
#include<cctype>
#include<string>
#include<map>
#include<vector>
#include<random>
#include<ctime>
auto make(const std::vector<int>&)->std::string;
auto check1()->std::vector<int>
{
std::vector<int>information;
int min_low, min_up, min_punct,number;
std::cout << "please input the less number of lower,upper,punct and the size of password(less than 65535)" << std::endl;
std::cin >> min_low >> min_up >> min_punct >> number;//设置小写、大写、标点符号、密码长度
while (number > 65535 || number <= 0|| min_low<0 ||min_up<0||min_punct<0 ||number<(min_low+min_up+min_punct))
{
std::cout << "please input the less number of lower,upper,punct and the size of password(less than 65535)" << std::endl;
std::cin >> min_low >> min_up >> min_punct >> number;
}
information.push_back(min_low);
information.push_back(min_up);
information.push_back(min_punct);
information.push_back(number);
return information;
}
auto go_on()->int
{
std::string s1;
std::cout << "Do you want to make a password('y'to continue)" << std::endl;
std::cin >> s1;
if (s1 == std::string("y"))return 1;
else
{
std::cout << "goodbye" << std::endl;
return 0;
}
}
auto make(const std::vector<int> &information)->std::string
{
std::default_random_engine e(time(nullptr));
std::vector<char>vec{ '1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','~','`','!','@','#','$','%','^','&','*','(',')','-','_',
'=','+','[','{',']','}','|',',','<','.','>','/','?' };
std::uniform_int_distribution<unsigned> u(0, vec.size()-1);
std::map<char, int>map1;
std::string password;
int check = 0;
std::vector<int>vec1;
while (1)
{
++check;
if (check > 1000)
{
password.clear();
vec1.clear();
map1.clear();
check=0;
std::default_random_engine e(time(nullptr));
}
while (vec1.size() != information[3])vec1.push_back(u(e));
for (auto i : vec1)
{
auto word = vec;
password.push_back(word);
++map1[word];
}
if(map1.size())
for (auto i = map1.begin(); i != map1.end(); ++i)
if (i->second > (information[3]>vec.size()? information[3] /vec.size()+1:(vec.size()/ information[3] +1)))
{
password.clear();
vec1.clear();
map1.clear();
break;
}
if (password.size())
{
auto up = 0, low = 0, punct_num = 0;
for (auto i : password)
{
if (islower(i))++low;
else if (isupper(i))++up;
else if (ispunct(i))++punct_num;
}
if (low < information[0] || up < information[1] || punct_num < information[2])
{
password.clear();
vec1.clear();
map1.clear();
}
}
if (password.size())break;
}
return password;
}
int main()
{
int check = go_on();
while (check)
{
std::cout<<make(check1())<<std::endl;
check = go_on();
}
return 0;
}
楼主拿去,生成强密码,可以设置小写、大写、标点符号、密码长度 |