Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Baza danych

Ostatnio zmodyfikowano 2024-06-16 01:06
Autor Wiadomość
omura
Temat założony przez niniejszego użytkownika
Baza danych
» 2024-06-16 00:28:18
Witam. Brak mi chwilowo pomysłu jak połączyć w jedną całość program generujący losowe hasło z bazą danych. Program główny wymaga podania loginu i hasła, następnie sprawdza wprowadzone dane i tworzy logi. Jeżeli wszystko jest ok następuje wygenerowanie hasła i zapisanie jego w logach. W bazie danych po zalogowaniu można dodać 2 użytkowników i przypisać im hasła. Wszytko jest później zapisywane do pliku. Problem polega na tym, że nie wiem jak to połączyć razem w jedną całość, aby miało to ręce i nogi. Zasada działania gotowego programu powinna być następująca: logowanie do programu (sprawdzenie poprawności i zapis do pliku) --> generowanie hasła (zapis do pliku) --> logowanie do bazy danych (sprawdzenie poprawności i zapis do pliku) --> dodanie użytkowników w bazie danych. Jeżeli ktoś może pomóc, będę wdzięczny.
Generowanie hasła
C/C++
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <ctime>
#include <cstdlib>


using namespace std;

void welcome()
{
   
cout << endl;
   
cout << "\t\t Welcom in my world." << endl;
   
cout << "\t\t Say hello if you want to be my friend." << endl;
   
cout << endl;
}

//Logowanie
void readPassword()
{
   
//Logowanie
   
string login_adm = "admin";
   
string password_adm = "test";
   
   
cout << "\t\t Enter your login and password: " << endl;
   
cout << "Login: " << endl;
   
cin >> login_adm;
   
cout << endl;
   
   
//ukrycie hasła pod "*"
   
cout << "Password: " << endl;
   
password_adm = "";
   
char ch = _getch();
   
while( ch != 13 )
   
{
       
password_adm.push_back( ch );
       
cout << '*';
       
ch = _getch();
   
}
   
//Info o dostępie lub odmowie
   
ofstream accessInfo( "Access_or_Denied_Info.txt", ios_base::app );
   
if( login_adm == "admin" && password_adm == "test" )
   
{
       
accessInfo << "Access Granted\n" << "Date: " << __DATE__ << endl;
       
accessInfo << "User: " << login_adm << endl;
       
accessInfo << "Time: " << __TIME__ << endl;
       
accessInfo << "-----------------------------" << endl;
       
   
}
   
else
   
{
       
accessInfo << "Access Denied\n" << "Date: " << __DATE__ << endl;
       
accessInfo << "User: " << login_adm << endl;
       
accessInfo << "" << "Time: " << __TIME__ << endl;
       
accessInfo << "-----------------------------" << endl;
   
}
   
   
accessInfo.close();
   
   
cout << "\t\t\tAccess information saved to file.\n";
   
   
string granted = "Access_Granted";
   
string denied = "Access_Denied";
   
   
//Zapis do pliku
   
   
fstream file;
   
file.open( "User_Data.txt", ios_base::app );
   
file << "User_Login: " << login_adm << endl;
   
file << "User_Password: " << password_adm << endl;
   
file << "-----------------------------" << endl;
   
file.close();
   
   
ofstream userFile( "User_File.txt", ios_base::app );
   
userFile << "User: " << login_adm << endl;
   
userFile << "Date: " << __DATE__ << endl;
   
userFile << "Time: " << __TIME__ << endl;
   
userFile << "-----------------------------" << endl;
   
   
userFile.close();
   
   
if(( login_adm == "admin" ) &&( password_adm == "test" ) )
   
{
       
cout << "\t\t\t\t Access granted." << endl;
       
file << granted << endl;
   
}
   
else
       
 cout << "\t\t\t\t Access denied. " << endl;
   
   
if(( login_adm != "admin" ) &&( password_adm == "test" ) ||( login_adm == "admin" ) &&( password_adm != "test" ) ||( login_adm != "admin" ) &&( password_adm != "test" ) )
   
{
       
file << denied << endl;
   
}
   
}

//Generowanie losowego hasła
string generatePassword( int minLength = 6, int maxLength = 8 )
{
   
string charset = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*?";
   
int charLength = charset.length();
   
int length = rand() - 100 %( maxLength - minLength + 1 ) + minLength;
   
string new_password_generate;
   
   
while( true )
   
{
       
string new_password_generate = "";
       
for( int i = 1; i < length; ++i )
       
{
           
new_password_generate += charset[ rand() - 100 % charLength ];
       
}
       
       
if( new_password_generate.find_first_of( "0123456789" ) != string::npos &&
       
new_password_generate.find_first_of( "qwertyuiopasdfghjklzxcvbnm" ) != string::npos &&
       
new_password_generate.find_first_of( "QWERTYUIOPASDFGHJKLZXCVBNM" ) != string::npos &&
       
new_password_generate.find_first_of( "!@#$%^&*?" ) != string::npos )
       
       
       
{
           
int specialCount = 0;
           
for( char c: new_password_generate )
           
{
               
if( !isalnum( c ) )
               
{
                   
specialCount++;
               
}
            }
           
if( specialCount >= 3 )
           
{
               
break;
           
}
        }
    }
   
   
return new_password_generate;
}

void passwdFile()
{
   
string new_password_generate = generatePassword();
   
   
ofstream passwdFile( "Generate_password.txt", ios_base::app );
   
if( passwdFile.is_open() )
   
{
       
passwdFile << "User: Admin ----- " << "New password: " << generatePassword << "  " << __DATE__ << " " << __TIME__ << endl;
       
passwdFile << "--------------------------------------------------------------------------------------" << endl;
       
passwdFile.close();
   
}
   
}


int main()
{
   
//Powitanie
   
welcome();
   
   
//Logowanie
   
readPassword();
   
   
//Generowanie losowego hasła - zapis do pliku
   
srand( time( NULL ) );
   
passwdFile();
   
   
   
   
cout << endl;
   
system( "pause" );
   
return 0;
}
Baza danych
C/C++
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <ctime>
#include <cstdlib>

using namespace std;

void displayWelcomeMessage()
{
   
cout << "-------------------------------------------------------------------" << endl;
   
cout << "Welcome to the user database. Please enter your login and password." << endl;
   
cout << "-------------------------------------------------------------------" << endl;
}

void getUserCredentials( string & loginUser, string & passwordUser )
{
   
cout << "Enter your login: ";
   
cin >> loginUser;
   
   
cout << "Enter your password: ";
   
char c;
   
passwordUser = "";
   
while(( c = _getch() ) != '\r' )
   
{
       
passwordUser.push_back( c );
       
cout << '*';
   
}
   
cout << endl;
}

void saveUserDataToFile( const string & loginUser, const string & passwordUser )
{
   
ofstream file( "User_Data_Admin.txt", ios_base::app );
   
if( file.is_open() )
   
{
       
file << "Login: " << loginUser << endl;
       
file << "Password: " << passwordUser << endl;
       
file << __DATE__ << " " << __TIME__ << endl;
       
file << "-------------------------------------------------------------------" << endl;
       
file.close();
   
}
}

void checkAccess( const string & loginUser, const string & passwordUser )
{
   
bool hasAccess =( loginUser == "admin" && passwordUser == "admin123" );
   
ofstream accessFile( "Access_Info_Admin.txt", ios_base::app );
   
if( accessFile.is_open() )
   
{
       
accessFile << "Access: " <<( hasAccess ? "Granted": "Denied" ) << endl;
       
accessFile << __DATE__ << " " << __TIME__ << endl;
       
accessFile << "-------------------------------------------------------------------" << endl;
       
accessFile.close();
   
}
}

void addNewUserAndPassword()
{
   
string newLogin, newPassword;
   
cout << "Enter new user login: ";
   
cin >> newLogin;
   
   
cout << "Enter new user password: ";
   
char c;
   
newPassword = "";
   
while(( c = _getch() ) != '\r' )
   
{
       
newPassword.push_back( c );
       
cout << '*';
   
}
   
cout << endl;
   
   
ofstream file2( "New_user.txt", ios_base::app );
   
if( file2.is_open() )
   
{
       
file2 << "New User Login: " << newLogin << endl;
       
file2 << "New User Password: " << newPassword << endl;
       
file2 << __DATE__ << " " << __TIME__ << endl;
       
file2 << "-------------------------------------------------------------------" << endl;
       
file2.close();
   
}
   
}

/* Wyświetlanie danych nowego użytkownika po ich wprowadzeniu
void displayFileContent(const string& filename)
{
    ifstream file(filename);
    if (file.is_open())
    {
        string line;
        while (getline(file, line))
        {
            cout << line << endl;
        }
        file.close();
    }
}*/

int main()
{
   
   
displayWelcomeMessage();
   
   
string loginUser, passwordUser;
   
getUserCredentials( loginUser, passwordUser );
   
   
saveUserDataToFile( loginUser, passwordUser );
   
checkAccess( loginUser, passwordUser );
   
   
addNewUserAndPassword();
   
addNewUserAndPassword();
   
   
/*  Wyświetlanie danych nowego użytkownika po ich wprowadzeniu cd.
    displayFileContent("New_user.txt");
    */
   
   
return 0;
}
P-181240
DejaVu
» 2024-06-16 01:04:25
Hasła nigdy nie umieszcza się w logach.
P-181241
pekfos
» 2024-06-16 01:06:49
Problem polega na tym, że nie wiem jak to połączyć razem w jedną całość, aby miało to ręce i nogi. Zasada działania gotowego programu powinna być następująca: logowanie do programu (sprawdzenie poprawności i zapis do pliku) --> generowanie hasła (zapis do pliku) --> logowanie do bazy danych (sprawdzenie poprawności i zapis do pliku) --> dodanie użytkowników w bazie danych.
Ta zasada działania nie ma rąk i nóg. Odpowiedzią na pytanie jest "skopiuj dwie funkcje i wywołaj", powinieneś sobie z tym poradzić, ale to ma tak mało sensu że raczej źle zadajesz pytanie.
P-181242
« 1 »
  Strona 1 z 1