-
Notifications
You must be signed in to change notification settings - Fork 2
/
TEA.rb
207 lines (162 loc) · 6.05 KB
/
TEA.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#---------------------------------------------------------------------
# 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.
#
# ====================================================================
# Copyright (c) 2005, 2006 Jeremy Hinegardner <jeremy@hinegardner.org>
#
# This implementation of the TEA New Variant is released under
# the MIT License:
#
# http://www.opensource.org/licenses/mit-license.html
#
# ====================================================================
#
#---------------------------------------------------------------------
# Ruby 1.8 compatibility
if RUBY_VERSION.include?('1.8')
class Fixnum; def ord; return self; end; end
end
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].ord.to_i << 24
num1 += plain_text[n+1].ord.to_i << 16
num1 += plain_text[n+2].ord.to_i << 8
num1 += plain_text[n+3].ord.to_i
num2 = plain_text[n+4].ord.to_i << 24
num2 += plain_text[n+5].ord.to_i << 16
num2 += plain_text[n+6].ord.to_i << 8
num2 += plain_text[n+7].ord.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)
while cipher_array.any? do
num1 = cipher_array.shift(4).join.to_i(16)
num2 = cipher_array.shift(4).join.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.map(&: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
require 'test/unit'
class TEATester < Test::Unit::TestCase
def test_en_decrypt
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}]"
assert_equal plain_text, decrypted_text
end
end
end