Method: RSA::OAEP#decode

Defined in:
lib/rsa_ext.rb

#decode(k, c, p = '') ⇒ String

Performs the rsa-oaep-mgf1 decrypt algorithm. This is specified in section 7.1.2 of www.ietf.org/rfc/rfc2437.txt.

This implementation assumes that the sha1 hashing algorithm was used.

Parameters:

  • k (RSA::Key)

    the private key whose public key was used to encrypt the data

  • c (String)

    a string of raw bytes representing the text to be decoded

  • p (String) (defaults to: '')

    the options which were used in the original encoding of the string. By default this is the empty string.

Returns:

  • (String)

    the decoded string of bytes

Raises:

  • (DecodeError)

    If decoding cannot occur, an error is raised



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rsa_ext.rb', line 32

def decode k, c, p = ''
  # First, generate how many bytes the key's modulus is
  n = k.modulus
  bytes = 0
  while n > 0
    bytes += 1
    n /= 2
  end
  bytes /= 8

  raise DecodeError, 'input is wrong length!' unless c.length == bytes

  enc = RSA::PKCS1.os2ip c
  m   = RSA::PKCS1.rsadp k, enc
  em  = RSA::PKCS1.i2osp m, bytes - 1

  eme_decode em, p
end