module SubstitutionCipher
Module containing the classes and methods of the library.
Constants
- ALPHA_RANGE
Public Class Methods
crack_offset(string)
click to toggle source
A method to crack (with the users help) a string that has been “encrypted” with SubstitutionCipher#encrypt.
# File lib/accu-cipher.rb, line 90 def self.crack_offset(string) first = Time.now range = 1..25 complete = false range.to_a.each do |value| puts "\n-------------\n\n" puts self.decrypt(string,value) puts "\n-------------\n\n" puts "Valid? |Y N|" result = gets.chomp.downcase if result == "y" then complete = value break end end puts "\n-------------\n\n" if not complete then puts "Crack failed." else puts "Offset: #{complete}\nTime taken: #{Time.now-first}" end return complete end
decrypt(string,offset=3)
click to toggle source
“Decrypt” a string via a substitution cipher.
# File lib/accu-cipher.rb, line 70 def self.decrypt(string,offset=3) string = self.purge_nonalpha string final = "" string.split("").each_with_index do |source,index| if (not source.ord == 10 and not source.ord == 32) then result = source.ord - offset if not SubstitutionCipher::ALPHA_RANGE.include? source.ord then result += 25 end else result = source.ord end final << result.chr end return final end
encrypt(string,offset=3)
click to toggle source
“Encrypt” a string via a substitution cipher.
# File lib/accu-cipher.rb, line 51 def self.encrypt(string,offset=3) string = self.purge_nonalpha string final = "" string.split("").each_with_index do |source,index| if (not source.ord == 10 and not source.ord == 32) then result = source.ord + offset if not SubstitutionCipher::ALPHA_RANGE.include? source.ord then result -= 25 end else result = source.ord end final << result.chr end return final end
purge_nonalpha(text)
click to toggle source
Remove non-alphabetical letters from plaintext (bar spaces and newlines)
# File lib/accu-cipher.rb, line 35 def self.purge_nonalpha(text) text = text.dup.downcase table = [] text.split("").each_with_index do |source,index| if not SubstitutionCipher::ALPHA_RANGE.include? source.ord and (not source.ord == 10 and not source.ord == 32) then table << index end end table.reverse.each do |index| text[index] = "" end return text end