Ruby version ot TEA (Tiny Encryption Alogrithm)?

Does anyone know of a pure-Ruby lib that implements the Tiny Encryption Algorithm?

http://www.ftp.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html
http://www.simonshepherd.supanet.com/tea.htm

I've been looking at porting a JavaScript version, but don't want to reinvent the wheel.

Related question: The JavaScript version uses a shift right, zero fill operation. Ruby seems to only offer signed shift-right. Is this the case? Is there a Ruby version of shift right, zero fill someplace?

Thanks,

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

I couldn't find one, and I happened to be a bit bored this evening... so
will this do?

If this code is going to be used to both encrypt and decrypt then it
should work. Not sure how it will interact well with other
implementations. The conversions of the encrypted longs to printable
text could be done in any number of manners. I took the easy route and
just converted the longs to hex. I would appreciate it if someone
double checked my implementation. And I'm sure there are speed
improvements. I went for readability.

enjoy,

-jeremy

tea.rb (6.5 KB)

···

On Wed, Jun 08, 2005 at 06:10:27AM +0900, James Britt wrote:

Does anyone know of a pure-Ruby lib that implements the Tiny Encryption
Algorithm?

--

Jeremy Hinegardner jeremy@hinegardner.org

Related question: The JavaScript version uses a shift right, zero fill
operation. Ruby seems to only offer signed shift-right. Is this the
case? Is there a Ruby version of shift right, zero fill someplace?

AFAIK Ruby only supports arithmetic, not logical right shift. But
logical right shift is quite easy to implement. Just shift the number of
bits and then mask the same number of the "front" bits.
For example, if you have a 32-Bit long number and want to shift one bit
& fill with zero you could do
i = (i >> 1) & 0x7FFFFFFF

irb(main):001:0> i = -3
=> -3
irb(main):002:0> 31.downto(0) do |n| print i[n] end
11111111111111111111111111111101=> 31
irb(main):003:0> i = (i >> 1) & 0x7FFFFFFF
=> 2147483646
irb(main):004:0> 31.downto(0) do |n| print i[n] end
01111111111111111111111111111110=> 31
irb(main):005:0>

regards
/max

Jeremy Hinegardner wrote:

Does anyone know of a pure-Ruby lib that implements the Tiny Encryption Algorithm?

I couldn't find one, and I happened to be a bit bored this evening... so
will this do?

This just might do it. Thanks.

If this code is going to be used to both encrypt and decrypt then it
should work. Not sure how it will interact well with other
implementations.

Well, I was looking to do the decryption using the JavaScript implementation here:

http://www.movable-type.co.uk/scripts/TEAblock.html

The two don't seem to play well together. But perhaps I can port the Ruby to JavaScript. Or learn from your code and write my own Ruby version of the block version of TEA.

Thanks,

James

···

On Wed, Jun 08, 2005 at 06:10:27AM +0900, James Britt wrote:

Max Nickel wrote:

Related question: The JavaScript version uses a shift right, zero fill operation. Ruby seems to only offer signed shift-right. Is this the case? Is there a Ruby version of shift right, zero fill someplace?

AFAIK Ruby only supports arithmetic, not logical right shift. But
logical right shift is quite easy to implement. Just shift the number of
bits and then mask the same number of the "front" bits.
For example, if you have a 32-Bit long number and want to shift one bit
& fill with zero you could do
i = (i >> 1) & 0x7FFFFFFF

Ah. Yes.
Thanks!

James

Ah yes, see I didn't even realize all the different variants. I just
did some searching on TEA and did the New Variant non-block version.
I probably won't get to it for the next week or so, but I think I could
probably put together a full suite of TEA variants as a ruby module.
That could be a nice fun couple of evenings.

enjoy,

-jeremy

···

On Wed, Jun 08, 2005 at 10:21:39PM +0900, James Britt wrote:

>
>If this code is going to be used to both encrypt and decrypt then it
>should work. Not sure how it will interact well with other
>implementations.

Well, I was looking to do the decryption using the JavaScript
implementation here:

Block TEA ‘Tiny Encryption Algorithm’ (xxtea) implemented in JavaScript

The two don't seem to play well together. But perhaps I can port the
Ruby to JavaScript. Or learn from your code and write my own Ruby
version of the block version of TEA.

--

Jeremy Hinegardner jeremy@hinegardner.org

I needed a simple encryption system for a project of mine.

The Ruby version of the tiny encryption algorithm worked just fine.

Thanks for having written it