flatulent 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +60 -16
- data/flatulent-0.0.2.gem +0 -0
- data/lib/flatulent/crypt/blowfish-tables.rb +190 -0
- data/lib/flatulent/crypt/blowfish.rb +109 -0
- data/lib/flatulent/crypt/cbc.rb +123 -0
- data/lib/flatulent/crypt/gost.rb +140 -0
- data/lib/flatulent/crypt/idea.rb +193 -0
- data/lib/flatulent/crypt/noise.rb +94 -0
- data/lib/flatulent/crypt/purerubystringio.rb +378 -0
- data/lib/flatulent/crypt/rijndael-tables.rb +117 -0
- data/lib/flatulent/crypt/rijndael.rb +269 -0
- data/lib/flatulent/crypt/stringxor.rb +27 -0
- data/lib/flatulent.rb +332 -121
- data/lib/flatulent.rb.bak +337 -0
- data/rails/app/controllers/flatulent_controller.rb +61 -6
- data/rails/lib/flatulent/attributes.rb +79 -0
- data/rails/lib/flatulent/crypt/blowfish-tables.rb +190 -0
- data/rails/lib/flatulent/crypt/blowfish.rb +109 -0
- data/rails/lib/flatulent/crypt/cbc.rb +123 -0
- data/rails/lib/flatulent/crypt/gost.rb +140 -0
- data/rails/lib/flatulent/crypt/idea.rb +193 -0
- data/rails/lib/flatulent/crypt/noise.rb +94 -0
- data/rails/lib/flatulent/crypt/purerubystringio.rb +378 -0
- data/rails/lib/flatulent/crypt/rijndael-tables.rb +117 -0
- data/rails/lib/flatulent/crypt/rijndael.rb +269 -0
- data/rails/lib/flatulent/fontfiles/banner.flf +2494 -0
- data/rails/lib/flatulent/fontfiles/big.flf +2204 -0
- data/rails/lib/flatulent/fontfiles/block.flf +1691 -0
- data/rails/lib/flatulent/fontfiles/bubble.flf +1630 -0
- data/rails/lib/flatulent/fontfiles/digital.flf +1286 -0
- data/rails/lib/flatulent/fontfiles/ivrit.flf +900 -0
- data/rails/lib/flatulent/fontfiles/lean.flf +1691 -0
- data/rails/lib/flatulent/fontfiles/mini.flf +899 -0
- data/rails/lib/flatulent/fontfiles/mnemonic.flf +3702 -0
- data/rails/lib/flatulent/fontfiles/script.flf +1493 -0
- data/rails/lib/flatulent/fontfiles/shadow.flf +1097 -0
- data/rails/lib/flatulent/fontfiles/slant.flf +1295 -0
- data/rails/lib/flatulent/fontfiles/small.flf +1097 -0
- data/rails/lib/flatulent/fontfiles/smscript.flf +1097 -0
- data/rails/lib/flatulent/fontfiles/smshadow.flf +899 -0
- data/rails/lib/flatulent/fontfiles/smslant.flf +1097 -0
- data/rails/lib/flatulent/fontfiles/standard.flf +2227 -0
- data/rails/lib/flatulent/fontfiles/term.flf +600 -0
- data/rails/lib/flatulent/pervasives.rb +32 -0
- data/rails/lib/flatulent/stringxor.rb +27 -0
- data/rails/lib/flatulent/text/double_metaphone.rb +356 -0
- data/rails/lib/flatulent/text/figlet/font.rb +117 -0
- data/rails/lib/flatulent/text/figlet/smusher.rb +64 -0
- data/rails/lib/flatulent/text/figlet/typesetter.rb +68 -0
- data/rails/lib/flatulent/text/figlet.rb +17 -0
- data/rails/lib/flatulent/text/levenshtein.rb +65 -0
- data/rails/lib/flatulent/text/metaphone.rb +97 -0
- data/rails/lib/flatulent/text/porter_stemming.rb +171 -0
- data/rails/lib/flatulent/text/soundex.rb +61 -0
- data/rails/lib/flatulent/text.rb +6 -0
- data/rails/lib/flatulent.rb +450 -0
- data/rails/log/development.log +14297 -0
- data/rails/log/fastcgi.crash.log +111 -0
- data/rails/log/lighttpd.access.log +3993 -0
- data/rails/log/lighttpd.error.log +111 -0
- data/rails/tmp/cache/javascripts/prototype.js-gzip-3275912-71260-1183440172 +0 -0
- data/rails/tmp/sessions/ruby_sess.32d68bc997054475 +0 -0
- data/rails/tmp/sessions/ruby_sess.4694a4b9bdf9bcf4 +0 -0
- data/rails/tmp/sessions/ruby_sess.99469fde69043a05 +0 -0
- data/rails/tmp/sessions/ruby_sess.a588c0a457345912 +0 -0
- data/rails/tmp/sessions/ruby_sess.b3344125a84a3efa +0 -0
- data/samples.rb +10 -0
- metadata +69 -3
- data/flatulent-0.0.0.gem +0 -0
@@ -0,0 +1,117 @@
|
|
1
|
+
# rijndael-tables.rb Richard Kernahan <kernighan_rich@rubyforge.org>
|
2
|
+
|
3
|
+
module Crypt
|
4
|
+
module RijndaelTables
|
5
|
+
|
6
|
+
LogTable = [
|
7
|
+
0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3,
|
8
|
+
100, 4, 224, 14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193,
|
9
|
+
125, 194, 29, 181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120,
|
10
|
+
101, 47, 138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142,
|
11
|
+
150, 143, 219, 189, 54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56,
|
12
|
+
102, 221, 253, 48, 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16,
|
13
|
+
126, 110, 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186,
|
14
|
+
43, 121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87,
|
15
|
+
175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230, 173, 232,
|
16
|
+
44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95, 176, 156, 169, 81, 160,
|
17
|
+
127, 12, 246, 111, 23, 196, 73, 236, 216, 67, 31, 45, 164, 118, 123, 183,
|
18
|
+
204, 187, 62, 90, 251, 96, 177, 134, 59, 82, 161, 108, 170, 85, 41, 157,
|
19
|
+
151, 178, 135, 144, 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209,
|
20
|
+
83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242, 211, 171,
|
21
|
+
68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38, 119, 153, 227, 165,
|
22
|
+
103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140, 128, 192, 247, 112, 7
|
23
|
+
]
|
24
|
+
|
25
|
+
AlogTable = [
|
26
|
+
1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53,
|
27
|
+
95, 225, 56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170,
|
28
|
+
229, 52, 92, 228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49,
|
29
|
+
83, 245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205,
|
30
|
+
76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136,
|
31
|
+
131, 158, 185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
|
32
|
+
181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
|
33
|
+
254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160,
|
34
|
+
251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
|
35
|
+
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218, 117,
|
36
|
+
159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122, 142, 137, 128,
|
37
|
+
155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84,
|
38
|
+
252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202,
|
39
|
+
69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14,
|
40
|
+
18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13, 23,
|
41
|
+
57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180, 199, 82, 246, 1
|
42
|
+
]
|
43
|
+
|
44
|
+
S = [
|
45
|
+
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
|
46
|
+
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
|
47
|
+
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
|
48
|
+
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
|
49
|
+
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
|
50
|
+
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
|
51
|
+
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
|
52
|
+
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
|
53
|
+
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
|
54
|
+
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
|
55
|
+
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
|
56
|
+
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
|
57
|
+
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
|
58
|
+
112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
|
59
|
+
225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
|
60
|
+
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22
|
61
|
+
]
|
62
|
+
|
63
|
+
Si = [
|
64
|
+
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251,
|
65
|
+
124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203,
|
66
|
+
84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78,
|
67
|
+
8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37,
|
68
|
+
114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182, 146,
|
69
|
+
108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157, 132,
|
70
|
+
144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69, 6,
|
71
|
+
208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138, 107,
|
72
|
+
58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230, 115,
|
73
|
+
150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223, 110,
|
74
|
+
71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27,
|
75
|
+
252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244,
|
76
|
+
31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95,
|
77
|
+
96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239,
|
78
|
+
160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
|
79
|
+
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125,
|
80
|
+
]
|
81
|
+
|
82
|
+
IG = [
|
83
|
+
[0x0e, 0x09, 0x0d, 0x0b],
|
84
|
+
[0x0b, 0x0e, 0x09, 0x0d],
|
85
|
+
[0x0d, 0x0b, 0x0e, 0x09],
|
86
|
+
[0x09, 0x0d, 0x0b, 0x0e]
|
87
|
+
]
|
88
|
+
|
89
|
+
Rcon = [
|
90
|
+
0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
|
91
|
+
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
|
92
|
+
0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc,
|
93
|
+
0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
|
94
|
+
0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91
|
95
|
+
]
|
96
|
+
|
97
|
+
Shifts = [
|
98
|
+
[
|
99
|
+
[0, 0],
|
100
|
+
[1, 3],
|
101
|
+
[2, 2],
|
102
|
+
[3, 1]
|
103
|
+
], [
|
104
|
+
[0, 0],
|
105
|
+
[1, 5],
|
106
|
+
[2, 4],
|
107
|
+
[3, 3]
|
108
|
+
], [
|
109
|
+
[0, 0],
|
110
|
+
[1, 7],
|
111
|
+
[3, 5],
|
112
|
+
[4, 4]
|
113
|
+
]
|
114
|
+
]
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,269 @@
|
|
1
|
+
# rijndael.rb Richard Kernahan <kernighan_rich@rubyforge.org>
|
2
|
+
|
3
|
+
# Adapted from the reference C implementation:
|
4
|
+
# rijndael-alg-ref.c v2.2 March 2002
|
5
|
+
# Reference ANSI C code
|
6
|
+
# authors: Paulo Barreto and Vincent Rijmen
|
7
|
+
# This code is placed in the public domain.
|
8
|
+
|
9
|
+
module Crypt
|
10
|
+
class Rijndael
|
11
|
+
|
12
|
+
require 'crypt/cbc'
|
13
|
+
include Crypt::CBC
|
14
|
+
|
15
|
+
require 'crypt/rijndael-tables'
|
16
|
+
include Crypt::RijndaelTables
|
17
|
+
|
18
|
+
|
19
|
+
def initialize(userKey, keyBits = 256, blockBits = 128)
|
20
|
+
case keyBits
|
21
|
+
when 128
|
22
|
+
@keyWords = 4
|
23
|
+
when 192
|
24
|
+
@keyWords = 6
|
25
|
+
when 256
|
26
|
+
@keyWords = 8
|
27
|
+
else raise "The key must be 128, 192, or 256 bits long."
|
28
|
+
end
|
29
|
+
|
30
|
+
case (keyBits >= blockBits) ? keyBits : blockBits
|
31
|
+
when 128
|
32
|
+
@rounds = 10
|
33
|
+
when 192
|
34
|
+
@rounds = 12
|
35
|
+
when 256
|
36
|
+
@rounds = 14
|
37
|
+
else raise "The key and block sizes must be 128, 192, or 256 bits long."
|
38
|
+
end
|
39
|
+
|
40
|
+
case blockBits
|
41
|
+
when 128
|
42
|
+
@blockSize = 16
|
43
|
+
@blockWords = 4
|
44
|
+
@shiftIndex = 0
|
45
|
+
when 192
|
46
|
+
@blockSize = 24
|
47
|
+
@blockWords = 6
|
48
|
+
@shiftIndex = 1
|
49
|
+
when 256
|
50
|
+
@blockSize = 32
|
51
|
+
@blockWords = 8
|
52
|
+
@shiftIndex = 2
|
53
|
+
else raise "The block size must be 128, 192, or 256 bits long."
|
54
|
+
end
|
55
|
+
|
56
|
+
uk = userKey.unpack('C'*userKey.length)
|
57
|
+
maxUsefulSizeOfUserKey = (keyBits/8)
|
58
|
+
uk = uk[0..maxUsefulSizeOfUserKey-1] # truncate
|
59
|
+
padding = 0
|
60
|
+
if (userKey.length < keyBits/8)
|
61
|
+
shortfallInUserKey = (keyBits/8 - userKey.length)
|
62
|
+
shortfallInUserKey.times { uk << padding }
|
63
|
+
end
|
64
|
+
@key = [[], [], [], []]
|
65
|
+
0.upto(uk.length-1) { |pos|
|
66
|
+
@key[pos % 4][pos / 4] = uk[pos]
|
67
|
+
}
|
68
|
+
@roundKeys = generate_key_schedule(@key, keyBits, blockBits)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def block_size
|
73
|
+
return(@blockSize) # needed for CBC
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def mul(a, b)
|
78
|
+
if ((a ==0) | (b == 0))
|
79
|
+
result = 0
|
80
|
+
else
|
81
|
+
result = AlogTable[(LogTable[a] + LogTable[b]) % 255]
|
82
|
+
end
|
83
|
+
return(result)
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def add_round_key(blockArray, roundKey)
|
88
|
+
0.upto(3) { |i|
|
89
|
+
0.upto(@blockWords) { |j|
|
90
|
+
blockArray[i][j] ^= roundKey[i][j]
|
91
|
+
}
|
92
|
+
}
|
93
|
+
return(blockArray)
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def shift_rows(blockArray, direction)
|
98
|
+
tmp = []
|
99
|
+
1.upto(3) { |i| # row zero remains unchanged
|
100
|
+
0.upto(@blockWords-1) { |j|
|
101
|
+
tmp[j] = blockArray[i][(j + Shifts[@shiftIndex][i][direction]) % @blockWords]
|
102
|
+
}
|
103
|
+
0.upto(@blockWords-1) { |j|
|
104
|
+
blockArray[i][j] = tmp[j]
|
105
|
+
}
|
106
|
+
}
|
107
|
+
return(blockArray)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def substitution(blockArray, sBox)
|
112
|
+
# replace every byte of the input with the byte at that position in the S-box
|
113
|
+
0.upto(3) { |i|
|
114
|
+
0.upto(@blockWords-1) { |j|
|
115
|
+
blockArray[i][j] = sBox[blockArray[i][j]]
|
116
|
+
}
|
117
|
+
}
|
118
|
+
return(blockArray)
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
def mix_columns(blockArray)
|
123
|
+
mixed = [[], [], [], []]
|
124
|
+
0.upto(@blockWords-1) { |j|
|
125
|
+
0.upto(3) { |i|
|
126
|
+
mixed[i][j] = mul(2,blockArray[i][j]) ^
|
127
|
+
mul(3,blockArray[(i + 1) % 4][j]) ^
|
128
|
+
blockArray[(i + 2) % 4][j] ^
|
129
|
+
blockArray[(i + 3) % 4][j]
|
130
|
+
}
|
131
|
+
}
|
132
|
+
return(mixed)
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
def inverse_mix_columns(blockArray)
|
137
|
+
unmixed = [[], [], [], []]
|
138
|
+
0.upto(@blockWords-1) { |j|
|
139
|
+
0.upto(3) { |i|
|
140
|
+
unmixed[i][j] = mul(0xe, blockArray[i][j]) ^
|
141
|
+
mul(0xb, blockArray[(i + 1) % 4][j]) ^
|
142
|
+
mul(0xd, blockArray[(i + 2) % 4][j]) ^
|
143
|
+
mul(0x9, blockArray[(i + 3) % 4][j])
|
144
|
+
}
|
145
|
+
}
|
146
|
+
return(unmixed)
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
def generate_key_schedule(k, keyBits, blockBits)
|
151
|
+
tk = k[0..3][0..@keyWords-1] # using slice to get a copy instead of a reference
|
152
|
+
keySched = []
|
153
|
+
(@rounds + 1).times { keySched << [[], [], [], []] }
|
154
|
+
t = 0
|
155
|
+
j = 0
|
156
|
+
while ((j < @keyWords) && (t < (@rounds+1)*@blockWords))
|
157
|
+
0.upto(3) { |i|
|
158
|
+
keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
|
159
|
+
}
|
160
|
+
j += 1
|
161
|
+
t += 1
|
162
|
+
end
|
163
|
+
# while not enough round key material collected, calculate new values
|
164
|
+
rconIndex = 0
|
165
|
+
while (t < (@rounds+1)*@blockWords)
|
166
|
+
0.upto(3) { |i|
|
167
|
+
tk[i][0] ^= S[tk[(i + 1) % 4][@keyWords - 1]]
|
168
|
+
}
|
169
|
+
tk[0][0] ^= Rcon[rconIndex]
|
170
|
+
rconIndex = rconIndex.next
|
171
|
+
if (@keyWords != 8)
|
172
|
+
1.upto(@keyWords - 1) { |j|
|
173
|
+
0.upto(3) { |i|
|
174
|
+
tk[i][j] ^= tk[i][j-1];
|
175
|
+
}
|
176
|
+
}
|
177
|
+
else
|
178
|
+
1.upto(@keyWords/2 - 1) { |j|
|
179
|
+
0.upto(3) { |i|
|
180
|
+
tk[i][j] ^= tk[i][j-1]
|
181
|
+
}
|
182
|
+
}
|
183
|
+
0.upto(3) { |i|
|
184
|
+
tk[i][@keyWords/2] ^= S[tk[i][@keyWords/2 - 1]]
|
185
|
+
}
|
186
|
+
(@keyWords/2 + 1).upto(@keyWords - 1) { |j|
|
187
|
+
0.upto(3) { |i|
|
188
|
+
tk[i][j] ^= tk[i][j-1]
|
189
|
+
}
|
190
|
+
}
|
191
|
+
end
|
192
|
+
j = 0
|
193
|
+
while ((j < @keyWords) && (t < (@rounds+1) * @blockWords))
|
194
|
+
0.upto(3) { |i|
|
195
|
+
keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
|
196
|
+
}
|
197
|
+
j += 1
|
198
|
+
t += 1
|
199
|
+
end
|
200
|
+
end
|
201
|
+
return(keySched)
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
def encrypt_byte_array(blockArray)
|
206
|
+
blockArray = add_round_key(blockArray, @roundKeys[0])
|
207
|
+
1.upto(@rounds - 1) { |round|
|
208
|
+
blockArray = substitution(blockArray, S)
|
209
|
+
blockArray = shift_rows(blockArray, 0)
|
210
|
+
blockArray = mix_columns(blockArray)
|
211
|
+
blockArray = add_round_key(blockArray, @roundKeys[round])
|
212
|
+
}
|
213
|
+
# special round without mix_columns
|
214
|
+
blockArray = substitution(blockArray,S)
|
215
|
+
blockArray = shift_rows(blockArray,0)
|
216
|
+
blockArray = add_round_key(blockArray, @roundKeys[@rounds])
|
217
|
+
return(blockArray)
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
def encrypt_block(block)
|
222
|
+
raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
|
223
|
+
blockArray = [[], [], [], []]
|
224
|
+
0.upto(@blockSize - 1) { |pos|
|
225
|
+
blockArray[pos % 4][pos / 4] = block[pos]
|
226
|
+
}
|
227
|
+
encryptedBlock = encrypt_byte_array(blockArray)
|
228
|
+
encrypted = ""
|
229
|
+
0.upto(@blockSize - 1) { |pos|
|
230
|
+
encrypted << encryptedBlock[pos % 4][pos / 4]
|
231
|
+
}
|
232
|
+
return(encrypted)
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
def decrypt_byte_array(blockArray)
|
237
|
+
# first special round without inverse_mix_columns
|
238
|
+
# add_round_key is an involution - applying it a second time returns the original result
|
239
|
+
blockArray = add_round_key(blockArray, @roundKeys[@rounds])
|
240
|
+
blockArray = substitution(blockArray,Si) # using inverse S-box
|
241
|
+
blockArray = shift_rows(blockArray,1)
|
242
|
+
(@rounds-1).downto(1) { |round|
|
243
|
+
blockArray = add_round_key(blockArray, @roundKeys[round])
|
244
|
+
blockArray = inverse_mix_columns(blockArray)
|
245
|
+
blockArray = substitution(blockArray, Si)
|
246
|
+
blockArray = shift_rows(blockArray, 1)
|
247
|
+
}
|
248
|
+
blockArray = add_round_key(blockArray, @roundKeys[0])
|
249
|
+
return(blockArray)
|
250
|
+
end
|
251
|
+
|
252
|
+
|
253
|
+
def decrypt_block(block)
|
254
|
+
raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
|
255
|
+
blockArray = [[], [], [], []]
|
256
|
+
0.upto(@blockSize - 1) { |pos|
|
257
|
+
blockArray[pos % 4][pos / 4] = block[pos]
|
258
|
+
}
|
259
|
+
decryptedBlock = decrypt_byte_array(blockArray)
|
260
|
+
decrypted = ""
|
261
|
+
0.upto(@blockSize - 1) { |pos|
|
262
|
+
decrypted << decryptedBlock[pos % 4][pos / 4]
|
263
|
+
}
|
264
|
+
return(decrypted)
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
end
|
269
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# stringxor.rb Richard Kernahan <kernighan_rich@rubyforge.org>
|
2
|
+
|
3
|
+
module Crypt
|
4
|
+
module StringXor
|
5
|
+
|
6
|
+
|
7
|
+
def ^(aString)
|
8
|
+
a = self.unpack('C'*(self.length))
|
9
|
+
b = aString.unpack('C'*(aString.length))
|
10
|
+
if (b.length < a.length)
|
11
|
+
(a.length - b.length).times { b << 0 }
|
12
|
+
end
|
13
|
+
xor = ""
|
14
|
+
0.upto(a.length-1) { |pos|
|
15
|
+
x = a[pos] ^ b[pos]
|
16
|
+
xor << x.chr()
|
17
|
+
}
|
18
|
+
return(xor)
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class String
|
26
|
+
include Crypt::StringXor
|
27
|
+
end
|