Created
December 21, 2011 00:56
-
-
Save yugi386/1504033 to your computer and use it in GitHub Desktop.
Criptografia Simples e Eficiente em Ruby - Simple and Efficient Encryption in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
INICIANDO O APLICATIVO E GERENCIANDO A CHAVE... | |
=end | |
require 'ftools' # acrescenta Metodos para mover, copiar, apagar e comparar arquivos | |
# FUNCOES: | |
def form10(palavra) | |
while TRUE | |
if palavra.size != 10 | |
palavra = "0" + palavra | |
else | |
break | |
end | |
end | |
return palavra | |
end | |
def hora() | |
# obtém a data e hora atual usando o método new | |
agora = Time.new | |
# exibe o resultado | |
puts agora.strftime("%d/%m/%Y - %H:%M:%S") | |
end | |
# INICIO DA ROTINA PRINCIPAL.... | |
puts "CRIPFIX\n" | |
puts "Criptografando arquivos com o algoritmo Cripfix em Ruby" | |
# VERIFICANDO A EXISTENCIA DO ARQUIVO: | |
puts "Digite o nome do arquivo: " | |
nomearq = gets | |
tam = nomearq.size | |
nomearq = nomearq[0,tam-1] | |
if File.exists?(nomearq) | |
puts "O arquivo existe - OK" | |
else | |
puts "O arquivo não existe" | |
exit | |
end | |
puts "Criptografando o arquivo: " + nomearq | |
puts "A CHAVE DE CIFRAGEM É COMPOSTA POR 4 NÚMEROS DE 32 BITS - 0 ATÉ 4294967295" | |
# LENDO CHAVE: | |
numero = Array.new | |
for cont in (0..3) | |
puts "Digite Número " + cont.to_s + ": " | |
numero[cont] = gets | |
end | |
acumula = "" | |
for cont in (0..3) | |
tam = numero[cont].to_s.size | |
if tam != 1 | |
if cont != 3 | |
acumula = acumula + numero[cont].to_s[0,numero[cont].to_s.size-1] + " | " | |
else | |
acumula = acumula + numero[cont].to_s[0,numero[cont].to_s.size-1] | |
end | |
else | |
if cont != 3 | |
acumula = acumula + numero[cont].to_s + " | " | |
else | |
acumula = acumula + numero[cont].to_s | |
end | |
end | |
end | |
puts "Chave.: " + acumula # Apresenta chave | |
# VALIDANDO CHAVE: | |
for cont in (0..3) | |
if numero[cont].to_i < 0 || numero[cont].to_i > (2**32)-1 # operador OU = || E = && | |
puts "Chave com valores inválidos" | |
exit | |
end | |
end | |
# GERANDO CHAVE FORMAL... | |
na = numero[0].to_i / 65536 | |
nb = numero[0].to_i % 65536 | |
nc = numero[1].to_i / 65536 | |
nd = numero[1].to_i % 65536 | |
ne = numero[2].to_i / 65536 | |
nf = numero[2].to_i % 65536 | |
ng = numero[3].to_i / 65536 | |
nh = numero[3].to_i % 65536 | |
# CRIANDO ARQUIVO DE DESTINO: | |
arq = File.new(nomearq, "r") # Abre origem | |
dest = File.new("temp.tmp","w+") # Cria destino | |
tamanho = File.size(nomearq) | |
puts tamanho | |
posic = 0 | |
arqfim = 0 # controla fim de rotina | |
hora() | |
while TRUE | |
# puts posic | |
tmp1 = (na * na).to_s.chomp | |
tmp1 = form10(tmp1) | |
tmp2 = (nb * nb).to_s.chomp | |
tmp2 = form10(tmp2) | |
xtemp = tmp1 + tmp2 | |
na = (na + xtemp[5,5].to_i + nc) % 65536 | |
nb = (nb + xtemp[10,5].to_i + nc) % 65536 | |
nc = (nc + nb) % 65536 | |
nd = (nd + na) % 65536 | |
xc1 = na % 256; | |
xc2 = na / 256; | |
xc3 = nb % 256; | |
xc4 = nb / 256; | |
valores = [xc1, xc2, xc3, xc4] | |
for cont in (0..3) | |
ler = arq.getc # Le Byte de origem | |
# puts ler | |
grava = ler.ord ^ valores[cont] | |
# puts grava | |
dest.write(grava.chr) | |
posic = posic + 1 | |
if posic == tamanho | |
arqfim = 1; | |
break | |
end | |
end | |
if arqfim == 1 | |
break | |
end | |
# 2ª PARTE DO CÓDIGO: | |
tmp1 = (ne * ne).to_s.chomp | |
tmp1 = form10(tmp1) | |
tmp2 = (nf * nf).to_s.chomp | |
tmp2 = form10(tmp2) | |
xtemp = tmp1 + tmp2 | |
ne = (ne + xtemp[5,5].to_i + ng) % 65536 | |
nf = (nf + xtemp[10,5].to_i + nh) % 65536 | |
ng = (ng + nf) % 65536 | |
nh = (nh + ne) % 65536 | |
xc1 = ne % 256; | |
xc2 = ne / 256; | |
xc3 = nf % 256; | |
xc4 = nf / 256; | |
valores = [xc1, xc2, xc3, xc4] | |
for cont in (0..3) | |
ler = arq.getc # Le Byte de origem | |
# puts ler | |
grava = ler.ord ^ valores[cont] | |
# puts grava | |
dest.write(grava.chr) | |
posic = posic + 1 | |
if posic == tamanho | |
arqfim = 1; | |
break | |
end | |
end | |
if arqfim == 1 | |
break | |
end | |
# Operação final: | |
aux1 = nc | |
aux2 = nd | |
nc = ng + 1 | |
nd = nh + 3 | |
ng = aux1 + 5 | |
nh = aux2 + 7 | |
end | |
hora() | |
arq.close | |
dest.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment