Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo
 Hello Tutors! could be split into:

 Hell o Tut ors!

 and xor'd with beer

 I think I understand how xor works (thanks to an earlier post) but I'm
 not sure how to iterate over each letter in a string.  What is the
 recommended way to do this?

The xor bitwise operator works with numbers --- not directly with strings
--- so one of your tasks will probably be to take a chunk like:

beer

and turn it into some numeric bit pattern.


It turns out that this isn't too bad if we abuse the 'struct' module:

http://www.python.org/doc/lib/module-struct.html


The idea is to unpack four single characters as a single 4-byte integer.
For example:

##
 struct.unpack('i', 'food')
(1685024614,)
##


This kind of transformation is reversable:

##
 struct.pack('i', 1685024614)
'food'
##


Does this make sense?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Danny Yoo [EMAIL PROTECTED] wrote:

 The idea is to unpack four single characters as a single 4-byte integer.

That's really useful, thanks, as I was planning to iterate over each
letter and call ord()

 This kind of transformation is reversable:

Highly useful.  Thanks very much indeed.

 Does this make sense?

Absolutely.

S.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Matthew Webber
As a side note, remember that that xor-ing a key with a message is trivial
to break (it's just a variation on the Vigenere cipher first published in
1568). So don't use if for any real applications.

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Steve Nelson
Sent: 14 March 2006 15:29
To: tutor@python.org
Subject: [Tutor] Iterate over letters in a word

Hello,

I'm trying to work on some programs to help me understand ciphers and
ultimately cryptography.  I've understood so far, that a simple form
of bit-level cryptography is to split the original message into chunks
the same length as a 'key' and then do an xor.  I'm trying to keep
this really simple so I can understand from first principles - so eg:

Hello Tutors! could be split into:

Hell o Tut ors!

and xor'd with beer

I think I understand how xor works (thanks to an earlier post) but I'm
not sure how to iterate over each letter in a string.  What is the
recommended way to do this?

Thanks,

S.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Matthew Webber [EMAIL PROTECTED] wrote:
 As a side note, remember that that xor-ing a key with a message is trivial
 to break (it's just a variation on the Vigenere cipher first published in
 1568). So don't use if for any real applications.

Yes - at the moment this is just a way for me to begin to get my head
around how cryptography works from anabsolutely ludicrously basic
position.  This all started because I couldn't get my head around the
difference between an encryption algorithm and the key.  I thought
that by writing my own, I would work it out!

S.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Steve Nelson
On 3/14/06, Steve Nelson [EMAIL PROTECTED] wrote:
 On 3/14/06, Danny Yoo [EMAIL PROTECTED] wrote:

  The idea is to unpack four single characters as a single 4-byte integer.

 That's really useful, thanks, as I was planning to iterate over each
 letter and call ord()

Ok, so experimenting a little further, and looking at the
documentation, it seems that len(string) and calcsize (i) must be the
same.  Is there a reason why 'i' is a 4 byte integer?  Doesn't this
mean that this method wouldn't scale if I then decided I wanted to
use, eg, a 6 byte key instead of a four?  Or do I misunderstand?

I am also struggling to understand why a 4 byte integer is so large?

 mystring = Hello I am Steve
 import struct
 struct.unpack('i', mystring[0:4])
(1819043144,)

I can see that the largest number I can generate in a 1 byte integer
is 255 - which is (2^8)-1.  Is the point that with a 4 byte number I
can actually get (2^32)-1 , ie 4294967295?  This just seems like a
huge number!

I suppose I've answered my question... but any comments or
clarifications would help a lot.

S.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo


 As a side note, remember that that xor-ing a key with a message is
 trivial to break (it's just a variation on the Vigenere cipher first
 published in 1568). So don't use if for any real applications.

Hi Matthew,

Counterpoint: think of one-time pads.

http://en.wikipedia.org/wiki/One-time_pad

XOR itself is just a technique --- it's just a binary operation between
two bit patterns --- but the use of one-time pads provides unbreakable
encryption.  As long as the xor-ing key is protected, and as long as the
encrypting key pattern is as long as the message, it's theoretically
unbreakable.

If we reuse the same encrypting bit-pattern over and over, or make the
XORing key less than random, then all bets are off, of course.  *grin*

There are different encryption schemes that, at its heart, use XOR.  But
XOR itself is not inherently insecure: it's our use of it that determines
the quality of the result.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Danny Yoo

 Yes - at the moment this is just a way for me to begin to get my head
 around how cryptography works from anabsolutely ludicrously basic
 position.  This all started because I couldn't get my head around the
 difference between an encryption algorithm and the key.  I thought that
 by writing my own, I would work it out!

Hi Steve,

Yup; it really has to do with numbers.  Once we manage that leap, then
it's just a matter of writing functions that transform numbers to other
numbers.

When you want to get more in depth, you may find:

http://mitpress.mit.edu/sicp/psets/ps3/readme.html

useful; it's a fun homework assignment on the RSA cryptosystem.  The
support functions are in Scheme, but converting it to Python isn't too
bad.


Best of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Adam
Here's something you might find useful they've just started a series
on cryptography on this site, you can read them or listen to a
podcast.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate over letters in a word

2006-03-14 Thread Adam
On 14/03/06, Adam [EMAIL PROTECTED] wrote:
 Here's something you might find useful they've just started a series
 on cryptography on this site, you can read them or listen to a
 podcast.

D'oh! Would help if I actually stuck the link in
http://www.grc.com/SecurityNow.htm#30
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor