#author linxu #decrypt the message of cipher.txt module rubytrain split_key = ":" class decoder def initialize(path) @path = path @code_table = hash.new get_code_table end attr_reader :path def get_code_table begin file = file.new(@path, "r") while (line = file.gets) value,key = line.strip().split(split_key) @code_table.store(key, value) end file.close rescue => err raise err end end def decode(encrypted_message) decrypted_message = string.new encrypted_message.each_char{|e| decrypted_message << @code_table.fetch(e,e)} return decrypted_message end end end def get_cipher_message(path) cipher_message = string.new begin file = file.new(path, "r") while (line = file.gets) cipher_message << line end file.close rescue => err raise err end return cipher_message end coder = rubytrain::decoder.new './code_table.txt' puts coder.decode 'b1eea v3pz!' puts coder.decode(get_cipher_message './cipher.txt')
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字

上传中....