HEX to ASCII

Piet van Oostrum piet at vanoostrum.org
Mon Oct 7 11:52:21 EDT 2013


markotaht at gmail.com 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, 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[i][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[i][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[i][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.
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list