-2

in my python program i generate a list of numbers which looks like this:

gen1=(random.sample(range(33,126), 8))
            conv=(gen1[0:])

then that list is converted to chr like this:

chr="".join([chr(i) for i in conv])

the printed result for example would look like this:

`&+hs,DY

Now my question is, how do i convert it back to how it was in 'conv' later on in my program, the user enters the generated key and my program need to reverse engineer it back to calculate something, i tried this:

InPut=input("Please enter the key you used to encrypt the above text: ")
        ord_key="".join([ord for d in InPut])

but that doesnt work if there is numbers in the key that was generated earlier please help me

Netwave
  • 40,134
  • 6
  • 50
  • 93
ThePlacid
  • 43
  • 8
  • First off, `conv=(gen1[0:])` is the same as gen1 so unless you want copy of gen1 then just use gen1, don't use `chr` as a variable name, you need to `(str(ord(d))` in your list comp but that is not going to give you the same output as conv or gen1, you need to just call `ord(d)` to match the output and forget join. – Padraic Cunningham Dec 05 '15 at 14:52
  • thanks i did all of that and i get "str objed is not callable" any ideas – ThePlacid Dec 05 '15 at 15:42
  • Then you have also shadowed `str`, don't use builtin functions/types as variable names – Padraic Cunningham Dec 05 '15 at 15:42
  • what do you mean by Shadowed – ThePlacid Dec 05 '15 at 15:48

1 Answers1

0

You are not calling ord properly, this should do:

InPut=input("Please enter the key you used to encrypt the above text: ")
ord_key="".join(map(str,[ord(d) for d in InPut]))

if you want to reverse the stringfied '&+hs,DY just map chr to it:

reversed = map(chr, "`&+hs,DY")

If you are using python 3.x transform it to a list:

reversed = list(map(chr, "`&+hs,DY"))
Netwave
  • 40,134
  • 6
  • 50
  • 93