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.
Also notice that I reset @cipher. I thought that by resetting @cipher it
would make it so I essentially AM calling a new object because the
entire objects state should be reset to what it was prior to when I
called it, all of the other variables in EncryptionBackend should be
reset when it is called the second time because it is passed a new
plaintext and a new key which should replace what was there before
without needing to make a new object…so why on earth doesn’t it
work ahhh.
###############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 does 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
#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 also 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