file handling issues

L

leo.carnovale

I am making this little game and I am trying to make some sort of script that does the following:
Checks to see if a file exists
If it does, check the numbers in it
If it doesn't, make one and fill it with some numbers
Sorts out if the numbers in the file are in the right format
If they are, then store them
If not, remake it again
THEN, check if there is a pass.
If there is, check if it fits the key and go on if so
If not, ask for a new one, check it, go on if its correct.

Every time I run it, it says the key is 0!?
I can not for the life of me figure out what it is doing.

The code:
<https://docs.google.com/document/d/1_Qo4bgmNhRer3fAt3EAtPNSei4N2q58f_q4_9r6OHbw/edit?usp=sharing>

Thanks

Leo
 
L

leo.carnovale

I am making this little game and I am trying to make some sort of script that does the following:












Every time I run it, it says the key is 0!?

I can not for the life of me figure out what it is doing.



The code:

<https://docs.google.com/document/d/1_Qo4bgmNhRer3fAt3EAtPNSei4N2q58f_q4_9r6OHbw/edit?usp=sharing>



Thanks



Leo

Probably should also mention this,
The KEY is what the user is presented with, and to use the program, they must enter a corresponding PASS which is created with a special manipulator that I made.
 
P

Piet van Oostrum

I am making this little game and I am trying to make some sort of script that does the following:

Every time I run it, it says the key is 0!?
I can not for the life of me figure out what it is doing.

The code:
<https://docs.google.com/document/d/1_Qo4bgmNhRer3fAt3EAtPNSei4N2q58f_q4_9r6OHbw/edit?usp=sharing>

What a mess is that code. First that messing around with the key is
crazy. You probably want to do some crypto on it, but why don't you just
use some standard crypto algorithm?

The you have defined T as True and F as False, but sometimes you use T
and sometimes True, and as far as I can see you never use F. Just stay
with True and false as it makes the code more readable.

Once you use "with open('Key')", all other case you use
f = open('Key',...)
f.read() or f.write()
f.close()

Be consistenst and use always "with open..."

There are at least three palces where you write a key to the file. Make
a function for this; it makes your code more structured.

And please, put spaces aroud the = signs.

Now the real problem:

if newpas:
f=open("Key","w")
print("No pass found!")
print("Your wonderful key is: ",int(keys[0]))
pasw=input("What is your pass? : ")
elif newkey:
f=open("Key","w")

Here you open the file for writing but you never write anything to it.
This makes the file empty. And apparently you do not check this the next
time you open it. If you would have used a function to write a key to
the file, this probably would not have occurred.

if mess_with(keys[0])==pasw:
hesin=1
print("Your in!")
f=open("Key","w")

Here you open the file again with the same variable. This causes the old
value to be lost and the file probably to be closed. Just messy.

print("writing %s" % str(keys[0])+pasw)
f.write(str(keys[0])+pasw)
f.close()
else:
hesin=0

And how are people supposed to guess an 8 character password? Are they
supposed to do that weird calculation on their calculators or some such?
 
L

Leo Carnovale

(e-mail address removed) writes:


I am making this little game and I am trying to make some sort of script that does the following:

Every time I run it, it says the key is 0!?
I can not for the life of me figure out what it is doing.

The code:



What a mess is that code. First that messing around with the key is

crazy. You probably want to do some crypto on it, but why don't you just

use some standard crypto algorithm?



The you have defined T as True and F as False, but sometimes you use T

and sometimes True, and as far as I can see you never use F. Just stay

with True and false as it makes the code more readable.



Once you use "with open('Key')", all other case you use

f = open('Key',...)

f.read() or f.write()

f.close()



Be consistenst and use always "with open..."



There are at least three palces where you write a key to the file. Make

a function for this; it makes your code more structured.



And please, put spaces aroud the = signs.



Now the real problem:



if newpas:

f=open("Key","w")

print("No pass found!")

print("Your wonderful key is: ",int(keys[0]))

pasw=input("What is your pass? : ")

elif newkey:

f=open("Key","w")



Here you open the file for writing but you never write anything to it.

This makes the file empty. And apparently you do not check this the next

time you open it. If you would have used a function to write a key to

the file, this probably would not have occurred.



if mess_with(keys[0])==pasw:

hesin=1

print("Your in!")

f=open("Key","w")



Here you open the file again with the same variable. This causes the old

value to be lost and the file probably to be closed. Just messy.



print("writing %s" % str(keys[0])+pasw)

f.write(str(keys[0])+pasw)

f.close()

else:

hesin=0



And how are people supposed to guess an 8 character password? Are they

supposed to do that weird calculation on their calculators or some such?



--

Piet van Oostrum <[email protected]>

WWW: http://pietvanoostrum.com/

PGP key: [8DAE142BE17999C4]

Yes the code is a mess, I have been tempted to re write the whole thing...
The reason for the messiness is firstly because I am relatively new to programming and because this is the result of me tinkering around with previouscode, which was still messy, but cleaner than this.

So the open("key","w") clears the file straight away? That helps a lot, thanks, this will definitely save me a lot of time in the future. Also the user doesn't guess the pass, I would give it to them. After they enter it, they go on to the guessing game (not shown in the code).

Thanks for the help!

Leo
 
L

Leo Carnovale

(e-mail address removed) writes:
I am making this little game and I am trying to make some sort of script that does the following:
Checks to see if a file exists
If it does, check the numbers in it
If it doesn't, make one and fill it with some numbers
Sorts out if the numbers in the file are in the right format
If they are, then store them
If not, remake it again
THEN, check if there is a pass.
If there is, check if it fits the key and go on if so
If not, ask for a new one, check it, go on if its correct.
Every time I run it, it says the key is 0!?
I can not for the life of me figure out what it is doing.
The code:
What a mess is that code. First that messing around with the key is
crazy. You probably want to do some crypto on it, but why don't you just
use some standard crypto algorithm?
The you have defined T as True and F as False, but sometimes you use T
and sometimes True, and as far as I can see you never use F. Just stay
with True and false as it makes the code more readable.
Once you use "with open('Key')", all other case you use
f = open('Key',...)
f.read() or f.write()
f.close()

Be consistenst and use always "with open..."
There are at least three palces where you write a key to the file. Make
a function for this; it makes your code more structured.
And please, put spaces aroud the = signs.
Now the real problem:
if newpas:
f=open("Key","w")

print("No pass found!")
print("Your wonderful key is: ",int(keys[0]))
pasw=input("What is your pass? : ")
elif newkey:
f=open("Key","w")

Here you open the file for writing but you never write anything to it.
This makes the file empty. And apparently you do not check this the next
time you open it. If you would have used a function to write a key to
the file, this probably would not have occurred.
if mess_with(keys[0])==pasw:

print("Your in!")
f=open("Key","w")

Here you open the file again with the same variable. This causes the old
value to be lost and the file probably to be closed. Just messy.
print("writing %s" % str(keys[0])+pasw)
f.write(str(keys[0])+pasw)
f.close()

And how are people supposed to guess an 8 character password? Are they
supposed to do that weird calculation on their calculators or some such?
--
Piet van Oostrum <[email protected]>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



Yes the code is a mess, I have been tempted to re write the whole thing....

The reason for the messiness is firstly because I am relatively new to programming and because this is the result of me tinkering around with previous code, which was still messy, but cleaner than this.



So the open("key","w") clears the file straight away? That helps a lot, thanks, this will definitely save me a lot of time in the future. Also the user doesn't guess the pass, I would give it to them. After they enter it, they go on to the guessing game (not shown in the code).



Thanks for the help!



Leo

Ah and one other thing!
What is this crypto algorithm you speak of? I desperately need some sort ofencryption as at the moment anyone can simply open the text file and change the numbers to numbers that work!
Where can I learn more about it?
 
M

Michael Torrie

Ah and one other thing! What is this crypto algorithm you speak of? I
desperately need some sort of encryption as at the moment anyone can
simply open the text file and change the numbers to numbers that
work! Where can I learn more about it?

There are two kinds of cryptography, generally. Symmetric and
asymmetric. Your needs will dictate which system you use. Symmetric
cryptography means that the sake encryption key that was used to encrypt
the data is used to decrypt it. Asymmetric cryptography basically means
one key is used to encrypt a message and another key is used to decrypt
the message.

In your case, perhaps symmetric encryption is most logical, though I'm
not at all clear what you are doing. However you'll have to embed the
encryption key in your python source code, which isn't hidden at all.
And actually any program that embeds a symmetric key in its code can be
cracked rather easily.

Python has many libraries to support encryption. Some are in the
standard library. Search for python and crypto to see what docs are
available. You might want to read up on basic cryptography principles
as well.
 
L

Leo Carnovale

There are two kinds of cryptography, generally. Symmetric and

asymmetric. Your needs will dictate which system you use. Symmetric

cryptography means that the sake encryption key that was used to encrypt

the data is used to decrypt it. Asymmetric cryptography basically means

one key is used to encrypt a message and another key is used to decrypt

the message.



In your case, perhaps symmetric encryption is most logical, though I'm

not at all clear what you are doing. However you'll have to embed the

encryption key in your python source code, which isn't hidden at all.

And actually any program that embeds a symmetric key in its code can be

cracked rather easily.



Python has many libraries to support encryption. Some are in the

standard library. Search for python and crypto to see what docs are

available. You might want to read up on basic cryptography principles

as well.

Thanks! Obviously, I need to do a lot of research.
 
D

Dennis Lee Bieber

Ah and one other thing!
What is this crypto algorithm you speak of? I desperately need some sort of encryption as at the moment anyone can simply open the text file and change the numbers to numbers that work!
Where can I learn more about it?

Given the use of the term "pasw" (and variants thereof) in the snipped
text...

Are you trying to password protect access to the program (ie, a
"login"), or hide the data in a recoverable state.

Many "login" type passwords aren't encrypted -- they are hashed,
possibly with a random starting seed created when the "login account name"
is entered to the system. These never recover the password itself (well,
supposedly they didn't but I believe cracks were found in the Linux passwd
file such that the hash is now stored in a system level file in parallel,
while the passwd file may have just the basics of the user login/owner/etc.
-- since passwd had to be accessible to most user level programs).

For "login", one matches the username in the password file, extracts
the seed, then hashes the entered password -- the hash has to match the one
in the file to be valid. If the user forgets the password, there is no way
to recover it -- the most a sysop can do is generate a new password, enter
its hash into the file, and tell the user what the new password is (along
with instructions to change it as soon as they log in).

For data hiding, a prior post provides information regarding symmetric
(same key used in both directions -- a very simplistic approach would be to
just XOR the data with the key (but this will reveal the length of the key
if the data has a large stretch of identical values, say a line of
------------------------------------------------------
as the repeats of the shorter key will create the same output sequence). In
the late 70s, the DES (data encryption standard) algorithm was supposed to
be practically unbreakable by brute force (trial and error) and secure
enough for international banking. 20 years later, desktop machines could
break it in a few weeks, if not sooner, and Triple-DES came into use.

Problem with symmetric keys? Both sides have to know the key before
data can be transferred, and anyone who discovers the key can
decrypt/modify/encrypt the data.

Asymmetric (public key) systems use a pair of keys. One key is used to
encrypt the data. The other key is used to decrypt it. They only work in
one direction. If you publish the encryption key, anyone can use it to
encrypt data which then can be sent to you, but only you can decrypt it
with the other key. Vice versa, if you publish the decryption key but keep
the encryption key secret, you can encrypt data that anyone with the public
key can decrypt and read -- but only you can provide the data (this is how
"signing" is normally done; only the holder of the signing encryption key
could produce the signature).
 
P

Piet van Oostrum

Leo Carnovale said:
Ah and one other thing!
What is this crypto algorithm you speak of? I desperately need some sort of encryption as at the moment anyone can simply open the text file and change the numbers to numbers that work!
Where can I learn more about it?

You can google for cryptography or look in Wikipedia. Now the question
is: will this game be played through the internet or on someone's
computer? If it is run on a computer then both the program and the
keyfile will be on that computer, so whatever algorithm you use the user
can use it either to detect the password derived from the key or choose
his own password and generate the corresponding key and put it in the
file. Or even change the program and take the check out. You could use
some obfuscation to make it difficult, like they do in DRM but basically
you cannot hode it from the user. If you use an internet connection then
of course you can keep the relevant data outside of the reach of the
user.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top