# #-- # Ronin - A Ruby platform designed for information security and data # exploration tasks. # # Copyright (c) 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #++ # require 'ronin/translators/translator' require 'ronin/chars' module Ronin module Translators class XOR < Translator # Set of characters to allow in the encoded data attr_accessor :allow # # Creates a new XOR Translator object using the given _options_. # If a _block_ is given it will be passed the newly created # Translator object. # # _options_ may include the following keys: # :allow:: The set of characters allowed in the encoded # result. Defaults to (1..255). # :disallow:: The set of characters that are not allowed # in the encoded result. # def initialize(options={},&block) @allow = Chars::CharSet.new(options[:allow] || (1..255)) if options[:disallow] @allow -= options[:disallow] end super(&block) end # # XOR encodes the specified _data_ prefixing the XOR key to the # encoded data. If a _block_ is given, it will be passed the encoded # data. # def encode(data,&block) alphabet = Chars.all.select { |b| data.include?(b.chr) } excluded = (Chars.all - alphabet) key = excluded.select { |b| @allow.include?(b) && alphabet.all? { |i| @allow.include?(i ^ b) } }.last text = '' text << key.chr data.each_byte { |b| text << (b ^ key).chr } block.call(text) if block return text end # # XOR decodes the specified _text_. If a _block_ is given, it will be # passed the decoded data. # def decode(text,&block) data = '' key = text[0] text[1..-1].each_byte do |b| data << (b ^ key).chr end return data end end end end