简体   繁体   English

Raisl 4-attr_encrypted:如何通过选择加密值来解密值

[英]Raisl 4 - attr_encrypted : How to decrypt values with select on encrypted values

We are encrypting values in table / model - RegisteredDomain, using attr_encrypted below 我们正在使用下面的attr_encrypted对表/模型-RegisteredDomain中的值进行加密

attr_encrypted :domain_name, :registered_by, :api_key, :key => "65xU4TdZntTV53"

Values get encrypted and stored to db as well. 值被加密并存储到数据库。 Below code saves values to db. 下面的代码将值保存到db。

registereddomain = RegisteredDomain.new(
        :domain_name => domain_name,
        :api_key =>  api_key,
        :hash_key => hash_key,
        :registered_by => ep_id,
        :status =>  status,
        :domain_type_id => domain_type_id              
        )
registereddomain.save

Problem is with decrypting values with below select. 问题在于使用以下选择解密值。 We get encrypted values here, could anyone tell how to get decrytpted values in listing. 我们在这里获得加密的值,谁能说出如何在清单中获取解密的值。

def select_all
  @registered_domains = RegisteredDomain.select("id, encrypted_domain_name, domain_type_id, encrypted_api_key, status").order(updated_at: :desc)
  return @registered_domains
end

There is probably a reason to store values in the database in encrypted mode. 可能有理由以加密模式将值存储在数据库中。 Hence if you want to decrypt the value, just call the appropriate method on an instance method: 因此,如果要解密该值,只需在实例方法上调用适当的方法即可:

@registered_domains = RegisteredDomain.select("id, encrypted_domain_name, domain_type_id, encrypted_api_key, status").order(updated_at: :desc)
@registered_domains.first.domain_name

Edit 编辑

you migth want to create a custom method for pulling the values from the database: 您米格思想创建一个自定义方法来从数据库中提取值:

def self.all_decrypted(columns)
  all.map do |record|
    columns.map do |column|
      record.send(column.to_sym)
    end
  end
end

RegisteredDomain.all_decrypted([:domain_name, :api_key]) This will return you an array of arrays, actual values of domain_name , api_key . RegisteredDomain.all_decrypted([:domain_name,:api_key])这将返回一个数组数组,即domain_name实际值和api_key

It'll be good if you can give some sample code of yours for controller and view as well.. As I am seeing you are directly using fields without the attribute option; 如果您还可以为控制器和视图提供一些示例代码,那就太好了。正如我所看到的,您直接使用没有attribute选项的字段; so in your database the field domain_name mus be represented as encrypted_domain_name . 因此,在您的数据库中,域domain_name可以表示为encrypted_domain_name So here in your select query you can try to modify your the attribute encrypted_domain_name to domain_name . 所以,在你这里选择查询,你可以尝试修改您的属性encrypted_domain_namedomain_name Also you should modify it where-ever you want to get the encrypted attribute. 此外,无论何时要获取加密属性,都应对其进行修改。 (I am not able to write the complete code-block properly hence I tried to explain it in short; but just renaming the attribute should work!) (我无法正确编写完整的代码块,因此我试图简要解释一下;但是只要重命名属性就可以!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM