№穷举密码算法指要(源码)

王朝vc·作者佚名  2006-01-08
宽屏版  字体: |||超大  

//*******************************************************************

//在许多情况下我们需要穷举组合的算法,比如密码词典。

//这个算法的关键是密码下标进位的问题。

//另外本例子中的写文件语句效率比较低,为了降低算法复杂度没有优化。

//如果要提高写文件的效率,可以使用缓冲区,分批写入。

//*********************************************breath.cnpick.com*****

void createpassword()

{

#define passwordmax 8 //将生成密码的最大长度

char a[]="0123456789abcdefghijklmnopqrstuvwxyz";

//可能的字符

long ndictcount=sizeof(a); //获得密码词典长度

char cpass[passwordmax+2]; //将生成的密码

long nminl=1,nmaxl=3; //本例中密码长度从1-3

long array[passwordmax]; //密码词典下标

assert(nminl<=nmaxl && nmaxl<=passwordmax);//容错保证

long nlength=nminl;

register long j,i=0;

bool bnext;

cstdiofile file;

file.open("c:\\dict.txt",cfile::modecreate|cfile::modewrite);

while(nlength<=nmaxl)

{

for(i=0;i<passwordmax;i++)

array[i]=0;

bnext=true;

while(bnext)

{

for(i=0;i<nlength;i++)

cpass[i]=a[array[i]];

cpass[i]='\0';

file.writestring(cpass);

file.writestring("\n");

for(j=nlength-1;j>=0;j--) //密码指针进位

{

array[j]++;

if(array[j]!=ndictcount-1)break;

else

{

array[j]=0;

if(j==0)bnext=false;

}

}

}

nlength++;

}

file.close();

}

在下小站 ☆编程先锋☆

http://wlbookwl.myrice.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有