0

I have the following situation in my rails 5.2.4.2 app (development environment):

file: chiffres_controller.rb

def encrypt text
    text = text.to_s unless text.is_a? String
    len = ActiveSupport::MessageEncryptor.key_len
    salt = SecureRandom.hex len
    key = ActiveSupport::KeyGenerator.new(Rails.application.credentials.secret_key_base).generate_key(salt, len)
    crypt = ActiveSupport::MessageEncryptor.new key
    encrypted_data = crypt.encrypt_and_sign text
    "#{salt}$$#{encrypted_data}"
  end

  def decrypt text
    salt, data = text.split "$$"
    len = ActiveSupport::MessageEncryptor.key_len
    key = ActiveSupport::KeyGenerator.new(Rails.application.credentials.secret_key_base).generate_key(salt, len)
    crypt = ActiveSupport::MessageEncryptor.new key
    crypt.decrypt_and_verify data
  end

This is used to show or hide my secret strings which contain only numbers. It worked till today after a reboot. Last change was new scheduler instance of rufus-scheduler that works with these methods. Then I got the following message:

ActionView::Template::Error (ActiveSupport::MessageEncryptor::InvalidMessage):
    22:               <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
    23:                 <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
    24:               <% else %>
    25:                 <% block.call day, sorted_events.fetch(day, []) %>
    26:               <% end %>
    27:             <% end %>
    28:           <% end %>

app/controllers/chiffres_controller.rb:157:in `decrypt'
app/views/presences/index.html.erb:26:in `block (2 levels) in _app_views_presences_index_html_erb__473195322_124069440'
app/views/presences/index.html.erb:24:in `each'
app/views/presences/index.html.erb:24:in `block in _app_views_presences_index_html_erb__473195322_124069440'
app/views/presences/index.html.erb:20:in `_app_views_presences_index_html_erb__473195322_124069440'

line 157 is the following: crypt.decrypt_and_verify data

my index.html.rb looks like (line 20 - 26):

<%= calendar number_of_days: 1, events: @meetings do |date, meetings| %>
<div style="width: 100%"><%= date.strftime("%d.%m.%Y") %> <%= link_to "Coronaliste (xlsx)", corona_path(format: "xlsx", :date => date) %></div>
<br/>
<% if meetings.present? %>
  <% meetings.each do |met| %>
    <% if met.place_id != 20 %>
      <% @theres[@ch.decrypt(met.secret).to_i] = "i" %>

I have read that I should regenerate my master.key and credentials.yml.enc like here. I did that but nothing has changed. I am working on a Windows 10 machine and I do not know why that happens. I need a fix, so that I can read my secret data (numbers) again. Any help is appreciated.

DJ MacHack
  • 47
  • 10

0 Answers0