dimanche 13 mars 2022

How can I use a loop to match every four characters of a password?

I´m new here as well as on programming, so sorry for any simple mistake. I have a problem because I need to write a code to find every four digits of a password. The problem is :

  • my target password is 4 characters long, so you'll just have to brute force 1 character at a time
  • I don´t need to guess the password!
  • I need to create a loop that only stops when all 4 characters have been guessed. On each loop iteration I need to calculate a random number between 1 and 3, which will correspond to each of the bellow scenarios:
  1. I guessed one character correctly, which increases guessesNumber by 1 and prints the message 'Founded X characters'

  2. I guessed incorrectly and your target's terminal has detected too many attempts, which resets guessesNumber to 0 and prints the message 'Starting over' to the console.

  3. I guessed incorrectly, but have not been detected yet, guessesNumber is kept with the same value.

Once the password is cracked (that is, guessesNumber has a value of 4) you should print the message 'hacked!'.

To do this I start:

var guessesNumber = 0;
var pass_char1 = Math.floor(Math.ceil(Math.random()*3));
var pass_char2 = Math.floor(Math.ceil(Math.random()*3));
var pass_char3 = Math.floor(Math.ceil(Math.random()*3));
var pass_char3 = Math.floor(Math.ceil(Math.random()*3));
var triesValues = Math.floor(Math.ceil(Math.random()*3));
var tries = 0

while (guessesNumber < 4) {
triesValue = Math.floor(Math.ceil(Math.random()*3)); tries++ ;

if (pass_char1 === triesvalue) {guessesNumber++; console.log('Founded ' + guessesNumber + ' characters'); pass_char1= 0}

else if (pass_char2 === triesvalue) {guessesNumber++; console.log('Founded ' + guessesNumber + ' characters'); pass_char2= 0}

else if (pass_char3 === triesvalue) {guessesNumber++; console.log('Founded ' + guessesNumber + ' characters'); pass_char3= 0}

else if (pass_char4 === triesvalue) {guessesNumber++; console.log('Founded ' + guessesNumber + ' characters'); pass_char4= 0}

else if (triesValue !== (pass_char1 || pass_char2 || pass_char3 || pass_char4) && tries > 5){guessesNumber = 0; console.log('Starting Over'); tries = 0;}

else {guessesNumber=guessesNumber}

}

if(guessesNumber===4){console.log('hacked!');}

Sorry for the long message! Anyone can help me and explain me what I´m doing wrong??




Aucun commentaire:

Enregistrer un commentaire