-1

I'm building a more or less simple encryption. Now after a bit of fuzzing and fiddling i came to a problem which i can't seem to find a solution for.

In the process of encryption i take a string as input and convert it to binary to do some math with it. After the "mathing" part im getting strings like this:

56010479313713691637235922367138913625577411202663921330215486595

now after this step i'm dissecting the string into pairs of 2 like this :

56 01 04 79 31 37 13 69 16 37 23 59 22 36 71 38 13 62 55 77 41 12 02 66 39 21 33 02 15 48 65 95 

which is now still 1 string object. My problem is now that i can't seem to make up a method to actually take these pairs 1 by 1 to make some more math with them(afterwards i also want to probably convert the results into ascii or something else). Might just be the lack of sleep, but i can't seem to find a solution, any help would be appreciated :)


Edit : I now used the functions provided by timgeb to further process my code. Now im stuck at another rather stupid, let's call it bug.

Below is the code snippet that reads the encrypted string (which is numeric) from a file tmp.txt, comments explain the rest of it i guess. My problem is now that When i run the script it goes to the point where it joins back the int to a string but after that the replacing of the numeric pairs to letters does not work at all.

#***** Read from tmp.txt *****
 alphaStr2 = linecache.getline('tmp.txt', 1)
 alphaStr2 = int(alphaStr2) / 123456789
 alphaStr = str(alphaStr2)

#***** Split into pairs of 2 *****
 alphaStr = " ".join(alphaStr[i:i+2] for i in range(0, (len(alphaStr) + 1), 2))

#***** Convert to list *****
 numStr = alphaStr.split()

#***** Convert list content to int's *****
 numStr = map(int, numStr)

#***** simple math to adjust to alphabet lenght of 51 (Aa-Zz) *****
 numMath = lambda x: x / 1.96078431372549
 numStr = map(numMath, numStr)

#***** "round" float numbers to integer *****
 numStr = [int(i) for i in numStr]

#***** convert back to string *****
 numStr = ' '.join(str(x).zfill(2) for x in numStr)

#***** Convert numbers into letters *****
 alphaStr = numStr.replace("00 ", "a")   
 alphaStr = numStr.replace("01 ", "b")   
 alphaStr = numStr.replace("02 ", "c")
 alphaStr = numStr.replace("03 ", "d")     
 alphaStr = numStr.replace("04 ", "e")
 .......
Cryo
  • 63
  • 2
  • 6

3 Answers3

0

Split your string:

>>> s = '56 01 04 79 31'
>>> numbers = s.split()
>>> numbers
['56', '01', '04', '79', '31']

Turn them into integers:

>>> numbers = map(int, numbers)
>>> numbers
[56, 1, 4, 79, 31]

Do some math with them

>>> some_math = lambda x: x+1
>>> numbers = map(some_math, numbers)
>>> numbers
[57, 2, 5, 80, 32]

Convert back to string (assuming you want numbers < 10 padded with zeros):

>>> ' '.join(str(x).zfill(2) for x in numbers)
'57 02 05 80 32'

Or bytearray->str if that's what you're after (requires numbers < 256):

>>> str(bytearray(numbers))
'9\x02\x05P '
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • is the lambda neccessary? and what does it do? – Cryo Feb 12 '16 at 11:13
  • @Cryo no, the lambda is just a silly anonymous function for demo purposes, to show how you could apply a function that does some calculation to every number. – timgeb Feb 12 '16 at 11:14
  • I'll just use this in my code for easyness sake, gonna rework this later anyway i'm already at ~250 lines of code for some "basic mathing" – Cryo Feb 12 '16 at 11:18
  • Just to remember that this code may have a different behaviour at python 3.x, because of `map` function. [see](http://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x) – emartinelli Feb 12 '16 at 11:39
0

I think that best way will be split this string into list:

sequence = "56 01 04 79 31 37 13 69 16 37 23 59 22 36 71 38 13 62 55 77 41 12 02 66 39 21 33 02 15 48 65 95"
sequence = sequence.split()

As result you will get list of strings (you can check this):

print type(sequence[10])
<type 'str'>

Then convert this list of strings into list of int elements.

for i in sequence:
    convert i to int

I belive now you will be able to do some math operations on lists.

patrat
  • 31
  • 3
0

Do you realize that

alphaStr = " ".join(alphaStr[i:i+2] for i in range(0, (len(alphaStr) + 1), 2))

Without the " ".join() does exactly what you want? Example

>>> alphaStr = 'hello world'
>>> x = [alphaStr[i:i+2] for i in range(0, (len(alphaStr) + 1), 2)]
>>> x
[he, ll, o , wo, rl, d]

And if you want integers I suggest to do it with generators as you don't need entire list in memory

numbers = (int(alphaStr[i:i+2]) for i in range(0, (len(alphaStr) + 1), 2))
numbers = map(numMath, numbers)
numbers = map(int, numbers)

Edit:

The problem with your replace statements is that each replace returns a new string with the replacement and you're overriding the last replace. You should do instead

alphaStr = numStr
alphaStr = alphaStr.replace('00 ', 'a') #Use the updated string to continue replacements
alphaStr = alphaStr.replace('01 ', 'b')

Be careful here as your last number won't be replaced, as your string will be something like '01 01 00' (No final space). So you should either add a space at the end

alphaStr = numStr + ' '
alphaStr = alphaStr.replace('00 ', 'a') #Use the updated string to continue replacements
alphaStr = alphaStr.replace('01 ', 'b')

But even better, if your conversion is exactly that (00 -> a, 01 -> b, ...), you should better use:

alphaStr = ''.join(chr(ord('a')+x) for x in numStr)

Here ord('a') returns the ascii code for 'a' then it adds x and returns the according character, the joins everything. Example:

>>> numStr = [0,2,5,9,12]
>>> ''.join(chr(ord('a')+x) for x in numStr)
acfjm

If you use this be careful when chr(ord('a')+x) is above 'z' as ascii code doesn't continue with capital letters ;)

Mr. E
  • 2,070
  • 11
  • 23
  • That's a nice approach, thanks for this, i'll try to implement this and save myself from a few lines of code. The main problem is still that the lines alphaStr = numStr.replace("00 ", "a") etc. don't do what they are supposed to – Cryo Feb 12 '16 at 13:30
  • @Cryo Edited my answer with some suggestions for the replace problem, take a look – Mr. E Feb 12 '16 at 13:47
  • great, Thank you. I actually just fixed the issue with replacing the names of all alphaStr and numStr variable to string, i know string is a reserverd keyword but it kind of fixed it and i wont be using any other string functions in this script, so i guess that's the fix i was looking for. – Cryo Feb 12 '16 at 14:54