hakeの日記

Windows環境でプログラミングの勉強をしています。

Blowfishによる暗号化

BURPはBlowfishという暗号化アルゴリズムを使用しているということで、探したところありました、Ruby用が。
http://www.moodindigo.org/blog/archives/000320.html

こちらのblowfishpr.rbを使用すればRubyでBlowfishを使用した暗号化/復号化が可能になります。

追記

バイナリの暗号化をする場合に末尾に0x00があると復号時に削除されてしまいますね、この辺は何か処理が必要です。

require "F:/ruby_RDE/blowfishpr.rb"

def print_hex(str)
  str.each_byte do |x|
    printf "0x%0x ", x
  end
  puts
end

def test(str, key, mode)
  passwd = key
  text = str
  print "  Plain : "
  if mode == "s"
    puts text
  else
    print_hex(text)
  end
  
  bfenc = Blowfish.new(passwd)
  cipher = bfenc.encrypt(text)
  print "Encrypt : "
  print_hex(cipher)

  bfdec = Blowfish.new(passwd)
  s = bfdec.decrypt(cipher)
  print "Decrypt : "
  if mode == "s"
    puts s
  else
    print_hex(s)
  end
end

passwd = "passward"
text = "あいうえお"
test(text, passwd, "s")
text = "\x00\x01\x02\x03\x04\x05\xF0\xF1\xF2\xF3\xF4\xF5"
test(text, passwd, "x")

#=>   Plain : あいうえお
#=> Encrypt : 0xeb 0xa2 0xbb 0x97 0x98 0x71 0x32 0x40 0x15 0x4b 0xac 0xbb 0x1d 0x23 0x8c 0x68 
#=> Decrypt : あいうえお
#=>   Plain : 0x0 0x1 0x2 0x3 0x4 0x5 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5 
#=> Encrypt : 0x9e 0x1b 0x5c 0x2b 0x29 0x99 0x59 0xb8 0x14 0x36 0x71 0x28 0x91 0xd6 0x24 0x3 
#=> Decrypt : 0x0 0x1 0x2 0x3 0x4 0x5 0xf0 0xf1 0xf2 0xf3 0xf4 0xf5