#	+-------------------+
#	|Ruby::Cipher v0.4.1|
#	|    Dirk Meijer    |
#	| 02/10/2005  20:59 |
#	|                   |
#	| Distributed under |
#	|  GNU GPL License  |
#	+-------------------+

version = "1.0.1"

class RubyCipher
	attr_reader :code, :code2
	ALPHA="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _"
	def initialize(code="RC",code2=24)
		@code=code
		@code2=code2
	end
	
	def RubyCipher.cipher(text,code="RC",code2=42)
		newtext=String.new
		0.upto(text.length-1) do |n|
			if text[n,1]=~/[a-zA-Z0-9 _]/
				newtext[n,1]=ALPHA[-((ALPHA=~/#{text[n,1]}/)+(ALPHA=~/#{code[n%(code.length),1]}/)+code2)%64,1]
			else
				newtext[n,1]=text[n,1]
			end
		end
		newtext
	end
	
	def cipher(text,code=@code,code2=@code2)
		newtext=String.new
		0.upto(text.length-1) do |n|
			if text[n,1]=~/[a-zA-Z0-9 _]/
				newtext[n,1]=ALPHA[-((ALPHA=~/#{text[n,1]}/)+(ALPHA=~/#{code[n%(code.length),1]}/)+code2)%64,1]
			else
				newtext[n,1]=text[n,1]
			end
		end
		newtext
	end
end

class String
	ALPHA="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _"
	def cipher(code="RC",code2=42)
		if code.class==String
			@code=code
			@code2=code2
		elsif code.class==RubyCipher
			@code=code.code
			@code2=code.code2
		end
		newtext=String.new
		0.upto(self.length-1) do |n|
			if self[n,1]=~/[a-zA-Z0-9 _]/
				newtext[n,1]=ALPHA[-((ALPHA=~/#{self[n,1]}/)+(ALPHA=~/#{@code[n%(@code.length),1]}/)+@code2)%64,1]
			else
				newtext[n,1]=self[n,1]
			end
		end
		newtext
	end
end

puts "Hello! welcome! please choose an option:
+--------------------+
|1 = Encrypt a string|
|2 = Decrypt a string|
|version = version   |
+--------------------+
Answer?:
"
answer = gets.chomp

case answer
when answer = "1"
  puts "Please enter the string you wish to Encrypt:"
  string = gets
  string = string[0,string.length-1]
  puts "Please enter the first code, also known as the passphrase:"
  code1 = gets
  code1.gsub!(/[^a-zA-Z0-9 _]/,"")
  puts "Please enter the secound code, also known as the number:"
  code2 = gets.to_i
  puts "The Encrypted string: " + RubyCipher.cipher(string, code1, code2)
when answer = "2"
  puts "Please enter the string you wish to Decrypt:"
  string = gets
  string = string[0,string.length-1]
  puts "Please enter the first code, also known as the passphrase:"
  code1 = gets
  code1.gsub!(/[^a-zA-Z0-9 _]/,"")
  puts "Please enter the secound code, also known as the number:"
  code2 = gets.to_i
  puts "The Decrypted string: " + RubyCipher.cipher(string, code1, code2)
when answer = "version"
  puts "We are currently at version " + version
end