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

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Jul 19 23:28:18 CEST 2012


> > bin_data = [ bin(ord(char)).split('b')[1].zfill(8) for char in data ]
> > bin_string = ''.join(bin_data)
> > bin_list = [ chr( int(char, 2) ) for char in bin_data ]
> Thank you exactly what I was looking for!
> >
> > I am not really sure what you are getting at with XOR and one time
> > padding, but it has been a while since I have done any encryption.
> And I have just started reading "Applied Cryptography", so I am putting
> some of what I learn into practice.
> >
> > I would think you could do all this by just converting everything
> > to int and then adding/replacing the pad in the list of ints.
> At first I was essentially doing just that, but when I first converted
> the large integers that are being used for the one time pad as the key
> to binary I saw how much larger it was, and then realized that was the
> bit length of the integer (technically Long). By doing that, I can get
> more out of the one time pad, but if you XOR binary against Ord, very
> few values will be changed because binary is only 1s and 0s as you know.
> To optimize the keys use, whether it wastes memory or not, I wanted to
> use binary on binary, this really comes into play with files, not so
> much the shorter strings.

How are you XOR-ing "binary" against something else? At a low level the data
is pretty similar so that they should be mostly interchangeable. It is
when you start abstracting the data that you have to convert between
abstractions.

Hold on, let me try a different angle. int, binary, and hex version of 
a number (lets say 65) are all just different representations of the same
number. The only thing that changes is the base.

65 in octal (base 10) is 65
65 in hex (base 16) is 41
65 in binary (base 2 ) is 1000001

But they are ALL the same number. 
>>> int( '65', 10 )
65
>>> int( '41', 16 )
65
>>> int( '1000001', 2 )
65


> But since you bring if up, how would you convert a file to a list of ints?

with open(filename, 'r' ) as f:
    ints = [ ord( char ) for line in f for char in line ]

Now all you need to do is modify the list to include your padding.

> but when I first converted
> the large integers that are being used for the one time pad as the key
> to binary I saw how much larger it was, and then realized that was the
> bit length of the integer (technically Long). By doing that, I can get
> more out of the one time pad,

Large integers? Are you adding the integers for some reason? Extended 
ASCII only has ordinal values less than 256.

Ramit

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list