class EncryptDecrypt::EncryptedClient

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

Public Class Methods

new( server ) click to toggle source

Initializes EncryptedServer object.

# 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

Public Instance Methods

interactive() click to toggle source

Opens an interactive session.

# 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| ... } click to toggle source

Listens for a generic network communication and yields to a passed block (if any).

# File lib/accu-encrypt.rb, line 246
def listen()
        msg = @server.gets.chomp
        yield msg
end
listen_code() click to toggle source

Listens for a network code.

# 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() click to toggle source

Listens for encrypted messages.

# 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() click to toggle source

Listens for multiple lines, appends them together and stops at END.

# 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) click to toggle source

Sends a plaintext message.

# File lib/accu-encrypt.rb, line 276
def send(text)
        @server.puts text
end
send_code(code) click to toggle source

Sends a code.

# File lib/accu-encrypt.rb, line 299
def send_code(code)
        send("C:#{code}")
end
send_encrypted(lines) click to toggle source

Send over an encrypted multiline message.

# 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) click to toggle source

Sends multiple lines and appends END to end the send.

# 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