CSDN博客

img xkou

xkou密码穷举类

发表于2004/10/12 22:10:00  1144人阅读

分类: C/C++/SDK

今天写了一个密码穷举类,可以设定密码字符,设定开始穷举的密码,提供了保存进度的载入进度的办法
下面是代码
#include <iostream>
#include <fstream>
#include <string>

#define CHARNUM 62
using namespace std;

class xkPassWord{
    
 public:
    xkPassWord(int digit);
    ~xkPassWord();
    string xkPassWord::getnewpass();
    bool   setpass(string);
    string getpass();
    bool save();
    bool load();
     
 private:
     char pwd[CHARNUM];
    string currentpassword;
    char *filename;
     int getcharpos(char ch);
    
};

xkPassWord::~xkPassWord(){
    delete [] filename;
}

xkPassWord::xkPassWord(int digit){
    filename=new char[20];
    strcpy(filename,"data.dat");
    if(digit<1) digit=1;
    //使用的密码字符
    strcpy(pwd,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    currentpassword.append(digit,'0');
}

bool xkPassWord::save(){
    ofstream out(filename);
    if(!outreturn false;
    out<<currentpassword;
    out.close();
    return true;
}

bool xkPassWord::load(){
    ifstream in(filename);
    if(!inreturn false;
    in>>currentpassword;
    in.close();
    return true;
}
    
int xkPassWord::getcharpos(char ch){
        int c=0;
        for(;c<CHARNUM;c++)
            if(pwd[c]==chbreak;
    
        return c;
}

string xkPassWord::getpass(){
    return currentpassword;
}

bool xkPassWord::setpass(string pass){
    basic_string <char>::iterator pos;
    for(pos=pass.begin();pos!=pass.end();pos++)
        if(getcharpos(*pos)==CHARNUM) return false;
    
    currentpassword=pass;
    return true;
}
    
string xkPassWord::getnewpass(){
         basic_string <char>::iterator pos;
         int charpos;
         pos=currentpassword.end()-1;
         
         charpos = getcharpos(*pos);
         
         //cout<<"endchar "<<*pos<<endl;
         //cout<<"charpos "<<charpos<<endl;
         
         if((charpos+1)<CHARNUM)     
             *pos=pwd[charpos+1];
         else{
             *pos=pwd[0];
             while(true){
                 if(pos==currentpassword.begin()){
                         currentpassword.insert(pos,pwd[0]);
                         break;
                 }
                 pos--;
                 charpos = getcharpos(*pos);
                 if((charpos+1)==CHARNUM){
                     *pos=pwd[0];
                     continue;
                 }else{
                     *pos=pwd[charpos+1];
                     break;
                 }
            }
         }
         
         return currentpassword;
}

int findchar(char ch,char a[]){
    int len=sizeof(a)/sizeof(char);
    int c=0;
    for(;c<len;c++){
        if(a[c]==chbreak;
    }
    return c;
}

/*

int main(){
    xkPassWord pw(1);
    int c=0;
    pw.load();
    cout<<pw.getpass()<<" ";
    for(;c<2000;c++)
        cout<<pw.getnewpass()<<" ";
    if(!pw.save())
        cout<<"无法写入文件: "<<endl;
    return 0;
}
*/
阅读全文
0 0

相关文章推荐

img
取 消
img