From: roob noob Date: 2012-05-17T03:16:31+09:00 Subject: what is going wrong here? Notice the initialization of both classes in each of the examples, if the instance variable is in initialization or in the called method is the only thing changing between these examples. I don't understand why I can't begin both classes with the initialization set to what I want it to be, I would think that the initialization value would be consistent for the objects entire life but it only works for the first pass, the second time through it doesn't work. The only way to fix this is to not have the respective instance variable set in initialization with either of the two classes. What is going wrong here? I like to set variables during initialization but it clearly doesn't work how I would hope, I would like to understand what is going wrong because even though I can get it to work I don't see why it doesn't work in some cases. Maybe it has to do with my poor use of instance variables :/, if this is the case I would still like to know what is going on so I can find a way to work around it when I say it doesn't work I mean the output is not correct, it gives me random looking output the second time even though it should give the plaintext. ###############This way doesn't work################## class EncryptionBackend require 'openssl' def initialize @cipher = OpenSSL::Cipher::Cipher.new("AES-256-CTR") end def encrypt(key, plaintext) puts plaintext @cipher.encrypt @cipher.key = key ciphertext = @cipher.update(plaintext) ciphertext << @cipher.final @cipher.reset puts ciphertext @ciphertext = ciphertext end end class EncryptMessage require 'openssl' def initialize @crypto = EncryptionBackend.new end def twice @message = "whatever" encrypt_message encrypt_message end def encrypt_message @sha256 = OpenSSL::Digest::SHA256.new key = @sha256.digest("whatever") @message = @crypto.encrypt(key, @message) end end message_encrypt = EncryptMessage.new message_encrypt.twice ###############This way works################### class EncryptionBackend require 'openssl' def initialize @cipher = OpenSSL::Cipher::Cipher.new("AES-256-CTR") end def encrypt(key, plaintext) puts plaintext @cipher.encrypt @cipher.key = key ciphertext = @cipher.update(plaintext) ciphertext << @cipher.final @cipher.reset puts ciphertext @ciphertext = ciphertext end end class EncryptMessage require 'openssl' def initialize #nothing end def twice @message = "whatever" encrypt_message encrypt_message end def encrypt_message @crypto = EncryptionBackend.new @sha256 = OpenSSL::Digest::SHA256.new key = @sha256.digest("whatever") @message = @crypto.encrypt(key, @message) end end message_encrypt = EncryptMessage.new message_encrypt.twice ###############this way works though################## class EncryptionBackend require 'openssl' def initialize #nothing end def encrypt(key, plaintext) @cipher = OpenSSL::Cipher::Cipher.new("AES-256-CTR") puts plaintext @cipher.encrypt @cipher.key = key ciphertext = @cipher.update(plaintext) ciphertext << @cipher.final @cipher.reset puts ciphertext @ciphertext = ciphertext end end class EncryptMessage require 'openssl' def initialize @crypto = EncryptionBackend.new end def twice @message = "whatever" encrypt_message encrypt_message end def encrypt_message @sha256 = OpenSSL::Digest::SHA256.new key = @sha256.digest("whatever") @message = @crypto.encrypt(key, @message) end end message_encrypt = EncryptMessage.new message_encrypt.twice ################This way also works####################### class EncryptionBackend require 'openssl' def initialize @cipher = OpenSSL::Cipher::Cipher.new("AES-256-CTR") end def encrypt(key, plaintext) puts plaintext @cipher.encrypt @cipher.key = key ciphertext = @cipher.update(plaintext) ciphertext << @cipher.final @cipher.reset puts ciphertext @ciphertext = ciphertext end end class EncryptMessage require 'openssl' def initialize ##nothing end def twice @message = "whatever" encrypt_message encrypt_message end def encrypt_message @crypto = EncryptionBackend.new @sha256 = OpenSSL::Digest::SHA256.new key = @sha256.digest("whatever") @message = @crypto.encrypt(key, @message) end end message_encrypt = EncryptMessage.new message_encrypt.twice -- Posted via http://www.ruby-forum.com/.