i need to do encryption and decryption in my rails app. Im trying to use ezcrypto, but whenever i do decryption i get this error.
OpenSSL::Cipher::CipherError in ProfilesController#show
wrong final block length
What would need to be changed to stop this error. I tried using another implementation of openssl like this (methods to be called from my model)
def encrypt_attr(unencrypted)
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::SHA1.hexdigest('pass')
e = c.update(unencrypted)
e << c.final
return e
end
def decrypt_attr(encrypted_attr)
if encrypted_attr != ""
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.decrypt
c.key = Digest::SHA1.hexdigest('pass')
d = c.update(encrypted_attr)
d << c.final
return d
end
end
It throws the exact same error on decryption. How should i do encryption and decryption and not get this openssl error.
require 'openssl'
require 'base64'
class AesEncryptDecrypt
KEY = "EncryptDecryptGurudathBN"
ALGORITHM = 'AES-128-ECB'
def self.encryption(msg)
begin
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.encrypt()
cipher.key = KEY
crypt = cipher.update(msg) + cipher.final()
crypt_string = (Base64.encode64(crypt))
return crypt_string
rescue Exception => exc
puts ("Message for the encryption log file for message #{msg} = #{exc.message}")
end
end
def self.decryption(msg)
begin
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.decrypt()
cipher.key = KEY
tempkey = Base64.decode64(msg)
crypt = cipher.update(tempkey)
crypt << cipher.final()
return crypt
rescue Exception => exc
puts ("Message for the decryption log file for message #{msg} = #{exc.message}")
end
end
end
irb(main):007:0> AesEncryptDecrypt.encryption('gurudath')
=> "rUPKObydUJd9cY9agm3Glw==\n"
irb(main):008:0> AesEncryptDecrypt.decryption('rUPKObydUJd9cY9agm3Glw==')
=> "gurudath"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With