Advertisement
Narzew

Completed File Crypt Program

Mar 23rd, 2013
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.32 KB | None | 0 0
  1. # Code from this video : https://www.youtube.com/watch?v=RqbtV4cURI0
  2. module CR
  3.     def self.encrypt(x,key)
  4.         result = ""
  5.         srand(((954938*key)%0xFFFFFF)+2)
  6.         x.each_byte{|y|
  7.             result << ((y+rand(444)+3)%256).chr
  8.         }
  9.         return result
  10.     end
  11.     def self.decrypt(x,key)
  12.         result = ""
  13.         srand(((954938*key)%0xFFFFFF)+2)
  14.         x.each_byte{|y|
  15.             result << ((y-rand(444)-3)%256).chr
  16.         }
  17.         return result
  18.     end
  19.     def self.encrypt_file(x,y,k)
  20.         file = File.open(x,'rb')
  21.         data = file.read
  22.         file.close
  23.         file = File.open(y,'wb')
  24.         file.write(CR.encrypt(data,k))
  25.         file.close
  26.     end
  27.     def self.decrypt_file(x,y,k)
  28.         file = File.open(x,'rb')
  29.         data = file.read
  30.         file.close
  31.         file = File.open(y,'wb')
  32.         file.write(CR.decrypt(data,k))
  33.         file.close
  34.     end
  35. end
  36.  
  37. begin
  38. print "Podaj metode :\n0-szyfruj\n1-deszyfruj\n"
  39. mode = gets.chomp!.to_i
  40. print "Podaj plik do szyfrowania :\n"
  41. filename = gets.chomp.to_s
  42. print "Podaj plik docelowy :\n"
  43. dest = gets.chomp.to_s
  44. print "Podaj klucz :\n"
  45. key = gets.chomp.to_i
  46. case mode
  47.     when 0 then CR.encrypt_file(filename,dest,key);print "\nSzyfrowanie pomyslne.";$stdin.gets
  48.     when 1 then CR.decrypt_file(filename,dest,key);print "\nDeszyfrowanie pomyslne.";$stdin.gets
  49.     else
  50.         print "Zly tryb"
  51. end
  52. rescue => e
  53.     print "Wystapil blad : #{e}\n"
  54.     print "Nacisnij ENTER by wyjsc\n"
  55.     $stdin.gets
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement