C++ full password generation implementation code
- 2020-06-01 10:21:52
- OfStack
Here "full password" refers to all possible passwords in the specified string. In the case of the string "0123456789", there are 100 possible 2-bit passwords, or L^N. (L represents the length of the string, N the number of bits to generate the password).
Method 1: recursion. This one is a little bit easier to understand, but for every character you break it down, you set it to a new string, and then you go to the last string. The code is as follows:
void CpasswordCreateDlg::CreatePass1(CString inStr,int m,CString outStr)
{
if (m==0)
{
fp.SeekToEnd();
CString tStr=outStr+L"\n";
fp.WriteString(tStr);//fp for 1 a CStdioFile , global variable
passFlag++;// How many passwords are generated for the progress bar, as ULONGLONG type
int persent=int((float)passFlag/passScore*100);//passScore The total number of passwords to generate
if (persent%5==0)
{
m_progressCtrl.SetPos(persent);//m_progressCtrl Is the progress bar
}
}
else
{
for (int i=0;i<inStr.GetLength();i++)
{
CreatePass1(inStr,m-1,outStr+inStr.Mid(i,1));
}
}
}
The second method: looping, without recursion. The code is as follows. Now, if I have to explain the meaning of the code, I really can't figure out why I designed it this way at that time. I can't even understand why I was so awesome at that time.
void CpasswordCreateDlg::CreatePass2(CString inStr,int m)
{
fp.SeekToEnd();
int *flag=new int [m];// with 1 Pointer to extract a character from a position in a string
for (int i=0;i<m;i++)
{
flag[i]=0;
}
int inStrLen=inStr.GetLength();
ULONGLONG passCount=(ULONGLONG)pow((double)inStrLen,m);
for (ULONGLONG i=0;i<passCount;i++)
{
for (int t=1;t<m;t++)
{
if (flag[m-t]>0&&flag[m-t]%inStrLen==0)// This place is a bit of a mouthful, and I forget how I came up with the idea.
{
flag[m-t-1]++;
flag[m-t]=0;
}
}
CString str=L"";
for (int j=0;j<m;j++)
{
str+=inStr.GetAt(flag[j]);
}
flag[m-1]++;
str+=L"\n";
fp.WriteString(str);
passFlag++;
int persent=int((float)passFlag/passScore*100);
if (persent%5==0)
{
m_progressCtrl.SetPos(persent);
}
}
delete [] flag;
}
The rate of generation is not very fast, about 20,000 per second, which is basically useless. For example, it takes about 12 hours to generate a 5-bit password at this speed, and the number of passwords is 916132832. If it is 6-bit or 7-bit, it is basically useless and inefficient. Hopefully the next step can be done in multiple threads, one faster.