Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert to binary and convert back to strings

Reply
Thread Tools

Convert to binary and convert back to strings

 
 
Harlin Seritt
Guest
Posts: n/a
 
      02-21-2007
Hi...

I would like to take a string like 'supercalifragilisticexpialidocius'
and write it to a file in binary forms -- this way a user cannot read
the string in case they were try to open in something like ascii text
editor. I'd also like to be able to read the binary formed data back
into string format so that it shows the original value. Is there any
way to do this in Python?

Thanks!

Harlin

 
Reply With Quote
 
 
 
 
Colin J. Williams
Guest
Posts: n/a
 
      02-22-2007
Harlin Seritt wrote:
> Hi...
>
> I would like to take a string like 'supercalifragilisticexpialidocius'
> and write it to a file in binary forms -- this way a user cannot read
> the string in case they were try to open in something like ascii text
> editor. I'd also like to be able to read the binary formed data back
> into string format so that it shows the original value. Is there any
> way to do this in Python?
>
> Thanks!
>
> Harlin
>

Try opening your file in the 'wb' mode.

Colin W.
 
Reply With Quote
 
 
 
 
Colin J. Williams
Guest
Posts: n/a
 
      02-22-2007
Harlin Seritt wrote:
> Hi...
>
> I would like to take a string like 'supercalifragilisticexpialidocius'
> and write it to a file in binary forms -- this way a user cannot read
> the string in case they were try to open in something like ascii text
> editor. I'd also like to be able to read the binary formed data back
> into string format so that it shows the original value. Is there any
> way to do this in Python?
>
> Thanks!
>
> Harlin
>

Try opening your file in the 'wb' mode.

Colin W.

 
Reply With Quote
 
Harlin Seritt
Guest
Posts: n/a
 
      02-22-2007
On Feb 21, 7:02 pm, "Colin J. Williams" <(E-Mail Removed)> wrote:
> Harlin Seritt wrote:
> > Hi...

>
> > I would like to take a string like 'supercalifragilisticexpialidocius'
> > and write it to a file in binary forms -- this way a user cannot read
> > the string in case they were try to open in something like ascii text
> > editor. I'd also like to be able to read the binary formed data back
> > into string format so that it shows the original value. Is there any
> > way to do this in Python?

>
> > Thanks!

>
> > Harlin

>
> Try opening your file in the 'wb' mode.
>
> Colin W.


Thanks for the help.

I tried doing this:

text = 'supercalifragilisticexpialidocius'

open('sambleb.conf', 'wb').write(text)

Afterwards, I was able to successfully open the file with a text
editor and it showed:
'supercalifragilisticexpialidocius'

I am hoping to have it show up some weird un-readable text. And then
of course be able to convert it right back to a string. Is this even
possible?

Thanks,

Harlin

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      02-22-2007
On 2007-02-21, Harlin Seritt <(E-Mail Removed)> wrote:

> I would like to take a string like
> 'supercalifragilisticexpialidocius' and write it to a file in
> binary forms -- this way a user cannot read the string in case
> they were try to open in something like ascii text editor.


Why wouldn't they be able to read it? ASCII _is_ binary.

> I'd also like to be able to read the binary formed data back
> into string format so that it shows the original value. Is
> there any way to do this in Python?


What you're describing as "this" doesn't seem to make any
sense.

--
Grant Edwards grante Yow! Kids, don't gross me
at off... "Adventures with
visi.com MENTAL HYGIENE" can be
carried too FAR!
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      02-22-2007
Harlin Seritt wrote:
> Hi...
>
> I would like to take a string like 'supercalifragilisticexpialidocius'
> and write it to a file in binary forms -- this way a user cannot read
> the string in case they were try to open in something like ascii text
> editor. I'd also like to be able to read the binary formed data back
> into string format so that it shows the original value. Is there any
> way to do this in Python?
>
> Thanks!
>
> Harlin
>

I promise you that everything written to a file is done in binary.
Computers don't know how to work with anything BUT binary. I think
what you want to do is to encrypt/obstifucate the string. For that
you will need to encrypt the string, write it out, read it back in,
and decrypt it. If you want it to be REALLY strong use pyCrypto
and something like AES-256.

http://www.amk.ca/python/code/crypto

If you just want to make it somewhat hard for someone to decypher
you can do something like below (sorry I can't remember where I
found this to attribute to someone):
import random
import zlib
import time

def tinycode(key, text, reverse=False):
rand = random.Random(key).randrange
if not reverse:
text = zlib.compress(text)
text = ''.join([chr(ord(elem)^rand(256)) for elem in text])
if reverse:
text = zlib.decompress(text)
return text

def strToHex(aString):
hexlist = ["%02X" % ord(x) for x in aString]
return ''.join(hexlist)

def HexTostr(hString):
res = ""
for i in range(len(hString)/2):
realIdx = i*2
res = res + chr(int(hString[realIdx:realIdx+2],16))
return res

if __name__ == "__main__":

keyStr = "This is a key"
#testStr = "which witch had which witches wrist watch abc def ghi"

testStr=time.strftime("%Y%m%d", time.localtime())

print "String:", testStr
etestStr = tinycode(keyStr, testStr)
print "Encrypted string:", etestStr
hex=strToHex(etestStr)
print "Hex : ", hex
print "Len(hex):", len(hex)
nonhex=HexTostr(hex)
#testStr = tinycode(keyStr, etestStr, reverse=True)
testStr = tinycode(keyStr, nonhex, reverse=True)
print "Decrypted string:", testStr

WARNING: THIS IS NOT A STRONG ENCRYPTION ALGORITHM. It is just a
nuisance for someone that really wants to decrypt the string. But
it might work for your application.

-Larry
 
Reply With Quote
 
Harlin Seritt
Guest
Posts: n/a
 
      02-22-2007
On Feb 21, 7:12 pm, Larry Bates <(E-Mail Removed)> wrote:
> Harlin Seritt wrote:
> > Hi...

>
> > I would like to take a string like 'supercalifragilisticexpialidocius'
> > and write it to a file in binary forms -- this way a user cannot read
> > the string in case they were try to open in something like ascii text
> > editor. I'd also like to be able to read the binary formed data back
> > into string format so that it shows the original value. Is there any
> > way to do this in Python?

>
> > Thanks!

>
> > Harlin

>
> I promise you that everything written to a file is done in binary.
> Computers don't know how to work with anything BUT binary. I think
> what you want to do is to encrypt/obstifucate the string. For that
> you will need to encrypt the string, write it out, read it back in,
> and decrypt it. If you want it to be REALLY strong use pyCrypto
> and something like AES-256.
>
> http://www.amk.ca/python/code/crypto
>
> If you just want to make it somewhat hard for someone to decypher
> you can do something like below (sorry I can't remember where I
> found this to attribute to someone):
> import random
> import zlib
> import time
>
> def tinycode(key, text, reverse=False):
> rand = random.Random(key).randrange
> if not reverse:
> text = zlib.compress(text)
> text = ''.join([chr(ord(elem)^rand(256)) for elem in text])
> if reverse:
> text = zlib.decompress(text)
> return text
>
> def strToHex(aString):
> hexlist = ["%02X" % ord(x) for x in aString]
> return ''.join(hexlist)
>
> def HexTostr(hString):
> res = ""
> for i in range(len(hString)/2):
> realIdx = i*2
> res = res + chr(int(hString[realIdx:realIdx+2],16))
> return res
>
> if __name__ == "__main__":
>
> keyStr = "This is a key"
> #testStr = "which witch had which witches wrist watch abc def ghi"
>
> testStr=time.strftime("%Y%m%d", time.localtime())
>
> print "String:", testStr
> etestStr = tinycode(keyStr, testStr)
> print "Encrypted string:", etestStr
> hex=strToHex(etestStr)
> print "Hex : ", hex
> print "Len(hex):", len(hex)
> nonhex=HexTostr(hex)
> #testStr = tinycode(keyStr, etestStr, reverse=True)
> testStr = tinycode(keyStr, nonhex, reverse=True)
> print "Decrypted string:", testStr
>
> WARNING: THIS IS NOT A STRONG ENCRYPTION ALGORITHM. It is just a
> nuisance for someone that really wants to decrypt the string. But
> it might work for your application.
>
> -Larry


Thanks Larry! I was looking for something more beautiful but what the
hey, it works!

Harlin

 
Reply With Quote
 
mensanator@aol.com
Guest
Posts: n/a
 
      02-22-2007
On Feb 21, 5:50 pm, "Harlin Seritt" <(E-Mail Removed)> wrote:
> Hi...
>
> I would like to take a string like 'supercalifragilisticexpialidocius'
> and write it to a file in binary forms -- this way a user cannot read
> the string in case they were try to open in something like ascii text
> editor. I'd also like to be able to read the binary formed data back
> into string format so that it shows the original value. Is there any
> way to do this in Python?
>
> Thanks!
>
> Harlin


import gmpy # GNU Multi-Prceision library for Python

f = open('getty.txt','r')
s = f.read()
f.close

print
print 'source file:'
print
print s

count = 0
f = open('getty_binary.txt','w')
for c in s:
o = ord(c)
b = gmpy.digits(o,2)
d = '0'*(8-len(b)) + b
w = '%s ' % d
f.write(w)
count += 1
if count % 5==0:
f.write('\n')
f.close

f = open('getty_binary.txt','r')
s = f.readlines()
f.close

print
print 'binary file:'
print

for i in s:
print i,
print

c = []
for k in s:
q = k.split()
for m in q:
c.append(chr(int(m,2)))

new_s = ''.join(c)

print
print 'decoded binary:'
print
print new_s
print


## output


## source file:
##
## Four score and seven years ago,
## our fathers brought forth on this continent
## a new nation, conceived in liberty
## and dedicated to the propostion that
## all men are created equal.
##
##
## binary file:
##
## 01000110 01101111 01110101 01110010 00100000
## 01110011 01100011 01101111 01110010 01100101
## 00100000 01100001 01101110 01100100 00100000
## 01110011 01100101 01110110 01100101 01101110
## 00100000 01111001 01100101 01100001 01110010
## 01110011 00100000 01100001 01100111 01101111
## 00101100 00001010 01101111 01110101 01110010
## 00100000 01100110 01100001 01110100 01101000
## 01100101 01110010 01110011 00100000 01100010
## 01110010 01101111 01110101 01100111 01101000
## 01110100 00100000 01100110 01101111 01110010
## 01110100 01101000 00100000 01101111 01101110
## 00100000 01110100 01101000 01101001 01110011
## 00100000 01100011 01101111 01101110 01110100
## 01101001 01101110 01100101 01101110 01110100
## 00001010 01100001 00100000 01101110 01100101
## 01110111 00100000 01101110 01100001 01110100
## 01101001 01101111 01101110 00101100 00100000
## 01100011 01101111 01101110 01100011 01100101
## 01101001 01110110 01100101 01100100 00100000
## 01101001 01101110 00100000 01101100 01101001
## 01100010 01100101 01110010 01110100 01111001
## 00001010 01100001 01101110 01100100 00100000
## 01100100 01100101 01100100 01101001 01100011
## 01100001 01110100 01100101 01100100 00100000
## 01110100 01101111 00100000 01110100 01101000
## 01100101 00100000 01110000 01110010 01101111
## 01110000 01101111 01110011 01110100 01101001
## 01101111 01101110 00100000 01110100 01101000
## 01100001 01110100 00001010 01100001 01101100
## 01101100 00100000 01101101 01100101 01101110
## 00100000 01100001 01110010 01100101 00100000
## 01100011 01110010 01100101 01100001 01110100
## 01100101 01100100 00100000 01100101 01110001
## 01110101 01100001 01101100 00101110 00001010
##
##
## decoded binary:
##
## Four score and seven years ago,
## our fathers brought forth on this continent
## a new nation, conceived in liberty
## and dedicated to the propostion that
## all men are created equal.








 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      02-22-2007
On 2007-02-22, Harlin Seritt <(E-Mail Removed)> wrote:

>> Try opening your file in the 'wb' mode.


> I tried doing this:
>
> text = 'supercalifragilisticexpialidocius'
>
> open('sambleb.conf', 'wb').write(text)
>
> Afterwards, I was able to successfully open the file with a text
> editor and it showed:
> 'supercalifragilisticexpialidocius'


Of course it did.

> I am hoping to have it show up some weird un-readable text.
> And then of course be able to convert it right back to a
> string. Is this even possible?


Sure. That's what is called "encryption". There are a bunch
of encryption libraries for Python.

http://www.amk.ca/python/code/crypto
http://www.freenet.org.nz/ezPyCrypto
http://www.example-code.com/python/encryption.asp
http://www.chilkatsoft.com/python-encryption.asp

--
Grant Edwards grante Yow! Two with FLUFFO,
at hold th' BEETS...side of
visi.com SOYETTES!
 
Reply With Quote
 
Ganesan Rajagopal
Guest
Posts: n/a
 
      02-22-2007
>>>>> "Harlin" == Harlin Seritt <(E-Mail Removed)> writes:

> I tried doing this:


> text = 'supercalifragilisticexpialidocius'


> open('sambleb.conf', 'wb').write(text)


> Afterwards, I was able to successfully open the file with a text
> editor and it showed:
> 'supercalifragilisticexpialidocius'




> I am hoping to have it show up some weird un-readable text. And then
> of course be able to convert it right back to a string. Is this even
> possible?


Looks like you just want to obfuscate the string. How about this?

import base64
text = 'supercalifragilisticexpialidocius'
open('sambleb.conf', 'w').write(base64.encodestring(text))

print base64.decodestring(open('sambleb.conf', 'r').read())

Ganesan

--
Ganesan Rajagopal

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
make content's of file in binary form to zero and convert them back Kelsey Bjarnason C Programming 6 02-06-2008 01:56 PM
Are C++ strings binary strings? ruffiano C++ 9 11-03-2006 09:01 PM
Convert Binary to Float (and back) Nebiru Ruby 1 09-16-2006 01:57 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments