Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # direction = 1
- # file = "plain.txt"
- # output = "output.txt"
- direction = -1
- file = "output.txt"
- output = "deciphered.txt"
- keys = []
- File.open("keys.txt","r") do |f|
- f.each_line do |l|
- keys << l.strip
- end
- end
- lines = []
- File.open(file,"r") do |f|
- cnt = 0
- f.each_line do |l|
- lines << l.strip
- end
- end
- sentences = []
- len = lines.length/2
- len.times do |i|
- sentences << {:text => lines[i*2],
- :keynum => lines[i*2+1].to_i,
- :key => keys[lines[i*2+1].to_i-1]}
- end
- def cipher(fwd,text,key)
- over = key.length
- keycnt = 0
- outstr = ""
- text.length.times do |i|
- char = text[i]+key[keycnt]*fwd
- # char = 0 if char < 0
- outstr << char.chr
- keycnt+=1
- keycnt = 0 if keycnt > over - 1
- end
- return outstr
- end
- sentences.each do |s|
- if s[:key]
- ciphered = cipher(direction,s[:text],s[:key])
- s[:ciphered] = ciphered
- else
- puts "Keys does not exist for #{s[:text]}, skipping"
- end
- end
- File.open(output,"w") do |f|
- sentences.each do |s|
- if s[:key]
- f.puts s[:ciphered]
- f.puts s[:keynum]
- end
- end
- end
Add Comment
Please, Sign In to add comment