Ruby 1.9.1 リファレンスマニュアル > ライブラリ一覧 > opensslライブラリ > OpenSSL::Cipher::Cipherクラス

class OpenSSL::Cipher::Cipher

クラスの継承リスト: OpenSSL::Cipher::Cipher < Object < Kernel < BasicObject

要約

対称鍵暗号のために抽象化されたインターフェースを提供するクラスです。

以下のクラスが実装されています。

AES も OpenSSL::Cipher::Cipher.new('aes256') などとすれば利用可能です。

ruby 1.8.3 から Cast5 と Idea が CAST5 と IDEA に改名されました。

使用例

require 'openssl'

p a123 = "0123456789"
pass = "hogehoge"
enc = OpenSSL::Cipher::DES.new
enc.encrypt
enc.pkcs5_keyivgen(pass)
p a = enc.update(a123)
p b = enc.final
s = a + b

dec = OpenSSL::Cipher::DES.new
dec.decrypt
dec.pkcs5_keyivgen(pass)
p a = dec.update(s)
p b = dec.final
p a + b #=> "0123456789"

特異メソッド

定義 説明
new(name)
new

対称鍵暗号アルゴリズムの名前 name を与えると暗号オブジェクトを生成します。 利用できるアルゴリズムはシステムにインストールされている openssl に依存します。

インスタンスメソッド

定義 説明
block_size

ブロックのサイズを返します。

decrypt(key, iv)

復号化の準備をします。iv は initialization vector のことで、必要なら使用されます。 この API に引数を渡すことは推奨されません。パスワードから鍵を生成する場合は、 pkcs5_keyivgen を使用してください。

encrypt(key, iv)

暗号化の準備をします。iv は initialization vector のことで、必要なら使用されます。 この API に引数を渡すことは推奨されません。パスワードから鍵を生成する場合は、 pkcs5_keyivgen を使用してください。

final

ブロックに残っているデータを暗号化あるいは復号化します。final を呼んだあとでは update を呼んではいけません。

iv=(iv)

初期ベクトル (initialization vector)。

iv_len

初期ベクトルの長さ。

key=(key)

暗号鍵を設定します。 なお、ここでいう「暗号鍵」は AES のアルゴリズムに渡される鍵であって、「パスワード」ではありません。

key_len
key_len=(length)

暗号鍵の長さ。

padding=(padding)

パディングを設定します。

pkcs5_keyivgen(pass, salt=nil, num=2048, digest=OpenSSL::Digest::MD5.new())

pass から key と iv を生成し key= と iv= に設定します。 salt は 8-octet string でないといけません。nil を返します。

random_iv
random_key
reset

暗号、暗号鍵、暗号化復号化フラグを共にリセットします。

update(data)

与えられた文字列を暗号化あるいは復号化して文字列として返します。直前に呼ばれた encrypt あるいは decrypt により暗号化か復号化かが決まります。

継承したメソッド

! != == === =~ __id__ __send__ _dump class clone dclone display enum_for eql? equal? extend freeze frozen? hash initialize initialize_copy inspect instance_eval instance_exec instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? marshal_dump marshal_load method method_missing methods must_be must_be_close_to must_be_empty must_be_instance_of must_be_kind_of must_be_nil must_be_same_as must_be_within_epsilon must_equal must_include must_match must_raise must_respond_to must_send must_throw nil? pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods public_methods remove_instance_variable respond_to? singleton_method_added singleton_method_removed singleton_method_undefined singleton_methods taint tainted? tap to_a to_ary to_hash to_int to_io to_proc to_regexp to_s to_str to_yaml to_yaml_properties to_yaml_style trust untaint untrust untrusted? wont_be wont_be_close_to wont_be_empty wont_be_instance_of wont_be_kind_of wont_be_nil wont_be_same_as wont_be_within_epsilon wont_equal wont_include wont_match wont_respond_to