4

Dwolla 允许应用程序请求和存储用户的 PIN 作为预授权形式,但要求对其进行加密。从服务条款

必须使用 FIPS 140-2 标准(至少)在传输和静态(包括任何和所有备份介质)中加密 PIN

通常,我会使用 Bcrypt 加密(实际上是进行安全哈希。Neil Slater,感谢您的更正)(使用 bcrypt-ruby gem),例如密码。但是如果我用 Bcrypt 加密,那么我必须传输散列,当然这与 Dwolla 的期望不匹配,并且 PIN 将被拒绝。

您如何对 PIN 进行加密和解密以实现安全传输?

更新:

Andrew 链接到下面的问题的答案之一是引用OpenSSL:Cipher,并且使用它我可以使用以下代码加密 PIN。但剩下的问题是:

  1. 我应该如何存储密钥、iv(初始化向量)和密码?保存为环境变量是否安全,还是将数据库表放入安全哈希中会更好?
  2. 下面的代码作为一种加密 PIN 的方式是否有意义?
  3. 由于我没有来自 Dwolla 的公钥,那么传输它的最佳方式是什么?

pin = "1111" # this is what needs to be encrypted

#encryption:
cipher = OpenSSL::Cipher.new('AES-128-CBC') #=> #<OpenSSL::Cipher:0x00000100ef09d8>
cipher.encrypt
key = cipher.random_key #=> odd characters...
iv = cipher.random_iv #=> odd characters...
encrypted = cipher.update(pin) + cipher.final #=> odd characters...

#dcryption: 
decipher = OpenSSL::Cipher::AES.new(128, :CBC)
decipher.decrypt
decipher.key = key
decipher.iv = iv
plain = decipher.update(encrypted) + decipher.final

puts plain == pin #=> true
4

2 回答 2

3

所以这就是我发现的。在 Rails 中,只生成一次密钥并将其存储为环境变量(并在部署时对其进行加密)。为每个引脚生成一个新的 iv(初始化向量)。将 iv 和加密的 pin 存储在数据库中。

您可能希望将加密的 PIN 和 IV 转换为 UTF8,以便在不更改设置数据库的方式的情况下成功保存。(默认情况下,它们将生成为 ASCII 8 位)。

这是在 User 模型中执行此操作的一种方法,但您可能需要重构,因为这些方法很大:

def dwolla_pin   # => this is to decrypt the PIN in order to use it
    unless encrypted_dwolla_pin.nil?
      decipher = OpenSSL::Cipher::AES.new(128, :CBC)
      decipher.decrypt
      decipher.key = ENV["ENCRYPT_KEY"]

      # Convert IV from UTF8 (as stored) back to ASCII-8bit (for OpenSSL)
      utf8_iv = self.iv_for_pin
      decipher.iv = Base64.decode64(utf8_iv.encode('ascii-8bit'))

      # Convert PIN from UTF8 (as stored) back to ASCII-8bit (for OpenSSL)
      utf8_pin = self.encrypted_dwolla_pin
      ascii_pin = Base64.decode64(utf8_pin.encode('ascii-8bit'))

      dwolla_pin ||= decipher.update(ascii_pin) + decipher.final
    end
  end

  def dwolla_pin=(new_pin)  # => this is to encrypt the PIN in order to store it
    return false unless valid_pin?(new_pin)
    cipher = OpenSSL::Cipher.new('AES-128-CBC')
    cipher.encrypt
    cipher.key = ENV["ENCRYPT_KEY"]

    # Create IV and convert to UTF-8 for storage in database
    iv = cipher.random_iv
    utf8_iv = Base64.encode64(iv).encode('utf-8')
    self.update_attribute(:iv_for_pin, utf8_iv)

    # Encrypt PIN and convert to UTF-8 for storage in database
    encrypted_pin = cipher.update(new_pin) + cipher.final
    utf8_pin = Base64.encode64(encrypted_pin).encode('utf-8')
    self.update_attribute(:encrypted_dwolla_pin, utf8_pin)
  end

  def valid_pin?(pin)  # => Here I'm just checking to make sure the PIN is basically in the right format
    pin.match(/^\d{4}/) && pin.length == 4
  end

“安全传输”是指用于使用的 SSL 和用于部署的 SSH。如果部署到 Heroku,那么已经使用 SSH,但对于 SSL,您需要从您的 DNS 主机通配符证书和 Heroku 上的 ssl 端点购买。

有人对此有什么要补充的吗?

于 2013-11-22T20:32:18.257 回答
0

在这种情况下,我会使用公钥/私钥加密。不是 Ruby 方面的专家,但此链接可能会有所帮助:

Ruby:使用私钥/公钥进行文件加密/解密

如果您的 pin 被发送到外部,那么您需要最终用户的公钥进行加密。如果这不可能,那么您可以混合使用不对称(公共/私有)和对称算法 - 基本上是 SSH 所做的。

http://en.wikipedia.org/wiki/Secure_Shell

于 2013-11-15T16:23:38.493 回答