Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module MsgCrypt
- def self.encode(msg)
- print "\n\n"
- $result = ""
- msg.each_byte{|x|
- $i = x * 3 + 18
- print "#{x.chr} => #{$i}\n"
- $result << "#{$i},"
- }
- return $result
- end
- def self.decode(msgtable)
- print "\n\n"
- $result = ""
- msgtable.each{|x|
- $i = (x -18) / 3
- print "#{x} => #{$i.chr}\n"
- $result << "#{$i.chr}"
- }
- return $result
- end
- def self.save_to_file(x)
- file = File.open('result.txt', 'wb')
- file.write(x)
- file.close
- end
- def self.read_key
- print "=============================================================\n"
- print "NARZEW MESSAGE ENCRYPTER/DECRYPTER v 1.0\n"
- print "Copyright 2012 Narzew\n"
- print "=============================================================\n"
- print "If you decrypt a message, add bytes to $code array :)\n"
- print "=============================================================\n"
- print "Type the running mode.\n"
- print "0 - encrypt\n"
- print "1 - decrypt\n"
- print "=============================================================\n"
- mode = gets.chomp
- case mode.to_i
- when 0 then $mode = 0
- when 1 then $mode = 1
- else
- print "=============================================================\n"
- print "Invalid Mode. Press ENTER to EXIT.\n"
- print "=============================================================\n"
- $stdin.gets
- exit
- end
- print "=============================================================\n"
- print "Type the file with the message to encrypt.\n" if $mode == 0
- print "Type the file with the message to decrypt.\n" if $mode == 1
- print "=============================================================\n"
- $filename = gets.chomp!
- if File.exist?($filename)
- file = File.open($filename, 'rb')
- $a = file.read
- file.close
- else
- print "=============================================================\n"
- print "File do not exist.\n"
- print "Press ENTER to close.\n"
- print "=============================================================\n"
- $stdin.gets
- exit
- end
- if $mode == 0
- $msg = $a
- MsgCrypt.save_to_file("$code=[" + MsgCrypt.encode($msg) + "]")
- elsif $mode == 1
- eval($a)
- MsgCrypt.save_to_file(MsgCrypt.decode($code))
- end
- rescue
- print "=============================================================\n"
- print "Fail to encrypt or decrypt the data.\n"
- print "PRESS ENTER TO EXIT.\n"
- print "=============================================================\n"
- $stdin.gets
- exit
- end
- def self.make_footer
- print "=============================================================\n"
- print "MESSAGE ENCRYPTED.\nALSO CHECK result.txt ;)\n" if $mode == 0
- print "MESSAGE DECRYPTED.\nALSO CHECK result.txt ;)\n" if $mode == 1
- print "Press ENTER to exit.\n"
- print "=============================================================\n"
- $stdin.gets
- end
- def self.do
- MsgCrypt.read_key
- MsgCrypt.make_footer
- end
- end
- begin
- MsgCrypt.do
- rescue => e
- print "\n=============================================================\n"
- print "Fatal error : #{e}\n"
- print "Press ENTER to exit.\n"
- print "=============================================================\n"
- $stdin.gets
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement