パッチワークの問題の解き方があまりにも恥しかったので,自分への懲戒のため自分の回答を晒します。(Google Dev Fest 2010 の Quiz) まずは,暗号通信から。 楽に,手を抜くために慣れたRubyで実装しました。
シーザー暗号なんだなと思ったけど,tr コマンドの使い方を理解していなかったことが発覚。なので,地味に書きました。手を抜くために,gemのJSONライブラリ,JSON implementation for Rubyを利用しました。
#!/opt/local/bin/ruby
require "json"
require "net/http"
target_host = "devquiz.appspot.com"
target_file = "/personalpost"
key = "(snip)"
plain_text="my email address"
pass_text=""
for i in 0 .. plain_text.size - 1
ch = plain_text[i]
if (ch >= 'a'[0] && ch <= 'z'[0] ) # 小文字の変換
tmp = (ch - 'a'[0] + 4) % 26
pass_text += ('a'[0] + tmp).chr
elsif (ch >= 'A'[0] && ch <= 'Z'[0] ) # 大文字の変換
tmp = (ch - 'A'[0] + 4) % 26
pass_text += ('A'[0] + tmp).chr
else # その他の文字はそのまま通過
pass_text += plain_text[i].chr
end
end
json = {"key" => key, "pass" => pass_text}.to_json
#p json
Net::HTTP.start(target_host, 80 ) do |http|
# response = http.get('/')
response = http.post( target_file, json )
p response
end
tr を用いた別解
DevFestクイズ答案: 暗号通信
No comments:
Post a Comment