Module: Aes
- Defined in:
- lib/ruby-aes.rb
Overview
This file is a part of ruby-aes <rubyforge.org/projects/ruby-aes>
Written by Alex Boussinet <[email protected]>
Valid modes are:
* ECB (Electronic Code Book)
* CBC (Cipher Block Chaining)
* OFB (Output Feedback)
* CFB (Cipher Feedback)
Valid key length:
* 128 bits
* 192 bits
* 256 bits
API calls:
Default key_length: 128
Default mode: 'ECB'
Default IV: 16 null chars ("00" * 16 in hex format)
Default key: 16 null chars ("00" * 16 in hex format)
Default input text: "PLAINTEXT"
Aes.check_key(key_string, key_length)
Aes.check_iv(iv_string)
Aes.check_kl(key_length)
Aes.check_mode(mode)
Aes.init(key_length, mode, key, iv)
Aes.encrypt_block(key_length, mode, key, iv, block) # no padding
Aes.decrypt_block(key_length, mode, key, iv, block) # no padding
Aes.encrypt_buffer(key_length, mode, key, iv, block) # padding
Aes.decrypt_buffer(key_length, mode, key, iv, block) # padding
Aes.encrypt_stream(key_length, mode, key, iv, sin, sout)
Aes.decrypt_stream(key_length, mode, key, iv, sin, sout)
Aes.bs() # block size for read operations (stream)
Aes.bs=(bs)
Constant Summary collapse
- @@aes =
nil
- @@bs =
4096
Class Method Summary collapse
- .bs ⇒ Object
- .bs=(bs) ⇒ Object
- .check_iv(iv_string) ⇒ Object
- .check_key(key_string, kl = 128) ⇒ Object
- .check_kl(key_length) ⇒ Object
- .check_mode(mode) ⇒ Object
- .decrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") ⇒ Object
- .decrypt_buffer(keyl, mode, key, iv, buffer = "DEFAULT PLAINTXT") ⇒ Object
- .decrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) ⇒ Object
- .encrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") ⇒ Object
- .encrypt_buffer(keyl, mode, key, iv, buffer = "PLAINTEXT") ⇒ Object
- .encrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) ⇒ Object
- .init(keyl, mode, key, iv) ⇒ Object
Class Method Details
.bs ⇒ Object
45 |
# File 'lib/ruby-aes.rb', line 45 def Aes.bs(); return @@bs end |
.bs=(bs) ⇒ Object
46 |
# File 'lib/ruby-aes.rb', line 46 def Aes.bs=(bs); @@bs = bs.to_i; @@bs==0 ? 4096 : @@bs = @@bs - @@bs%16 end |
.check_iv(iv_string) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby-aes.rb', line 61 def Aes.check_iv(iv_string) k = iv_string.length hex = (iv_string =~ /[a-f0-9A-F]{#{k}}/) == 0 bin = ! hex if k == 32 && hex return [iv_string].pack("H*") elsif k == 16 && bin return iv_string else raise "Bad IV string" end end |
.check_key(key_string, kl = 128) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby-aes.rb', line 48 def Aes.check_key(key_string, kl = 128) kl = Aes.check_kl(kl) k = key_string ? key_string.length : 0 raise "Bad key string or bad key length" if (k != kl/8) && (k != kl/4) hex = (key_string =~ /[a-f0-9A-F]{#{k}}/) == 0 && (k == kl/4) bin = ! hex if ! (([32, 48, 64].include?(k) && hex) || ([16, 24, 32].include?(k) && bin)) raise "Bad key string" end hex ? [key_string].pack("H*") : key_string end |
.check_kl(key_length) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/ruby-aes.rb', line 82 def Aes.check_kl(key_length) case key_length when 128, 192, 256 else raise "Bad key length" end key_length end |
.check_mode(mode) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/ruby-aes.rb', line 74 def Aes.check_mode (mode) case mode when 'ECB', 'CBC', 'OFB', 'CFB' else raise "Bad cipher mode" end mode end |
.decrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") ⇒ Object
106 107 108 109 |
# File 'lib/ruby-aes.rb', line 106 def Aes.decrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") Aes.init(keyl, mode, key, iv) @@aes.decrypt_block(block) end |
.decrypt_buffer(keyl, mode, key, iv, buffer = "DEFAULT PLAINTXT") ⇒ Object
116 117 118 119 120 |
# File 'lib/ruby-aes.rb', line 116 def Aes.decrypt_buffer(keyl, mode, key, iv, buffer = "DEFAULT PLAINTXT") raise "Bad Block size" if buffer.length < 16 Aes.init(keyl, mode, key, iv) @@aes.decrypt_buffer(buffer) end |
.decrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ruby-aes.rb', line 145 def Aes.decrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) Aes.init(keyl, mode, key, iv) case sout when String, Array, IO else raise "Bad output stream (String, Array, IO)" end case sin when String sout << @@aes.decrypt_buffer(sin) when IO while buf = sin.read(@@bs)#+1) if buf.length == @@bs sout << @@aes.decrypt_blocks(buf) else sout << @@aes.decrypt_buffer(buf) end end else raise "Bad input stream (String, IO)" end end |
.encrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") ⇒ Object
100 101 102 103 104 |
# File 'lib/ruby-aes.rb', line 100 def Aes.encrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT") raise "Bad Block size" if block.length < 16 || block.length > 16 Aes.init(keyl, mode, key, iv) @@aes.encrypt_block(block) end |
.encrypt_buffer(keyl, mode, key, iv, buffer = "PLAINTEXT") ⇒ Object
111 112 113 114 |
# File 'lib/ruby-aes.rb', line 111 def Aes.encrypt_buffer(keyl, mode, key, iv, buffer = "PLAINTEXT") Aes.init(keyl, mode, key, iv) @@aes.encrypt_buffer(buffer) end |
.encrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ruby-aes.rb', line 122 def Aes.encrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT) Aes.init(keyl, mode, key, iv) case sout when String, Array, IO else raise "Bad output stream (String, Array, IO)" end case sin when String sout << @@aes.encrypt_buffer(sin) when IO while buf = sin.read(@@bs) if buf.length == @@bs sout << @@aes.encrypt_blocks(buf) else sout << @@aes.encrypt_buffer(buf) end end else raise "Bad input stream (String, IO)" end end |
.init(keyl, mode, key, iv) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/ruby-aes.rb', line 90 def Aes.init(keyl, mode, key, iv) unless @@aes @@aes = AesAlg.new(Aes.check_kl(keyl), Aes.check_mode(mode), Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil) else @@aes.init(Aes.check_kl(keyl), Aes.check_mode(mode), Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil) end end |