class EncryptV
Archivo encrypt_v/lib/encryptvigen/encryptv.rb
Public Instance Methods
decrypt(encrypted_msg, key)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 11 def decrypt(encrypted_msg, key) # Metodo para desencriptar un mensaje new_key = extend_key(encrypted_msg, key) algorithm_decrypt_v(encrypted_msg,new_key) end
encrypt(msg, key)
click to toggle source
Metodos publicos
# File lib/encryptvigen/encryptv.rb, line 6 def encrypt(msg, key) # Metodo para encriptar un mensaje algorithm_encrypt_v(msg,key) end
Protected Instance Methods
algorithm_decrypt_v(encrypted_msg, new_key)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 80 def algorithm_decrypt_v(encrypted_msg, new_key) msg_len = encrypted_msg.length decrypted_msg = gen_ch_array(msg_len) #Desencriptacion available_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ".split("") encrypted_msg.split("").each_with_index{ |c,i| if validate(c) || (c.eql? ' ') decrypted_msg[i] = available_chars[(((index(encrypted_msg.split("")[i]) - index(new_key.split("")[i])) + available_chars.length()) % available_chars.length())] else decrypted_msg[i] = decrypted_msg.split("")[i] end } return decrypted_msg.join(""); end
algorithm_encrypt_v(msg, key)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 59 def algorithm_encrypt_v(msg, key) # Encrypta un mensaje utilizando el algoritmo vigenere msg_len = msg.length key_len = key.length encrypted_msg = gen_ch_array(msg_len) new_key = extend_key(msg, key) # Encriptacion available_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ".split("") msg.split("").each_with_index{ |c,i| if validate(c) || (c.eql? ' ') encrypted_msg[i] = available_chars[((index(msg.split("")[i]) + index(new_key.split("")[i])) % available_chars.length())] else encrypted_msg[i] = msg.split("")[i] end } return encrypted_msg.join("") end
extend_key(msg, key)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 37 def extend_key(msg, key) # Generar une nueva clave a base del mensage y la clave original msg_len = msg.length new_key = gen_ch_array(msg_len) key_len = key.length j = 0 msg.split("").each_with_index{ |c,i| if j.eql? key_len j = 0 end new_key[i] = key.split("")[j] j = j + 1 } return new_key.join("") end
gen_ch_array(len)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 32 def gen_ch_array(len) # Devuelve un array de tamaƱo 'len' de caracteres 'x' ('x'*len).split("") end
index(ch)
click to toggle source
Metodos privados
# File lib/encryptvigen/encryptv.rb, line 21 def index(ch) # Devuelve la posicion del caracter en el hash available_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ".split("") available_chars.each_with_index { |c,i| if c.eql? ch return i end } return -1 end
validate(str)
click to toggle source
# File lib/encryptvigen/encryptv.rb, line 53 def validate(str) # Devuelve verdadero si el str es alfa-numerico chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a str.chars.detect {|ch| !chars.include?(ch)}.nil? end