Class: SJCL::Cipher::AES
- Inherits:
-
Object
- Object
- SJCL::Cipher::AES
- Defined in:
- lib/sjcl/aes.rb
Constant Summary collapse
- TABLES =
SJCL::Cipher::AES_Tables::TABLES
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #decrypt(data) ⇒ Object
- #encrypt(data) ⇒ Object
-
#initialize(key) ⇒ AES
constructor
A new instance of AES.
- #schedule_keys ⇒ Object
Constructor Details
#initialize(key) ⇒ AES
Returns a new instance of AES.
8 9 10 11 12 |
# File 'lib/sjcl/aes.rb', line 8 def initialize(key) @raw_key = key @keyLen = key.length schedule_keys end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/sjcl/aes.rb', line 6 def key @key end |
Instance Method Details
#decrypt(data) ⇒ Object
56 57 58 |
# File 'lib/sjcl/aes.rb', line 56 def decrypt(data) crypt(data,1) end |
#encrypt(data) ⇒ Object
52 53 54 |
# File 'lib/sjcl/aes.rb', line 52 def encrypt(data) crypt(data,0) end |
#schedule_keys ⇒ Object
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 |
# File 'lib/sjcl/aes.rb', line 14 def schedule_keys sbox = TABLES[0][4] decTable = TABLES[1] encKey = @raw_key.dup decKey = [] rcon = 1 i = @keyLen j = 0 while i < 4*@keyLen + 28 tmp = encKey[i-1] ? encKey[i-1] & 0xFFFFFFFF : 0 if (i % @keyLen === 0 || (@keyLen === 8 && i % @keyLen === 4)) tmp = sbox[tmp >> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255] if (i % @keyLen === 0) tmp = tmp<<8 ^ tmp >> 24 ^ rcon << 24 rcon = rcon << 1 ^ (rcon >> 7) * 283 end end encKey[i] = (encKey[i-@keyLen] ^ tmp) & 0xFFFFFFFF; i += 1 end while i > 0 tmp = encKey[j & 3 != 0 ? i : i - 4]; tmp = tmp & 0xFFFFFFFF if (i<=4 || j<4) decKey[j] = tmp; else decKey[j] = decTable[0][sbox[tmp >> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]] end decKey[j] = decKey[j] & 0xFFFFFFFF i -= 1 j += 1 end @key = [encKey, decKey] end |