Module: Rmega::Crypto::Rsa

Extended by:
Rsa
Included in:
Rsa
Defined in:
lib/rmega/crypto/rsa.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(m, pqdu) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rmega/crypto/rsa.rb', line 17

def decrypt(m, pqdu)
  p, q, d, u = pqdu
  if p && q && u
    m1 = powm(m, d % (p - 1), p)
    m2 = powm(m, d % (q - 1), q)
    h = m2 - m1
    h = h + q if h < 0
    h = h * u % q
    h * p + m1
  else
    pow_m(m, d, p * q)
  end
end

#powm(b, p, m) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'lib/rmega/crypto/rsa.rb', line 6

def powm(b, p, m)
  if p == 1
    b % m
  elsif (p & 0x1) == 0
    t = powm(b, p >> 1, m)
    (t * t) % m
  else
    (b * powm(b, p-1, m)) % m
  end
end