Page 1 of 1

how to find an encrypted key?

PostPosted: Wed Jun 10, 2015 10:40 pm
by ghostheadx2
So, I have this python file. I'm trying to make the key appear. I'm not asking for it decrypted, but I want to print the encrypted key. I'm wondering if I could do this by writing the key to another file so that when I open that file it appears. When I try to run it right now, I get:

Code: Select all
Usage: ZOR.py [encrypt/decrypt] [in_file] [out_file] [password]


Here is the file:

Code: Select all
#!/usr/bin/python

import sys

"""
Daedalus Corporation encryption script.
"""

def xor(input_data, key):
    result = ""
    for ch in input_data:
        result += chr(ord(ch) ^ key)

    return result

def encrypt(input_data, password):
    key = 0
    for ch in password:
        key ^= ((2 * ord(ch) + 3) & 0xff)

    return xor(input_data, key)

def decrypt(input_data, password):
    return encrypt(input_data, password)

def usage():
    print("Usage: %s [encrypt/decrypt] [in_file] [out_file] [password]" % sys.argv[0])
    exit()

def main():
    if len(sys.argv) < 5:
        usage()

    input_data = open(sys.argv[2], 'r').read()
    result_data = ""

    if sys.argv[1] == "encrypt":
        result_data = encrypt(input_data, sys.argv[4])
    elif sys.argv[1] == "decrypt":
        result_data = decrypt(input_data, sys.argv[4])
    else:
        usage()

    out_file = open(sys.argv[3], 'w')
    out_file.write(result_data)
    out_file.close()

main()



I feel like whatever I'm doing isn't working so I gave the original, unedited, file so that maybe someone could give me a hint. Here's the current hint:

The password gets reduced to a one byte XOR key. That's only 256 possible keys!


-- Wed Jun 10, 2015 10:51 pm --

I got it. I just need to get the XOR key. Then I can decrypt the key. How do I get the XOR key from a python file?

Re: how to find an encrypted key?

PostPosted: Wed Jun 10, 2015 11:20 pm
by Goatboy
Here's a hint:

The password gets reduced to a one byte XOR key. That's only 256 possible keys!

Think about what this is saying. The key is one byte. A byte is 8 bits. There are 256 combinations of 8 bits, so there are 256 things that could be considered a single byte. As far as brute force goes, this is about as easy as it gets.

Re: how to find an encrypted key?

PostPosted: Thu Jun 11, 2015 3:00 pm
by ghostheadx2
But I don't know how to brute force.

-- Thu Jun 11, 2015 3:00 pm --

I guess I just have to Google "how to brute force" then?

Re: how to find an encrypted key?

PostPosted: Fri Jun 12, 2015 12:13 am
by Goatboy
That might get you close, but do you know how XOR works? Do you know the difference between a "key", "plaintext", and "ciphertext"? Studying up on basic encryption would be a good start.

Re: how to find an encrypted key?

PostPosted: Sat Jun 13, 2015 10:47 pm
by ghostheadx2

Re: how to find an encrypted key?

PostPosted: Sun Jun 14, 2015 12:24 am
by JackBurden
This a fun fairly easy programming challenge that will teach you the basics of XOR
https://projecteuler.net/problem=59 "XOR Decryption"

Re: how to find an encrypted key?

PostPosted: Thu Jun 18, 2015 10:35 pm
by sanddbox
ghostheadx2 wrote:But I don't know how to brute force.

-- Thu Jun 11, 2015 3:00 pm --

I guess I just have to Google "how to brute force" then?


If every iPhone passcode was only 1 number long, how would you get into somebody's phone? Yes, you don't know their passcode, but it takes you 1 second to input a guess, and there's only 10 possible guesses...

Now imagine the same scenario, except there's 256 possible passwords, but you have this special device called a computer to do the guessing for you, and computers are fast, so each guess only takes around 10 milliseconds. The password would be easy to find, right?

I suspect you don't understand binary and binary operations (like XOR) well enough, otherwise the solution would be obvious. All you need to figure out is how to generate every possible solution, and then how to check each possibility to see if it's right.

Re: how to find an encrypted key?

PostPosted: Fri Jun 19, 2015 2:24 am
by ghostheadx2
I don't understand them well enough. I have to read up on some stuff I guess. I'm finding cryptography tutorials to do so I'm getting on it.

-- Fri Jun 19, 2015 2:34 am --

would cryptography tutorials help?

Re: how to find an encrypted key?

PostPosted: Sun Jun 21, 2015 8:59 pm
by sanddbox
To a certain extent, but "cryptography tutorials" is very vague. For this problem, you just need a basic grasp of programming, an understanding of binary (including operations like XOR), and knowledge of the meaning of the words "brute" and "force".