Passwort verschlüsselugsproblem



  • Hi

    Ich hab ein kleines Problem mit einem Programm.
    Das Programm selber soll mit einem Passwort geschützt werden, damit es nicht so einfach gestartet werden soll. Das Passwort ist drei Zeichen lang, aber wenn ich ein falsches Passwort mit mehr als 3 Zeichen eingebe bleiben die überschüssigen Zeichen im Speicher vorhanden und werden in jeder neue Passwort eingabe verwendet. Wie kann ich den Speicher wieder komplett löschen ?
    Hier ist mal der Quelltext:

    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string>
    #include <conio.h>
    using namespace std;
    
    int q;
    
    int main(int argc, char **argv)
    {
        std::string pwd = "xyz";
        char eingabe[80];
        memset(eingabe, 0, 80);
        int idx=0;
      do
       {
        idx=0;
        cout << "Passwort: ";
        char c=0;
        while (c!=13)
        { // 13 wird's bei RETURN
            while (!kbhit());
            while (kbhit())
            {
                c=getch();
                if (c==8)
                {// 8 ist Backspace - lösche letztes Zeichen
                    if (idx>0)
                    {
                       eingabe[--idx]=0;
                       cout << "\b \b";
                    }
                } else if (c!=13) {  // alles außer Return aufnehmen
                    cout << "*";
                    eingabe[idx++]=c;
                    }
    
             }
        }
       }  while (eingabe != pwd);
    
          system("PAUSE");
          return 0;
     }
    

    Danke schonmal für eure Hilfe 🙂



  • #include <iostream>
    #include <string>
    
    int main() {
      std::string pwd = "xyz";
      std::string guess;
      int tries_left = 3;
    
      do {
        if(tries_left == 0)
          return -1;
        --tries_left;
    
        std::cout << "Passwort: ";
        std::getline(std::cin, guess);
      } while(guess != pwd);
    
      // ...
    }
    


  • Hi

    Danke für die schnelle Antwort 🙂
    Der gepostete Code funktioniert scheinbar nicht 😞
    Gibt es auch ne möglichkeit die Versuchsbeschräkung in den von mit geposteten Code einzufügen ?



  • #include <iostream.h> 
    #include <stdlib.h> 
    #include <string>
    #include <conio.h>
    using namespace std;
    
    int q;
    
    int main(int argc, char **argv)
    {
        std::string pwd = "xyz";
        char eingabe[80];
        memset(eingabe, 0, 80);
        int idx=0;
        int try = 0;
      do
       {
        idx=0;
        cout << "Passwort: ";
        char c=0;
        try++;
            while ((c=getch()) != 13 && idx < 2)
            {
                if (c==8)
                {
                    if (idx>0)
                    {
                       idx--; //immerhin hast du auch ein zeichen weniger im string stehen
                       eingabe[idx]=0;
                       cout << "\b \b";
                    }
                }else{
                    eingabe[idx]=c;
                    idx++;
                    cout << "*";
                }
             }
        }
       }  while (eingabe != pwd && try < 3);
    
          system("PAUSE");
          return 0;
     }
    


  • In deiner Schleife wird die Eingabe ja auch nicht wieder gelöscht,...

    #include <iostream.h> 
    #include <stdlib.h> 
    #include <string>
    #include <conio.h>
    using namespace std;
    
    int q;
    
    int main(int argc, char **argv)
    {
        std::string pwd = "xyz";
        char eingabe[80];
        memset(eingabe, 0, 80);
        int idx=0;
        int try = 0;
      do
       {
        memset(eingabe, 0, 80);   // Eingabe löschen bevor sie erneut eingegeben wird.
        idx=0;
        cout << "Passwort: ";
        char c=0;
        try++;
            while ((c=getch()) != 13 && idx < 2)
            {
                if (c==8)
                {
                    if (idx>0)
                    {
                       idx--; //immerhin hast du auch ein zeichen weniger im string stehen
                       eingabe[idx]=0;
                       cout << "\b \b";
                    }
                }else{
                    eingabe[idx]=c;
                    idx++;
                    cout << "*";
                }
             }
        }
       }  while (eingabe != pwd && try < 3);
    
          system("PAUSE");
          return 0;
     }
    

Anmelden zum Antworten