Ruby version ot TEA (Tiny Encryption Alogrithm)?

J

James Britt

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
 
J

Jeremy Hinegardner

--vkogqOf2sHV7VnPd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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?

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

--
========================================================================
Jeremy Hinegardner (e-mail address removed)


--vkogqOf2sHV7VnPd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="tea.rb"

#---------------------------------------------------------------------
# This is a pure ruby implementation of the Tiny Encryption Algorithm
# (TEA) by David Wheeler and Roger Needham of the Cambridge Computer
# Laboratory.
#
# For more information:
#
# http://www.simonshepherd.supanet.com/tea.htm
#
# This is an implementation of the 'New Variant' of the cipher.
#---------------------------------------------------------------------
require 'digest/md5'
class Crypt
class TEA
DELTA = 0x9e3779b9
ITERATIONS = 32

#-------------------------------------------------------------
# encrypt the given plaintext with the given key, where the key
# is a text pass phrase
#-------------------------------------------------------------
def self.encrypt(plain_text,pass_phrase)
tea =TEA.new
tea.encrypt(plain_text,pass_phrase)
end

#-------------------------------------------------------------
# decrypt the given ciphertext with the given key, where the key
# is a text pass phrase
#-------------------------------------------------------------
def self.decrypt(cipher_text,pass_phrase)
tea = TEA.new
tea.decrypt(cipher_text,pass_phrase)
end


#-------------------------------------------------------------
def encrypt(plain_text,pass_phrase)

key = passphrase_to_key(pass_phrase)

# pad the plaintext to a length modulo 8
# and preface the string with how many padding characters
# there where, including itself
to_pad = 8 - plain_text.length % 8
plain_text = "#{to_pad}#{plain_text}"

1.upto(to_pad-1) do |i|
plain_text = plain_text + rand(0xff).chr
end

cipher_text = []

# for each 8 char's pack them into 2 ints
range = Range.new(0,plain_text.length,true)
range.step(8) do |n|

num1 = plain_text[n].to_i << 24
num1 += plain_text[n+1].to_i << 16
num1 += plain_text[n+2].to_i << 8
num1 += plain_text[n+3].to_i

num2 = plain_text[n+4].to_i << 24
num2 += plain_text[n+5].to_i << 16
num2 += plain_text[n+6].to_i << 8
num2 += plain_text[n+7].to_i

enum1,enum2 = encrypt_chunk(num1,num2,key)

cipher_text << enum1
cipher_text << enum2

end

cipher_text.collect { |c| sprintf("%.8x",c) }.join('')
end

#-------------------------------------------------------------
def decrypt(cipher_text,pass_phrase)

key = passphrase_to_key(pass_phrase)

plain_text = []

# convert the cipher_text into an array of 2 character
# strings
cipher_array = cipher_text.scan(/../)

# for each 8 char's pack them into 2 ints
range = Range.new(0,cipher_array.length,true)

range.step(8) do |n|
num1 = cipher_array[n].to_i(16) << 24
num1 += cipher_array[n+1].to_i(16) << 16
num1 += cipher_array[n+2].to_i(16) << 8
num1 += cipher_array[n+3].to_i(16)

num2 = cipher_array[n+4].to_i(16) << 24
num2 += cipher_array[n+5].to_i(16) << 16
num2 += cipher_array[n+6].to_i(16) << 8
num2 += cipher_array[n+7].to_i(16)

enum1,enum2 = decrypt_chunk(num1,num2,key)

plain_text << ((enum1 & 0xFF000000) >> 24)
plain_text << ((enum1 & 0x00FF0000) >> 16)
plain_text << ((enum1 & 0x0000FF00) >> 8)
plain_text << ((enum1 & 0x000000FF))

plain_text << ((enum2 & 0xFF000000) >> 24)
plain_text << ((enum2 & 0x00FF0000) >> 16)
plain_text << ((enum2 & 0x0000FF00) >> 8)
plain_text << ((enum2 & 0x000000FF))

end

pad_count = plain_text.shift.chr.to_i
(pad_count - 1).times { |i| plain_text.pop }

plain_text.collect { |c| c.chr }.join("")
end

############
private
############


#-------------------------------------------------------------
# convert the given passphrase to and MD5 sum and get the 128
# bit key as 4 x 32 bit ints
#-------------------------------------------------------------
def passphrase_to_key(pass_phrase)
Digest::MD5.digest(pass_phrase).unpack('L*')
end


#-------------------------------------------------------------
# encrypt 2 of the integers ( 8 characters ) of the input into
# the cipher text output
#-------------------------------------------------------------
def encrypt_chunk(num1,num2,key)
y,z,sum = num1,num2,0

ITERATIONS.times do |i|
y += ( z << 4 ^ z >> 5) + z ^ sum + key[sum & 3]
y = y & 0xFFFFFFFF;

sum += DELTA
z += ( y << 4 ^ y >> 5) + y ^ sum + key[sum >> 11 & 3]
z = z & 0xFFFFFFFF;

# ruby can keep on getting bigger because of Bignum so
# you have to and with 0xFFFFFFFF to get the Fixnum
# bytes

end
return [y,z]
end


#-------------------------------------------------------------
# decrypt 2 of the integer cipher texts into the plaintext
#-------------------------------------------------------------
def decrypt_chunk(num1,num2,key)
y,z = num1,num2
sum = DELTA << 5
ITERATIONS.times do |i|
z -= ( y << 4 ^ y >> 5) + y ^ sum + key[sum >> 11 & 3]
z = z & 0xFFFFFFFF
sum -= DELTA
y -= ( z << 4 ^ z >> 5) + z ^ sum + key[sum & 3]
y = y & 0xFFFFFFFF
end
return [y,z]
end
end
end


if __FILE__ == $0 then
plain_text = "Just another Ruby Hacker!"
pass_phrase = "I love ruby"

puts "encrypting\t[#{plain_text}]"
cipher_text = Crypt::TEA.encrypt(plain_text,pass_phrase)

puts "decrypting\t[#{cipher_text}]"
decrypted_text = Crypt::TEA.decrypt(cipher_text,pass_phrase)

puts "decrypted text\t[#{decrypted_text}]"
end

--vkogqOf2sHV7VnPd--
 
J

James Britt

Jeremy said:
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
 
M

Max Nickel

--=-z6gJ3Kf8/NaVAkBCfZej
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
Related question: The JavaScript version uses a shift right, zero fill=20
operation. Ruby seems to only offer signed shift-right. Is this the=20
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 =3D (i >> 1) & 0x7FFFFFFF

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

regards
/max

--=-z6gJ3Kf8/NaVAkBCfZej
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iQCVAwUAQqb7/7C1SnkHJ6uKAQKPhwQAzeksphosz3fXG8N0hnRXhvmyUL+8VWsZ
p2LtiUvI9QT4whYVsq2EujsvABNulMdYE90SFC9fhNNkkxGtR9cTP8RdrW2NJTub
Y0nmtYm3KBTsQK52V3he9rAKZX8xnYLCcwlYwYK60g1XVhCcXxgHdzThZMnh1Eph
DiMLOnOIm38=
=mzWq
-----END PGP SIGNATURE-----

--=-z6gJ3Kf8/NaVAkBCfZej--
 
J

James Britt

Max said:
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
 
J

Jeremy Hinegardner

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.

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
 
T

Tom Reilly

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top