Class: EncryptDecrypt::EncryptedClient
- Inherits:
- 
      Object
      
        - Object
- EncryptDecrypt::EncryptedClient
 
- Defined in:
- lib/accu-encrypt.rb
Overview
A class which allows for the creation of an encrypted session over a server, with the client acting as the client.
Codes: 0 - Connection refused, 1 - Connection accepted, 2 - Ready, 3 - Sending encrypted message, 4 - End communications, 5 - Verification message inbound
Instance Method Summary collapse
- 
  
    
      #initialize(server)  ⇒ EncryptedClient 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Initializes EncryptedServer object. 
- 
  
    
      #interactive  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Opens an interactive session. 
- 
  
    
      #listen {|msg| ... } ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Listens for a generic network communication and yields to a passed block (if any). 
- 
  
    
      #listen_code  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Listens for a network code. 
- 
  
    
      #listen_encrypted  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Listens for encrypted messages. 
- 
  
    
      #listen_lines  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Listens for multiple lines, appends them together and stops at END. 
- 
  
    
      #send(text)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sends a plaintext message. 
- 
  
    
      #send_code(code)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sends a code. 
- 
  
    
      #send_encrypted(lines)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Send over an encrypted multiline message. 
- 
  
    
      #send_lines(lines)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sends multiple lines and appends END to end the send. 
Constructor Details
#initialize(server) ⇒ EncryptedClient
Initializes EncryptedServer object.
| 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | # File 'lib/accu-encrypt.rb', line 201 def initialize( server ) @server = server @listen_thread, @interactive_thread = nil, nil @mode = EncryptDecrypt.get_mode if listen_code() == 1 then @password = EncryptDecrypt.get_password "Please enter password for communications: " send_code(2) if listen_code() == 2 then puts "Ready code received.\nSuccessfully connected!" listen_encrypted() interactive() @listen_thread.join @interactive_thread.join puts "Client exiting." else puts "Server unready for communications." end else puts "Connection refused." end end | 
Instance Method Details
#interactive ⇒ Object
Opens an interactive session.
| 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | # File 'lib/accu-encrypt.rb', line 304 def interactive() @interactive_thread = Thread.new { loop { print "What would you like to do? |S Q| " response = gets.chomp.downcase while response != "q" and response != "s" do puts "Incorrect input: |" + response + "|" print "What would you like to do? |S Q| " response = gets.chomp.downcase end if response == "q" then puts "Sending close message to server." send_code(4) @listen_thread.exit @interactive_thread.exit else print "Type message and end with END:\n\n" lines = "" while line = gets and line.chomp != "END" do #puts "|" + line + "|" lines << line end lines = lines.chomp puts "Message read.\nEncrypting now." send_encrypted(lines) puts "Message sent." end } } end | 
#listen {|msg| ... } ⇒ Object
Listens for a generic network communication and yields to a passed block (if any).
| 246 247 248 249 | # File 'lib/accu-encrypt.rb', line 246 def listen() msg = @server.gets.chomp yield msg end | 
#listen_code ⇒ Object
Listens for a network code.
| 263 264 265 266 267 268 269 270 271 272 273 | # File 'lib/accu-encrypt.rb', line 263 def listen_code() listen { |msg| if msg.slice(0..1) == "C:" then #puts "Code present." return msg.slice(2..msg.length).to_i else #puts "Code not present." return msg.slice(0..1) end } end | 
#listen_encrypted ⇒ Object
Listens for encrypted messages.
| 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | # File 'lib/accu-encrypt.rb', line 224 def listen_encrypted() @listen_thread = Thread.new { loop { code = listen_code() if code == 3 then lines = "" while line = @server.gets and line != "END\n" do lines << line end secret,text = EncryptDecrypt.decrypt(@password,lines,@mode) puts "Message received:\n\n" + text + "\n\nMessage end." elsif code == 4 then puts "Server closed connection." @interactive_thread.exit @listen_thread.exit end } } end | 
#listen_lines ⇒ Object
Listens for multiple lines, appends them together and stops at END.
| 253 254 255 256 257 258 259 260 | # File 'lib/accu-encrypt.rb', line 253 def listen_lines() lines = [] while line = @server.gets.chomp and line != "END" do #puts "|" + line + "|" lines << line end lines end | 
#send(text) ⇒ Object
Sends a plaintext message.
| 276 277 278 | # File 'lib/accu-encrypt.rb', line 276 def send(text) @server.puts text end | 
#send_code(code) ⇒ Object
Sends a code.
| 299 300 301 | # File 'lib/accu-encrypt.rb', line 299 def send_code(code) send("C:#{code}") end | 
#send_encrypted(lines) ⇒ Object
Send over an encrypted multiline message.
| 291 292 293 294 295 296 | # File 'lib/accu-encrypt.rb', line 291 def send_encrypted(lines) secret,text = EncryptDecrypt.encrypt(@password,lines,@mode) send_code(3) send(text) send("END") end | 
#send_lines(lines) ⇒ Object
Sends multiple lines and appends END to end the send.
| 282 283 284 285 286 287 288 | # File 'lib/accu-encrypt.rb', line 282 def send_lines(lines) array = lines.split("\n") array.each do |line| send(line) end send("END") end |