[Tutor] string to binary and back... Python 3

Jordan wolfrage8765 at gmail.com
Thu Jul 19 22:43:44 CEST 2012



On 07/19/2012 10:04 PM, eryksun wrote:
> On Thu, Jul 19, 2012 at 3:08 PM, Jordan <wolfrage8765 at gmail.com> wrote:
<SNIP>
> I'm not an expert with cryptography, but here's a simple XOR example:
>>>> from itertools import cycle
>>>> text = b'Mary had a little lamb.'
>>>> key = b'1234'
>>>> cypher = bytes(x^y for x,y in zip(text, cycle(key)))
>>>> cypher
> b'|SAM\x11ZRP\x11S\x13XXFGXT\x12_U\\P\x1d'
>>>> text2 = bytes(x^y for x,y in zip(cypher, cycle(key)))
>>>> text2
> b'Mary had a little lamb.'
Hmm interesting, I am reading up on on itertools.cycle() and zip now.
Thanks always more than one way to solve a problem.


More information about the Tutor mailing list