From: Eleanor McHugh Date: 2009-05-04T00:50:33+09:00 Subject: Re: OpenSSL::Cipher dad decrypt On 3 May 2009, at 14:44, Pål Bergström wrote: > This is what I use. Would be good if I could catch the error and > prevent > it from "hanging" the browser with an error (using Rails). > def self.decrypt(cstring) > cgi = CGI.new("html3") > passo = cgi.cookies["thecookie"].to_s > etext = Base64.decode64(cstring) > c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") > c.decrypt > c.key = key = Digest::SHA2.digest(passo) > d = c.update(etext) > d << c.final > return d > end You need to do something along the lines of: begin c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") c.decrypt c.key = key = Digest::SHA2.digest(passo) d = c.update(etext) d << c.final d rescue OpenSSL::CipherError => e "incorrect password" rescue Exception => e "unknown error" end where you can have several rescue blocks, one for each exception recovery behaviour you have a use for. Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason