Snippets

Chihiro Hasegawa [TokyoWesternsCTF2016] Backpacker's cihper (easy mode) [Crypto:200pts]

Created by Chihiro Hasegawa
require 'json'
require 'openssl'
require '/home/vagrant/ctf/tools/ctfutils/crypto.rb' #=> modinv

pubkey = JSON.parse(File.read('pubkey'))

bn_1 = pubkey[1022]
bn = pubkey[1023]

p = 2 * bn_1 - bn
q = (bn * modinv(2**1023, p)) % p

ctext = File.read('encrypted').to_i
des_ctext = (ctext * modinv(q, p)) % p

x = ""
pubkey.size.times do |i|
  unless (des_ctext % (2**(i + 1))) == 0
    x += "1"
    des_ctext -= (pubkey[i] * modinv(q, p)) % p
  else
    x += "0"
  end
end

des = OpenSSL::Cipher.new('des-ecb').decrypt
des.key = 'testpass'
puts des.update([x].pack("B*")).split("\n")[0] #=> Flag:TWCTF{ESC4P3FR0MPR1SON}

Comments (0)