http://bugeyes.blog.edu.cn/user1/20989/archives/2006/1113349.shtml
思路很简单:
1)选择用来生成密码的字符
2)设置位数
3)计算组合
4)计算排列
头文件:
//---------------------------------------------------------------------------
#ifndef mimaH
#define mimaH
//---------------------------------------------------------------------------
#i nclude <Classes.hpp>
#i nclude <Controls.hpp>
#i nclude <StdCtrls.hpp>
#i nclude <Forms.hpp>
#i nclude <Dialogs.hpp>
#i nclude <stdio.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TGroupBox *GBMode;
TCheckBox *CBBiaodian;
TGroupBox *GBDigit;
TCheckBox *CBFive;
TCheckBox *CBSix;
TCheckBox *CBSeven;
TCheckBox *CBEight;
TCheckBox *CBNine;
TCheckBox *CBTen;
TCheckBox *CBEleven;
TCheckBox *CBTwelve;
TCheckBox *CBThirteen;
TCheckBox *CBFourteen;
TSaveDialog *SaveDialog1;
TButton *BtnOK;
TCheckBox *CBDigit;
TCheckBox *CBAlphaSmall;
TCheckBox *CBAlphaBig;
TButton *BtnCheck;
void __fastcall BtnOKClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall BtnCheckClick(TObject *Sender);
private:
char digit[11]; //数字符号,空余5个位置用来存放标点
char bigalpha[27]; //大写字母
char smallalpha[27]; //小写字母
char biaodian[6]; //标点符号
char result[20];//存放密码
char current[80];//当前用来生成密码的字符数组
int count;//用来生成密码的字符个数,即current数组大小
int r;// 当前密码位数
FILE *fp;//文件指针,指向字典文件
void __fastcall zuhe(int m,int k);//计算组合
void __fastcall pailie();//计算排列,并输出到密码文件
void __fastcall sort(char arr[],int start,int end);//排序函数
int __fastcall next(char arr[],int n);//计算机下一个排列
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
cpp文件:
//---------------------------------------------------------------------------
#i nclude <vcl.h>
#pragma hdrstop
#i nclude "mima.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnOKClick(TObject *Sender)
{
if(BtnCheck->Tag==0)
{
ShowMessage("请先使用验证功能");
return;
}
BtnCheck->Tag =0;
BtnOK->Enabled =false;
// ShowMessage(current);
SaveDialog1->Title ="请选择存放位置__BugEyes";
ShowMessage("请选择将文件存放于有较大空间的分区");
if(!SaveDialog1->Execute())
{
ShowMessage("生成失败,因为无法创建文件,请重新生成");
BtnOK->Enabled =true;
return;
}
if((fp=fopen(SaveDialog1->FileName.c_str(),"a"))==NULL)
{
MessageBox(NULL,"打开文件失败","信息",MB_OK);
return;
}
ShowMessage("由于您选择了较多的密码,程序运行时间可能较长。\n请耐心等待!\n请单击确定按钮开始计算密码");
if(CBFive->Checked)
{
r=1;
zuhe(count,r);
r=2;
zuhe(count,r);
r=3;
zuhe(count,r);
r=4;
zuhe(count,r);
r=5;
zuhe(count,r);
}
if(CBSix->Checked)
{
r=6;
zuhe(count,r);
}
if(CBSeven->Checked)
{
r=7;
zuhe(count,r);
}
if(CBEight->Checked)
{
r=8;
zuhe(count,r);
}
if(CBNine->Checked)
{
r=9;
zuhe(count,r);
}
if(CBTen->Checked)
{
r=10;
zuhe(count,r);
}
if(CBEleven->Checked)
{
r=11;
zuhe(count,r);
}
if(CBTwelve->Checked)
{
r=12;
zuhe(count,r);
}
if(CBThirteen->Checked)
{
r=13;
zuhe(count,r);
}
if(CBFourteen->Checked)
{
r=14;
zuhe(count,r);
r=15;
zuhe(count,r);
r=16;
zuhe(count,r);
r=17;
zuhe(count,r);
r=18;
zuhe(count,r);
}
fclose(fp);
BtnOK->Enabled =true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
current[0]='\0';//清空密码字符数组
for(int i=0;i<11;i++) //产生数字数组
digit[i]='0'+i;
digit[10]='\0';
for(int i=0;i<27;i++) //产生字母数组
{
bigalpha[i]='A'+i;
smallalpha[i]='a'+i;
}
bigalpha[26]='\0';
smallalpha[26]='\0';
biaodian[0]=',';biaodian[1]='.';biaodian[2]=';'; //产生标点数组
biaodian[3]=':';biaodian[4]='?';biaodian[5]='\0';
BtnOK->Enabled =false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::zuhe(int m,int k)
{
int i,j;
for(i=m;i>=0;i--) //允许字符有重复
{
result[k-1]=current[i];
if(k>1)
zuhe(i,k-1);
else
{
result[r]='\0';
// ShowMessage(result);
pailie();
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::pailie()
{
char temp[20];
strcpy(temp,result); //存储原始数据
sort(result,0,r-1);
do
{
fputs(result,fp);
fprintf(fp,"\n");
}while(next(result,r-1)!=-1);
strcpy(result,temp); //恢复原始数据,这2句非常关键,
//因为next函数修改了result内容,从而影响结果
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sort(char arr[],int start,int end)
{
int i,j;
for(i=0;i<end-start;i++)
for(j=start;j<=end-1;j++)
if(arr[j]>arr[j+1])
{
char t;
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
//---------------------------------------------------------------------------
int __fastcall TForm1::next(char arr[],int n)
{
int i,j;
char temp;
for(i=n-1;i>=0;i--)
if(arr[i]<arr[i+1])
break;
if(i<0)
return -1;
for(j=n;j>i;j--)
if(arr[j]>arr[i])
break;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
sort(arr,i+1,n);
return 1;
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(fp)
fclose(fp);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnCheckClick(TObject *Sender)
{ //验证当前设置的有效性
current[0]='\0';
if(CBAlphaBig->Checked)//包含大写字母
strcat(current,bigalpha);
if(CBAlphaSmall->Checked)//包含小写字母
strcat(current,smallalpha);
if(CBDigit->Checked) //包含数字
strcat(current,digit);
if(CBBiaodian->Checked)//包含标点
strcat(current,biaodian);
count=strlen(current)-1;
//ShowMessage(IntToStr(count));
if(count<17)
CBFourteen->Enabled =false;
else
CBFourteen->Enabled =true;
if(count<12)
CBThirteen->Enabled =false;
else
CBThirteen->Enabled =true;
if(count<11)
CBTwelve->Enabled =false;
else
CBTwelve->Enabled =true;
if(count<10)
CBEleven->Enabled =false;
else
CBEleven->Enabled =true;
if(count<9)
CBTen->Enabled =false;
else
CBTen->Enabled =true;
if(count<8)
CBNine->Enabled =false;
else
CBNine->Enabled =true;
if(count<7)
CBEight->Enabled =false;
else
CBEight->Enabled =true;
if(count<6)
CBSeven->Enabled =false;
else
CBSeven->Enabled =true;
if(count<5)
CBSix->Enabled =false;
else
CBSix->Enabled =true;
if(count<4)
{
CBFive->Enabled =false;
BtnOK->Enabled =false;
ShowMessage("请选择字典模式");
return;
}
else
{
CBFive->Enabled =true;
BtnOK->Enabled =true;
}
BtnCheck->Tag =1;
}
//---------------------------------------------------------------------------
软件运行截图:
在我的blog上面有工程源代码下载