Brute Force in python [closed]

2024/11/18 12:43:59

I am trying loop the decrypted message 10 times, but I am getting very different results, I don't know why. Below is my code. I have used the same code for encrypting and decrypting. I only had to change the newchar = newchar-shfit. Using the same code for looping gets me very different results.

  num1=0shift=0print("Brute force")question=input(print("Please enter something to decrypt: "))decryptedword = ""while num1<10 and shift <10 :for character in question:newchar = ord(character)newchar = newchar - shiftif newchar < 32:newchar = newchar + 95elif newchar > 126:newchar = newchar - 95decryptedword = decryptedword + chr(newchar)print(num1,"decrypted word: ",decryptedword)num1=num1+1shift=shift+1

For example if type in asdasd in the input, I get:

1 decrypted word: a

2 decrypted word: ar

3 decrypted word: arb

4 decrypted word: arb^

5 decrypted word: arb^o

6 decrypted word: arb^o_

OK, sample output on how it should be, if I input dvg, I should get:

1 decrypted word:fxi

2 decrytped word:asd

3 decrypted word:ewh

and so on..

Answer

Well you are increasing the shift value with every loop iteration, and therefore altering your key with each iteration. And since you are incrementing your key (shift) with every iteration your decrypted result is also different in each iteration.

If you remove this line shift=shift+1 the decrypted message should be the same with every iteration.
(EDIT3: I do not know how to 'strike through' text, but the above lines should be ignored, as I had misunderstood the authors problem.)

EDIT: And another note, your indentation seems to be wrong. The if ; elif statement is indented as if they are NOT part of the loop, which also causes unexpected behavior.
EDIT2: Furthermore decryptedword = "" Should be inside the loop so it gets reset with every iteration.

   num1=0shift=0print("Brute force")question=input(print("Please enter something to decrypt: "))while num1<10 and shift <10 :decryptedword = ""for character in question:newchar = ord(character)newchar = newchar - shiftif newchar < 32:newchar = newchar + 95elif newchar > 126:newchar = newchar - 95decryptedword = decryptedword + chr(newchar)print(num1,"decrypted word: ",decryptedword)num1=num1+1shift=shift+1
https://en.xdnf.cn/q/120071.html

Related Q&A

Choosing only non-zeros from a long list of numbers in text file

I have a text file with a long list of numbers. I would like to choose only the non-zeros and make another text file. This is a portion of the input file:0.00000E+00 0.00000E+00 0.00000E+00 0.00000…

Why can I not plot using Python on repl.it

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).The codeimport numpy as np import matplotlib.pyplot as plttime = np.array([0,…

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python program that rolls a fair die counts the number of rolls before a 6 shows up

import randomsample_size = int(input("Enter the number of times you want me to roll the die: "))if (sample_size <=0):print("Please enter a positive number!")else:counter1 = 0coun…

How to read CSV file in Python? [duplicate]

This question already has answers here:How do I read and write CSV files?(9 answers)Closed 1 year ago.Im using Spyder for Python 2.7 on Windows 8. Im trying to open and read a csv file and see all the…

How to match the bundle id for android app?

Id like to match the urls like this:input: x = "https://play.google.com/store/apps/details?id=com.alibaba.aliexpresshd&hl=en"get_id(x)output: com.alibaba.aliexpresshdWhat is the best way…

Memory Usage During running a Deep learning CNN Model in Colab

I am conducting a research which requires me to know the memory used during run time by the model when i run a deep learning model(CNN) in google colab. Is there any code i can use to know the same .Ba…

Gensim example, TypeError:between str and int error

When running the below code. this Python 3.6, latest Gensim library in Jupyterfor model in models:print(str(model))pprint(model.docvecs.most_similar(positive=["Machine learning"], topn=20))[1…

How to interpret this JSON file?

Im trying to interpret this JSON file but I couldnt figure it out. {"results": [{"fsq_id": "4dc586fbcc3ff3b3045e2ef3","categories": [{"id": 17056,"…

How to extract a field from this payload with a regex? [duplicate]

This question already has answers here:Parse JSON with Python(2 answers)Closed 6 years ago.I have this payload that I wish to extract a field from:{"encrypted_sender_transaction_id":"514…