Advertisement
Narzew

Message Crypt Example

Nov 1st, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.06 KB | None | 0 0
  1. module MsgCrypt
  2.  
  3. def self.encode(msg)
  4. print "\n\n"
  5. $result = ""
  6. msg.each_byte{|x|
  7. $i = x * 3 + 18
  8. print "#{x.chr} => #{$i}\n"
  9. $result << "#{$i},"
  10. }
  11. return $result
  12. end
  13. def self.decode(msgtable)
  14. print "\n\n"
  15. $result = ""
  16. msgtable.each{|x|
  17. $i = (x -18) / 3
  18. print "#{x} => #{$i.chr}\n"
  19. $result << "#{$i.chr}"
  20. }
  21. return $result
  22. end
  23.  
  24. def self.save_to_file(x)
  25.  file = File.open('result.txt', 'wb')
  26.  file.write(x)
  27.  file.close
  28. end
  29.  
  30. def self.read_key
  31. print "=============================================================\n"
  32. print "NARZEW MESSAGE ENCRYPTER/DECRYPTER v 1.0\n"
  33. print "Copyright 2012 Narzew\n"
  34. print "=============================================================\n"
  35. print "If you decrypt a message, add bytes to $code array :)\n"
  36. print "=============================================================\n"
  37. print "Type the running mode.\n"
  38. print "0 - encrypt\n"
  39. print "1 - decrypt\n"
  40. print "=============================================================\n"
  41. mode = gets.chomp
  42. case mode.to_i
  43. when 0 then $mode = 0
  44. when 1 then $mode = 1
  45. else
  46. print "=============================================================\n"
  47. print "Invalid Mode. Press ENTER to EXIT.\n"
  48. print "=============================================================\n"
  49. $stdin.gets
  50. exit
  51. end
  52. print "=============================================================\n"
  53. print "Type the file with the message to encrypt.\n" if $mode == 0
  54. print "Type the file with the message to decrypt.\n" if $mode == 1
  55. print "=============================================================\n"
  56.  
  57. $filename = gets.chomp!
  58. if File.exist?($filename)
  59. file = File.open($filename, 'rb')
  60. $a = file.read
  61. file.close
  62. else
  63. print "=============================================================\n"
  64. print "File do not exist.\n"
  65. print "Press ENTER to close.\n"
  66. print "=============================================================\n"
  67. $stdin.gets
  68. exit
  69. end
  70.  
  71. if $mode == 0
  72. $msg = $a
  73. MsgCrypt.save_to_file("$code=[" + MsgCrypt.encode($msg) + "]")
  74. elsif $mode == 1
  75. eval($a)
  76. MsgCrypt.save_to_file(MsgCrypt.decode($code))
  77. end
  78.  
  79. rescue
  80.  
  81. print "=============================================================\n"
  82. print "Fail to encrypt or decrypt the data.\n"
  83. print "PRESS ENTER TO EXIT.\n"
  84. print "=============================================================\n"
  85. $stdin.gets
  86. exit
  87.  
  88. end
  89.  
  90. def self.make_footer
  91. print "=============================================================\n"
  92.  print "MESSAGE ENCRYPTED.\nALSO CHECK result.txt ;)\n" if $mode == 0
  93.  print "MESSAGE DECRYPTED.\nALSO CHECK result.txt ;)\n" if $mode == 1
  94.  print "Press ENTER to exit.\n"
  95. print "=============================================================\n"
  96.  $stdin.gets
  97. end
  98.  
  99. def self.do
  100.  MsgCrypt.read_key
  101.  MsgCrypt.make_footer                                          
  102. end
  103.  
  104. end
  105.  
  106. begin
  107.  
  108. MsgCrypt.do
  109.  
  110. rescue => e
  111.  
  112. print "\n=============================================================\n"
  113. print "Fatal error : #{e}\n"
  114. print "Press ENTER to exit.\n"
  115. print "=============================================================\n"
  116. $stdin.gets
  117.  
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement