Question

What input will successfully validate this program? (Please show exactly how you did it, and what...

What input will successfully validate this program? (Please show exactly how you did it, and what the input will be to validate it)

#!/usr/bin/env python

import sys

def verify(guess):
vals = [
       130,
       154,
       136,
       252,
       131,
       157,
       155,
       137,
       252,
       229,
       225,
       227,
       229
]

if len(guess) != 13:
return False

for i, c in enumerate(guess):
if (ord(c) ^ 209) != vals[i]:
return False
return True


if len(sys.argv) != 1:
print 'Usage: python check.pyc'
exit(1)

guess = raw_input("Enter your guess for the flag: ");

if verify(guess):
print "That's the correct flag!"
else:
print "Wrong flag."

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your Python Code ::

After keenly observing the code we can find,

(1) The above code is asking for an input (raw_input in python2) and it is calling the defined function named guess.There are already few values which are stored in a list we can observe that. And now the function is checking the len of the input string if it is not equal to 13 it is simply returning false and which in further will be returned to,

if verify(guess): //if len of the string is not equal to 13 it returns False.

Then the if statement is validated as,

if False: // Means it is False so the following statements in if block won't be executed and executes the statement in else block which is "Wrong Flag".

So,the first condition which need to be satisfied by the input string is it should be of length equal to 13.

Ok let's assume that we have given an input string of length 13.

The next condition is,it is enumerating the string.Let us see with an example how the for loop with enumerate works with an example.

For example I have a given a string of length 13. Let the string be 'ABCDEFGHIKLMN'

So, i is having the values of 0 to len(str)-1. And c is having the each chars in the string.

Ok next,the big thing. Here vals = [130,154,136,252,131,157,155,137,252,229,225,227,229] is,

if (ord(c) ^ 209) != vals[i]:
return False
return True

^ is the XOR operation.

ord(c),c has each char of the str. ord(c) is nothing but,ASCII value of the char. And it is been XORed with 209.

Let's see how the XOR operation works,

For example x^y if the corresponding bits in both x and y are same(both 0 are both 1) then it returns 0,if they are different(x is 0 and y is 1 (or) x is 1 and y is 0) it gives 1.

Truth Table for XOR operation ::

So, now let's see for our own example

Let c = 'A',ord(c) is 65 now,XOR it with 209.

Binary of 65 1 0 0 0 0 0 1
Binary of 209 1 1 0 1 0 0 0 1
Result :: 1 0 0 1 0 0 0 0

After,the XOR operation the result is 10010000. Converting into decimal form we get 144.

Let's see the same with python code.

So,now coming the condition in the code.

if (ord(c) ^ 209) != vals[i]:
return False
return True

(2) We need to give such type of string which ord is when XORed with 209 is equal to vals[i],then only it returns True.

So,we need to find the string which satisfies the above 2 conditions.One is it's length must be equal to 13.And the other is the above condition.

Ok so now our condition is,

Let a be the string and x be the each char in the string.We need to find the string which is equal to vals[i] after each iteration.

(ord(c) ^ 209) == vals[i]: //Only then it returns True to the called function.

vals = [130,154,136,252,131,157,155,137,252,229,225,227,229]

(ord(c) ^ 209) == vals[i]

This condition can be written as ::

ord(c) == vals[i] ^ 209

So for first iteration the value of vals[i] is 130.So,130^209 is 83. And chr(83) is 'S'

So doing the same for all the values in vals using python2 gives us.

Yep! We found the string which satisfies the above two conditions.Hence it returns True and prints us "That's the correct flag!".

Here I named the py file as check.py

Thanks. Happy Chegging.

Add a comment
Know the answer?
Add Answer to:
What input will successfully validate this program? (Please show exactly how you did it, and what...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a graph in python for a given input series without using any libraries

    I was asked this question:Given any input series a corresponding graph must be generated without the use of any libraries.After, trying my best, I arrived at this solution to which they replied it had a logical issue.# Create the matrix print("Enter the sequence with spaces: ") arr = list(map(int, input().split())) count = len(arr) rows = int(sum(arr))  cols = int(sum(arr) + 4) content = [[" "]*cols for _ in range(rows)] maxq = 0 maxp = 0 content[0][0] = "/" # Apply the positions in the matrix p = 0 q = arr[0] k = 0 for l in range(q):     if (k != q):         content[k][p] = "/"         p = p + 1         k = k + 1 p = q flag = 1 i = 0 j = 0 k = 0 c = 0 temp = 0 r = 0 flag = 1 for i,j in enumerate(arr):     c = c + 1     if c < count:         k = arr[i+1]     else:         k = 0     if arr[i]:         if flag == 1:             content[q][p] = "/\\"             if maxq < q:                 maxq = q                 maxp = p             qori = q             pori = p             p = p + k             temp = q - k...

  • Throughout this script, can you provide helpful comments about how each function works in the script...

    Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process. Also are there any errors in this script or a way to improve this script? Any help would be appreciated. Thank you. THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR! #!/usr/bin/env python # file=input("Please provide fasta file with 2 sequences:") match=float(input('What is the match score?:')) missmatch=float(input('What is the missmatch score?:')) gap=float(input('What is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT