fazlamesai de çıkan Proje Euler yarışması dikkatimi çekmişti. Sırf meraktan soruları şöyle bir okudum. XOR lu kriptolu bir soru çok ilgimi çekti (ah merak ah..). Acaba bu metin de ne yazıyor diye aklıma bir kurt düştü.
Ben de çalakalem bir ruby kodu yazayım dedim. Çok da bildiğimden değil hani. sadece öğrenmiş olayım diye
Ortaya şöyle bir kod çıktı:
class Key
attr_reader :key
def nextByte
@base = (@base + 1) % 3
@key[@base]
end
def initialize(key)
@base = -1
@key = key
end
end ctextfile = File.open("cipher1.txt")
ctext = ctextfile.gets.split(",").collect { |b| b.to_i.chr }.join
cleartext = Array.new keylist = Array.new
('a'..'z').each { |v1|
('a'..'z').each { |v2|
('a'..'z').each { |v3|
keylist.push Key.new("#{v1}#{v2}#{v3}")
}
}
} keylist.each { |key|
t = ""
puts "Crypting for key: #{key.key}"
ctext.each_byte { |byte|
b = byte ^ key.nextByte
t < < b.chr
}
cleartext.push t
} f = File.open("decipher.txt","w")
cleartext.each { |t|
f.puts t
}
Tabii dikkatle kodu incelemiş olan sizlerde farkettiniz ki metni bulan bir kısım yok. o işi grep e bıraktım
grep the decipher.txt | grep and | grep or | grep of | grep is | grep was
Ben merakımı giderdim. İsteyen de kendi merakını gidersin..
1. Yorum , Yavuz
9/Ağu/2006 , 7:30 pm
Hm, bir de böyle deneyelim
class Key
attr_reader :key
def nextByte
@base = (@base + 1) % 3
@key[@base]
end
def initialize(key)
@base = -1
@key = key
end
end
ctextfile = File.open(“cipher1.txt”)
ctext = ctextfile.gets.split(“,”).collect { |b| b.to_i.chr }.join
cleartext = Array.new keylist = Array.new
(‘a’..’z').each { |v1|
(‘a’..’z').each { |v2|
(‘a’..’z').each { |v3|
keylist.push Key.new(“#{v1}#{v2}#{v3}”)
}
}
}
keylist.each { |key|
t = “”
puts “Crypting for key: #{key.key}”
ctext.each_byte { |byte|
b = byte ^ key.nextByte
t
2. Yorum , Yavuz
9/Ağu/2006 , 7:33 pm
OK pre tag çalışmadı, zaten html için anlamlı karakterleri quote etmeyi de unutmuşum. Kirlilik için pardon.