HEX to ASCII

M

markotaht

problem is : Traceback (most recent call last):
File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
TypeError: sequence item 0: expected str instance, bytes found

If i take away the join command i get this:
Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']

the Key-00000000 is the key im using to decrypt the code. everything else is generated by the decrytion process and the unhexlify command. So my guessis, the join command cant handle the b"u" type of format. how can i get rid of the b.

Or does anyone have a better idea how to translate HEX into ASCII and sort out the lines that make sense
 
P

Peter Otten

problem is : Traceback (most recent call last):
File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in
<module>
print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
TypeError: sequence item 0: expected str instance, bytes found

If i take away the join command i get this:
Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f',
b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_',
b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r',
b'3', b'\x1f', b'V', b's', b'9', b'\x1d']

the Key-00000000 is the key im using to decrypt the code. everything else
is generated by the decrytion process and the unhexlify command. So my
guess is, the join command cant handle the b"u" type of format. how can i
get rid of the b.
tulemus2 = [b'u', b'o', ...]
b"".join(tulemus2)
b'uo\x00\x1d |N\x0f9jKJ&#AK5k_\x1e,j\x0c\x08i(\x06\\r3\x1fVs9\x1d'
Or does anyone have a better idea how to translate HEX into ASCII and sort
out the lines that make sense

That is very likely, but you have to be specific about the input data and
what "makes sense" as the output.

If you start with a hexdump of binary data you already have the human-
readable form, and binascii.unhexlify() will convert it back to the
"unreadable" binary form.
 
M

MRAB

problem is : Traceback (most recent call last):
File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
TypeError: sequence item 0: expected str instance, bytes found

If i take away the join command i get this:
Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']

the Key-00000000 is the key im using to decrypt the code. everything else is generated by the decrytion process and the unhexlify command. So my guess is, the join command cant handle the b"u" type of format. how can i get rid of the b.

Or does anyone have a better idea how to translate HEX into ASCII and sort out the lines that make sense
(I'm assuming you're using Python 3)

tulemus2 is a list of bytestrings (bytes), "" is a Unicode string
(str). You can't mix them.

Try b"".join(tulemus2) instead.
 
P

Piet van Oostrum

problem is : Traceback (most recent call last):
File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
TypeError: sequence item 0: expected str instance, bytes found

If i take away the join command i get this:
Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']

the Key-00000000 is the key im using to decrypt the code. everything else is generated by the decrytion process and the unhexlify command. So my guess is, the join command cant handle the b"u" type of format. how can i get rid of the b.

Or does anyone have a better idea how to translate HEX into ASCII and sort out the lines that make sense

Why do you post the same question twice under different subjects?
 
M

markotaht

esmaspäev, 7. oktoober 2013 4:27.44 UTC+3 kirjutas Piet van Oostrum:
(e-mail address removed) writes:


problem is : Traceback (most recent call last):
File "C:\Users\Marko\Desktop\hacker.org\XOR cypher.py", line 35, in <module>
print("Key-" + str(võti) + ": " + str("".join(tulemus2)))
TypeError: sequence item 0: expected str instance, bytes found

If i take away the join command i get this:
Key-00000000: [b'u', b'o', b'\x00', b'\x1d', b' ', b'|', b'N', b'\x0f', b'9', b'j', b'K', b'J', b'&', b'#', b'A', b'K', b'5', b'k', b'_', b'\x1e', b',', b'j', b'\x0c', b'\x08', b'i', b'(', b'\x06', b'\\', b'r', b'3', b'\x1f', b'V', b's', b'9', b'\x1d']
the Key-00000000 is the key im using to decrypt the code. everything else is generated by the decrytion process and the unhexlify command. So my guess is, the join command cant handle the b"u" type of format. how can i get rid of the b.
Or does anyone have a better idea how to translate HEX into ASCII and sort out the lines that make sense



Why do you post the same question twice under different subjects?

--


Because i was told so, cause the subject change so it cant be under there anymore.

This is the code i came up with:
from teisendaja import *
from operator import *
import binascii

teisendus = teisendus()
kood = input("Kood: ")
key = input("Võti: ")

chunksize = 2
vastus = [teisendus.teisendus3(16,2,kood[i: (i + chunksize)]) for i in range(0, len(kood),chunksize)]
vastus = ["0"*(8-len(x)) + x for x in vastus]
#key = teisendus.teisendus3(10,2,int(key))
getBin = lambda x, n: x >= 0 and str(bin(x))[2:].zfill(n) or "-" + str(bin(x))[3:].zfill(n)


def dekrüpteeria(vastus, key):
XOR = []
tulemus = []
for i in range(len(vastus)):
for j in range(8):
XOR.append(str(ixor(int(vastus[j]), int(key[j]))))
tulemus.append("".join(XOR))
key = "".join(XOR)
XOR = []
return tulemus

tulemus2= []
if key == "":
for i in range(256):
võti = getBin(i,8)
tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, võti)]
tulemus = ["0"*(2-len(x)) + x for x in tulemus]
# for j in range(len(tulemus)):
tulemus2.append(binascii.unhexlify("".join(tulemus)))
print("Key-" + str(võti) + ": " + str(tulemus2))
tulemus2 = []
#tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, key)]
#print(":".join(tulemus))

#751a6f1d3d5c3241365321016c05620a7e5e34413246660461412e5a2e412c49254a24

Although this is quite ugly atm. But it serves me well, until i reach the unhexlify part. The number and lette r string at the wery end is the mesage im trying to decypher.
 
M

markotaht

I forgot to tell. The teisendaja module that i have imported, is a number converter that allow to convert numbers from one base to another. i mostly use it for HEX to BIN and vice versa, but it supports other bases too.
 
M

Mark Lawrence

I forgot to tell. The teisendaja module that i have imported, is a number converter that allow to convert numbers from one base to another. i mostly use it for HEX to BIN and vice versa, but it supports other bases too.

That's nice to know, but what has it got to do with the market price of
oranges in Timbuktu? Or to put it another way, you're forcing
volunteers to go and find your original message as once again you don't
quote any context. Please make life easier for everybody, including
yourself, by quoting something from the original.

Thanks in anticipation.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
P

Piet van Oostrum

This is the code i came up with:
from teisendaja import *
from operator import *
import binascii

teisendus = teisendus()
kood = input("Kood: ")
key = input("Võti: ")

chunksize = 2
vastus = [teisendus.teisendus3(16,2,kood[i: (i + chunksize)]) for i in range(0, len(kood),chunksize)]
vastus = ["0"*(8-len(x)) + x for x in vastus]
#key = teisendus.teisendus3(10,2,int(key))
getBin = lambda x, n: x >= 0 and str(bin(x))[2:].zfill(n) or "-" + str(bin(x))[3:].zfill(n)

Instead of boolean and expr1 or expr2 in current Python you can better write:
expr1 if boolean else expr2.
and I think using def getBin(x, n):would be more clear.

But I have another observation: You use getBin only for positive ints, so the whole '-' case isn't necessary. Actually it would be damaging, as the rest of the code assumes that the key that results from getBin is 8 characters long, whereas in the negative case it would be 9 characters. Also you use int(key[j]) and if key[0] == '-' this would give an error.
If you just want to get 8 binary digits for an int using format would be simpler:

getBin = lambda x, n: '{0:={1}b}'.format(x, n)
or getBin = lambda x, n: '{0:=0{1}b}'.format(x, n) if you want also negatives (but this would give you 7 or eight binary digits, not always 8.)

def dekrüpteeria(vastus, key):
XOR = []
tulemus = []
for i in range(len(vastus)):
for j in range(8):
XOR.append(str(ixor(int(vastus[j]), int(key[j]))))
tulemus.append("".join(XOR))
key = "".join(XOR)
XOR = []
return tulemus


You can use list comprehension:

def dekrüpteeria(vastus, key):
tulemus = []
for i in range(len(vastus)):
XOR = [(str(ixor(int(vastus[j]), int(key[j])))) for j in range(8)]
tulemus.append("".join(XOR))
key = "".join(XOR)
return tulemus

and then because you only use "".join(XOR), not XOR itself:

def dekrüpteeria(vastus, key):
tulemus = []
for i in range(len(vastus)):
XOR = "".join([(str(ixor(int(vastus[j]), int(key[j])))) for j in range(8)])
tulemus.append(XOR))
key = XOR
return tulemus

and then you could rewrite this also to use a list comprehension for tulemus, but that may make it in a too big one liner.

Also note that you always use int() on the elements of key and vastus, so it might be simpler to store these as int arrays (lists) instead of strings
tulemus2= []
if key == "":
for i in range(256):
võti = getBin(i,8)
tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, võti)]
tulemus = ["0"*(2-len(x)) + x for x in tulemus]

Look at the zfill method for the above 2 line

Probably
tulemus = [teisendus.teisendus3(2,16,i).zfill(2) for i in dekrüpteeria(vastus, võti)]
will do the same
# for j in range(len(tulemus)):
tulemus2.append(binascii.unhexlify("".join(tulemus)))
print("Key-" + str(võti) + ": " + str(tulemus2))
tulemus2 = []
#tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, key)]
#print(":".join(tulemus))

#751a6f1d3d5c3241365321016c05620a7e5e34413246660461412e5a2e412c49254a24

Although this is quite ugly atm. But it serves me well, until i reach the unhexlify part. The number and lette r string at the wery end is the mesage im trying to decypher.

The result of unhexlify is a byte string. So tulemus2 is a list of byte strings, which you can joint with xxx = b''.join(tulemus2), and then you have another byte string. If you are sure this is ASCII (which means all bytes are < 128), the you can convert it to a string with str(xxx, 'ascii') or xxx.decode('ascii'). If there are bytes > 127 then you have to know which encoding it is to be able to make a string out of it.

Is this some known encryption method? If so why not use a standard solution for it? If it is a home brew encryption: are you sure it is safe? Most home brew solutions in encryption are not.
 
M

markotaht

esmaspäev, 7. oktoober 2013 18:52.21 UTC+3 kirjutas Piet van Oostrum:
(e-mail address removed) writes:


This is the code i came up with:
from teisendaja import *
from operator import *
import binascii

teisendus = teisendus()
kood = input("Kood: ")
key = input("Võti: ")

chunksize = 2
vastus = [teisendus.teisendus3(16,2,kood[i: (i + chunksize)]) for i in range(0, len(kood),chunksize)]
vastus = ["0"*(8-len(x)) + x for x in vastus]
#key = teisendus.teisendus3(10,2,int(key))
getBin = lambda x, n: x >= 0 and str(bin(x))[2:].zfill(n) or "-" + str(bin(x))[3:].zfill(n)



Instead of boolean and expr1 or expr2 in current Python you can better write:

expr1 if boolean else expr2.

and I think using def getBin(x, n):would be more clear.



But I have another observation: You use getBin only for positive ints, sothe whole '-' case isn't necessary. Actually it would be damaging, as the rest of the code assumes that the key that results from getBin is 8 characters long, whereas in the negative case it would be 9 characters. Also you use int(key[j]) and if key[0] == '-' this would give an error.

If you just want to get 8 binary digits for an int using format would be simpler:



getBin = lambda x, n: '{0:={1}b}'.format(x, n)

or getBin = lambda x, n: '{0:=0{1}b}'.format(x, n) if you want also negatives (but this would give you 7 or eight binary digits, not always 8.)




def dekrüpteeria(vastus, key):
tulemus = []
for i in range(len(vastus)):
for j in range(8):
XOR.append(str(ixor(int(vastus[j]), int(key[j]))))
tulemus.append("".join(XOR))

key = "".join(XOR)

return tulemus



You can use list comprehension:



def dekrüpteeria(vastus, key):

tulemus = []

for i in range(len(vastus)):

XOR = [(str(ixor(int(vastus[j]), int(key[j])))) for j in range(8)]

tulemus.append("".join(XOR))

key = "".join(XOR)

return tulemus



and then because you only use "".join(XOR), not XOR itself:



def dekrüpteeria(vastus, key):

tulemus = []

for i in range(len(vastus)):

XOR = "".join([(str(ixor(int(vastus[j]), int(key[j])))) for j in range(8)])

tulemus.append(XOR))

key = XOR

return tulemus



and then you could rewrite this also to use a list comprehension for tulemus, but that may make it in a too big one liner.



Also note that you always use int() on the elements of key and vastus, soit might be simpler to store these as int arrays (lists) instead of strings
tulemus2= []
if key == "":
for i in range(256):
võti = getBin(i,8)
tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, võti)]
tulemus = ["0"*(2-len(x)) + x for x in tulemus]



Look at the zfill method for the above 2 line



Probably

tulemus = [teisendus.teisendus3(2,16,i).zfill(2) for i in dekrüpteeria(vastus, võti)]

will do the same


# for j in range(len(tulemus)):

print("Key-" + str(võti) + ": " + str(tulemus2))
tulemus2 = []
#tulemus = [teisendus.teisendus3(2,16,i) for i in dekrüpteeria(vastus, key)]
#print(":".join(tulemus))
#751a6f1d3d5c3241365321016c05620a7e5e34413246660461412e5a2e412c49254a24

Although this is quite ugly atm. But it serves me well, until i reach the unhexlify part. The number and lette r string at the wery end is the mesage im trying to decypher.



The result of unhexlify is a byte string. So tulemus2 is a list of byte strings, which you can joint with xxx = b''.join(tulemus2), and then you have another byte string. If you are sure this is ASCII (which means all bytes are < 128), the you can convert it to a string with str(xxx, 'ascii') orxxx.decode('ascii'). If there are bytes > 127 then you have to know which encoding it is to be able to make a string out of it.



Is this some known encryption method? If so why not use a standard solution for it? If it is a home brew encryption: are you sure it is safe? Most home brew solutions in encryption are not.

--

Piet van Oostrum <[email protected]>

WWW: http://pietvanoostrum.com/

PGP key: [8DAE142BE17999



Are you familira with the page called hacker.org? THere are tons of challenges. And this is one of the cypher challenges called Feedback cypher. The end result should be in the format of "The antswer is ....." Or something like that. The problem is that the key is unknown. So i try all the 255 keys there are and then il look for the line that makes sense.
 
M

markotaht

esmaspäev, 7. oktoober 2013 17:16.29 UTC+3 kirjutas Mark Lawrence:
That's nice to know, but what has it got to do with the market price of

oranges in Timbuktu? Or to put it another way, you're forcing

volunteers to go and find your original message as once again you don't

quote any context. Please make life easier for everybody, including

yourself, by quoting something from the original.



Thanks in anticipation.



--

Roses are red,

Violets are blue,

Most poems rhyme,

But this one doesn't.



Mark Lawrence
teisendaja module doesent matter. Only thing that matters about is that it returns a string with the converted number.
 
M

Mark Lawrence

esmaspäev, 7. oktoober 2013 17:16.29 UTC+3 kirjutas Mark Lawrence:
teisendaja module doesent matter. Only thing that matters about is that it returns a string with the converted number.

Actually another thing that matters is finding a technology that doesn't
spread superfluous newlines throughout emails. I've snipped them above.
Please take note of this
https://wiki.python.org/moin/GoogleGroupsPython. Thanks in aniticipation.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top