Class: CryptoToolchain::Tools::RSAParityOracleAttack

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oracle:, n:, e: 3) ⇒ RSAParityOracleAttack

Returns a new instance of RSAParityOracleAttack.



4
5
6
7
8
# File 'lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb', line 4

def initialize(oracle: , n: , e: 3)
  @oracle = oracle
  @n = n
  @e = e
end

Instance Attribute Details

#eObject (readonly)

Returns the value of attribute e.



9
10
11
# File 'lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb', line 9

def e
  @e
end

#nObject (readonly)

Returns the value of attribute n.



9
10
11
# File 'lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb', line 9

def n
  @n
end

#oracleObject (readonly)

Returns the value of attribute oracle.



9
10
11
# File 'lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb', line 9

def oracle
  @oracle
end

Instance Method Details

#execute(_ciphertext, output: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/crypto_toolchain/tools/rsa_parity_oracle_attack.rb', line 11

def execute(_ciphertext, output: false)
  ciphertext = _ciphertext.to_number
  min = BigDecimal(0)
  max = BigDecimal(n)
  mid = max/2
  mult = 2.modpow(e, n)
  Math.log2(n).ceil.times do
    mid = (min + max) / 2
    ciphertext = ((ciphertext) * mult) % n
    if oracle.execute(ciphertext.to_bin_string) == 0
      max = mid
    else
      min = mid
    end
    if output
      print "\e[2J\e[f\r#{max.to_i.to_bin_string.gsub(/[^[:print:]]/, '*')}"
    end
  end
  max.to_i.to_bin_string
end