I'm trying to decrypt some data I'm fetching from an API and getting some weird errors.
The data I'm fetching was encrypted using Blowfish and then encoded to a base64 string and made available in a JSON string. Here's a sample of what that JSON string looks like
# get json data
response = Net::HTTP.get(URI('http://www.url-to-json.com'))
results = JSON.parse(response)
# decode the base64 results
decoded = Base64.decode64(results['payload'])
# setup blowfish object with key
blowfish = Crypt::Blowfish.new('my_secret_key')
# decrypt the data
/Users/Ken/.rvm/gems/ruby-1.9.3-p327@vs/gems/crypt-2.2.1/lib/crypt/stringxor.rb:4:in `^': undefined method `b' for "java.uti":String (NoMethodError)
from /Users/Ken/.rvm/gems/ruby-1.9.3-p327@vs/gems/crypt-2.2.1/lib/crypt/cbc.rb:62:in `decrypt_stream'
from /Users/Ken/.rvm/gems/ruby-1.9.3-p327@vs/gems/crypt-2.2.1/lib/crypt/cbc.rb:115:in `decrypt_string'
from /Users/Ken/Code/vs/scripts/test.rb:55:in `run'
from init.rb:43:in `<main>'
The culprit is this
b method. It was introduced in Ruby 2.0. As you can see in the documentation it is returning copy of a string with ASCII-8BIT encoding. You can either update ruby version or monkey-patch String class to add this method. It is normally implemented in C, but I think this Ruby implementation will work as well:
class String def b self.dup.force_encoding("ASCII-8BIT") end end