Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.96 KB | None | 0 0
  1. require 'socket'
  2. require 'openssl'
  3. require 'rubygems'
  4. require 'net/ssh'
  5.  
  6. #ssh/encryption config
  7. HOST = '192.168.1.113'
  8. USER = 'username'
  9. PASS = 'password'
  10. KEY = "qwertyuiopasdfghjklzxcvbnmqwerty"
  11. IV = "qwertyuiopasdfghjklzxcvbnmqwerty"
  12. PREAMBLE="begin_encryption"
  13.  
  14. class Encryption
  15.     def initialize (string)
  16.         @string = string
  17.     end
  18.     #encrypts
  19.     def encrypt_payload
  20.         cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  21.         cipher.encrypt
  22.         cipher.key = ::KEY
  23.         cipher.iv = ::IV
  24.         cipher.update(@string) + cipher.final
  25.        
  26.     end
  27.     #decrypts
  28.     def decrypt_payload
  29.         cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
  30.         cipher.decrypt
  31.         cipher.key = ::KEY
  32.         cipher.iv = ::IV
  33.         cipher.update(@string) + cipher.final
  34.     end
  35.    
  36. end
  37.  
  38.  
  39. #this is a socket client to tx/rx streamimg aes-256 encrypted data.
  40. class Socket_IO_crypt
  41.   def initialize(data, port, host)
  42.   @io_obj = data
  43.   @port = port
  44.   @host = host
  45.   end
  46.  
  47.     def server
  48.     server = TCPServer.open( @host , @port)
  49.     loop {
  50.         rx = server.accept    
  51.       #scans for a preamble and decrypts string sending it to a handler
  52.         encrypted_str = rx.to_s.gsub(::PREAMBLE , "")
  53.         p rx.to_s + "reciever"
  54.         p encrypted_str + "encrypted"
  55.         decrypted_str = Encryption.new(encrypted_str)
  56.         print decrypted_str + "decrypted"
  57.         rx.close
  58.     }
  59.   end
  60. #listens for incomming connections on @port sending a encrypted string with a preamble  
  61. #shoots an encrypted message to @host atc @port with preamble
  62.   def client
  63.  
  64.       tx = TCPSocket.open(@host, @port)
  65.       data = Encryption.new(@io_obj)
  66.       crypt_data = data.encrypt_payload
  67.       p ::PREAMBLE + crypt_data
  68.       tx.send ::PREAMBLE + crypt_data      
  69.       tx.close
  70.   end
  71. end
  72. asdf = Socket_IO_crypt.new(" Hello World" , "5554" , "127.0.0.1")
  73. asdf.client
  74.  
  75. user@Firefly:~/Desktop$ sudo ruby crypt_sock.rb
  76. #<TCPSocket:0xb704e810>
  77. "#<TCPSocket:0xb704e810>"
  78. #<Encryption:0xb704e734>#<TCPSocket:0xb704e770>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement