class CipherworldAmitjuly2020::Cipher::LetterNumber
Public Class Methods
new(char_set: ,key: )
click to toggle source
# File lib/cipherworld_amitjuly2020.rb, line 9 def initialize(char_set: ,key: ) @char_set = char_set @key = key end
Public Instance Methods
decrypt(encryptedtext)
click to toggle source
# File lib/cipherworld_amitjuly2020.rb, line 34 def decrypt(encryptedtext) decrypted = encryptedtext.scan(/../).map{|x| n = ((x.to_i)-@key)%99 decryptdict.fetch(n.to_s)}.join end
encrypt(string)
click to toggle source
# File lib/cipherworld_amitjuly2020.rb, line 14 def encrypt(string) begin # fetching letter and encrypting it ciphered = string.chars.map{|x| n = dictionary.fetch(x) + @key} rescue KeyError => e puts "Unsuitable text for cipher: #{e}" end # counter reset to 99 ciphered = ciphered.map{|n| if n > 99 and n % 99 == 0 n = 99 elsif n > 99 n %= 99 else n end} # double digit for each letter encryption ciphered.map{ |x| x < 10? "0" + x.to_s : x }.join end
Private Instance Methods
decryptdict()
click to toggle source
# File lib/cipherworld_amitjuly2020.rb, line 57 def decryptdict character_set = {} text_file.each.with_index{ |x,i| i < 9? character_set[x[3]] = x[0] : character_set[x[3..4]] = x[0] } character_set end
dictionary()
click to toggle source
creating dictionary from character set
# File lib/cipherworld_amitjuly2020.rb, line 51 def dictionary character_set = {} text_file.each{|x| character_set[x[0]] = (x[3..4].to_i)} character_set end
text_file()
click to toggle source
obtaining character set
# File lib/cipherworld_amitjuly2020.rb, line 43 def text_file list = [] File.open(@char_set).each{|x| list.push(x)} list.delete_at(0) list end