On Thu, Jul 19, 2012 at 3:08 PM, Jordan <wolfrage8...@gmail.com> wrote:
>
>>>>> size = 8 * max(len(b3), len(b4))
>>>>> format(r, "0%db" % size)
>> '00000011000000110000001100000011'
> Is this output the output for size rather than the two variables joined
> together?

Using "format" is useful if you need the string to be padded with
zeros for the most significant byte. I wouldn't think it's important
if you're just using the bitstring representation as a sanity check on
your algorithm. In that case you can more easily use "bin".

That said, len(b3) is the number of characters (bytes) in the bytes
object. Since b3 and b4 could be different lengths in general, I took
the max length to use for the zero padding. In this case both b3 and
b4 contain 4 bytes, so "size" is 32.

> OK. I am using one time pads to XOR data, but the one time pads (keys)
> are very large numbers, converting them to binary increases their size
> exponentially, which allows me to get more XORing done out of a single
> key. I am XORing both files and strings so I need to have code that can
> do both even if that means two branches of code via an if/else perhaps
> with an isinstance(data, str).

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.'
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to