Смекни!
smekni.com

Разработка программы, генерирующей пароли пользователей (стр. 3 из 3)

#include "Random.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

Password::Password(int l)

{length = l;

passwrd = new char [length+1];

dicstr = new char [length+1];

PassAlfavit = new char [36];

HowMuchn = new int [length];

dicstr[length]='\0';

ValueOfDictionary= 134547;}

Password::~Password()

{delete(passwrd);

delete(dicstr);

delete(PassAlfavit);

delete(HowMuchn);}

void Password::GenAlfavit()

{char symbol='A';

for (int index=0;index<36;index++)

{if (index==26)

symbol='0';

PassAlfavit[index]=symbol;

symbol++;}}

char * Password::GetPassword()

{return passwrd;}

void Password::GenPass()

{Random rnd1;

for (int index =0;index<length;index++)

passwrd[index]=PassAlfavit[rnd1.NextR(35)];

passwrd[index]='&bsol;0';}

BOOL Password::TestChar()

{ClearMass(length,HowMuchn);

for (int index1=0;index1<length;index1++)

for (int index2=0;index2<length;index2++)

if (passwrd[index1]==passwrd[index2])

HowMuchn[index1]++;

if (HowMuch(length,HowMuchn))

return TRUE;

else return FALSE;}

void Password::ClearMass(int length,int * mass)

{for (int index =0;index<length;index++)

mass[index]=0;}

BOOL Password::HowMuch(int length, int *HowMuch)

{for (int index =0;index<length;index++)

if (HowMuch[index]>1)

return FALSE;

return TRUE;}

BOOL Password::TestDictionary()

{CFile Dictionary;

Try

{Dictionary.Open("length08.txt",CFile.modeRead,NULL);}

catch(CFileException *e )

{Dictionary.Abort(); // close file safely and quietly

e->~CFileException();

MessageBox(0,"Не удается открыть словарь", "ошибка", MB_APPLMODAL|MB_OK|MB_ICONSTOP);

return FALSE;}

for (int index =0;index<ValueOfDictionary;index++)

{Dictionary.Read(dicstr,8);

Dictionary.Seek(2,CFile.current);

if (Compare(length))

return FALSE;}

Dictionary.Close();

return TRUE;}

int Password::Compare(int length)

{for (int index=0;index<length;index++)

if (passwrd[index]!=dicstr[index])

return 0;

return 1;}


ПРИЛОЖЕНИЕ В

Random.cpp

// Random.cpp: implementation of the Random class.

//////////////////////////////////////////////////////////////////////

#include <windows.h>

#include <Winbase.h>

#include <stdlib.h>

#include "stdafx.h"

#include "PassGen.h"

#include "Random.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

Random::Random()

{srand(GetTickCount());}

Random::~Random()

{}

int Random::NextR(int x)

{int rnd1;

rnd1=rand();

srand(rnd1);

rnd1=rand() % x;

return rnd1;}


ПРИЛОЖЕНИЕ Г

PassGenDlg.cpp

// PassGenDlg.cpp : implementation file

#include "stdafx.h"

#include "PassGen.h"

#include "PassGenDlg.h"

#include "Password.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CPassGenDlg dialog

CPassGenDlg::CPassGenDlg(CWnd* pParent /*=NULL*/)

: CDialog(CPassGenDlg::IDD, pParent)

{//{{AFX_DATA_INIT(CPassGenDlg)

// NOTE: the ClassWizard will add member initialization here

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}

void CPassGenDlg::DoDataExchange(CDataExchange* pDX)

{CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CPassGenDlg)

// NOTE: the ClassWizard will add DDX and DDV calls here

//}}AFX_DATA_MAP}

BEGIN_MESSAGE_MAP(CPassGenDlg, CDialog)

//{{AFX_MSG_MAP(CPassGenDlg)

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDGEN, OnGen)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CPassGenDlg message handlers

BOOL CPassGenDlg::OnInitDialog()

{CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically

// when the application's main window is not a dialog

SetIcon(m_hIcon, TRUE); // Set big icon

SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control}

// If you add a minimize button to your dialog, you will need the code below

// to draw the icon. For MFC applications using the document/view model,

// this is automatically done for you by the framework.

void CPassGenDlg::OnPaint()

{if (IsIconic())

{CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle

int cxIcon = GetSystemMetrics(SM_CXICON);

int cyIcon = GetSystemMetrics(SM_CYICON);

CRect rect;

GetClientRect(&rect);

int x = (rect.Width() - cxIcon + 1) / 2;

int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon

dc.DrawIcon(x, y, m_hIcon);}

else

{CDialog::OnPaint();}}

// The system calls this to obtain the cursor to display while the user drags

// the minimized window.

HCURSOR CPassGenDlg::OnQueryDragIcon()

{return (HCURSOR) m_hIcon;}

void CPassGenDlg::OnGen()

{Password ps (8) ;

ps.GenAlfavit();

do

{ps.GenPass();

if (!ps.TestChar())

continue;

else

if (ps.TestDictionary())

break;

else

continue;}

while (TRUE);

SetDlgItemText(IDC_EDIT1,ps.GetPassword());}