module EncryptDecrypt

The primary module which packages the classes and global methods to be used for encryption.

Public Class Methods

decrypt(secret,plainText,*args) click to toggle source

Decrypts based on a continuing password offset algorithm.

# File lib/accu-encrypt.rb, line 367
def self.decrypt(secret,plainText,*args)
        currentKey = secret.clone
        output = ""
        plainText.each_byte { |plain|
                secondbyte = currentKey[0].ord
                out = plain - secondbyte
                while out < 0 do
                        out = out + 255
                end
                output[output.length] = out.chr
                currentKey[currentKey.length] = out.chr
                currentKey.slice! 0
        }
        if args[0] == :continuing then
                return secret,output,currentKey
        elsif args[0] == :double then
                return self.decrypt(secret,output)
        else
                return secret,output
        end
end
decrypt_file(filename,pass,*args) click to toggle source

Decrypts a file.

# File lib/accu-encrypt.rb, line 410
def self.decrypt_file(filename,pass,*args)
        if File.exists? filename then
                if args[0] != :double then
                        file = File.open filename
                        pass,output = *self.decrypt(pass,file.read)
                        file.close
                        File.delete filename
                        File.open(filename,"w") do |filer|
                                filer << output
                        end
                else
                        self.decrypt_file(filename,pass)
                        self.decrypt_file(filename,pass)
                end
        else
                puts "ERROR"
        end
end
encrypt(secret,source,*args) click to toggle source

Encrypts based on a continuing password offset algorithm.

# File lib/accu-encrypt.rb, line 343
def self.encrypt(secret,source,*args)
        currentKey = secret.clone
        output = ""
        source.each_byte { |orig|
                secondbyte = currentKey[0].ord
                out = orig + secondbyte
                while out > 255 do
                        out = out - 255
                end
                output[output.length] = out.chr
                currentKey[currentKey.length] = orig.chr
                currentKey.slice! 0
        }
        if args[0] == :continuing then
                return secret,output,currentKey
        elsif args[0] == :double then
                return self.encrypt(secret,output)
        else
                return secret,output
        end
end
encrypt_file(filename,pass,*args) click to toggle source

Encrypts a file.

# File lib/accu-encrypt.rb, line 390
def self.encrypt_file(filename,pass,*args)
        if File.exists? filename then
                if args[0] != :double then
                        file = File.open filename
                        pass,output = *self.encrypt(pass,file.read)
                        file.close
                        File.delete filename
                        File.open(filename,"w") do |filer|
                                filer << output
                        end
                else
                        self.encrypt_file(filename,pass)
                        self.encrypt_file(filename,pass)
                end
        else
                puts "ERROR"
        end
end
get_mode() click to toggle source

Returns current encryption mode.

# File lib/accu-encrypt.rb, line 811
def self.get_mode()
        @@mode.dup
end
get_password(prompt="Password: ") click to toggle source

Reads a password using the highline gem.

# File lib/accu-encrypt.rb, line 824
def self.get_password(prompt="Password: ")
        ask(prompt) {|q| q.echo = false}
end
interactive() click to toggle source

Chooses either interactive mode based on operating system.

# File lib/accu-encrypt.rb, line 801
def self.interactive()
        if defined? WindowTerminal and WindowTerminal.os == :linux then
                self.interactive_windows
        else
                self.interactive_terminal
        end
end
interactive_help() click to toggle source

Returns interactive help in a string.

# File lib/accu-encrypt.rb, line 337
def self.interactive_help()
        "---------\nThis program encrypts and decrypts files by offsetting the binary representation of the current character by the binary representation of the first character in an ongoing password.  The ongoing password is made up of the password appended to the beginning of the original plaintext.  Thus, the pattern is theoretically unpredictable as it is impossible to exactly where the original password starts and the obviously unpredictable plaintext encryption begins.  However, this algorithm's security is heavily correlated to the length and complexity of the opening password.  A complex but memorable password is recommended for secure encryption.\n---------\n"
end
interactive_terminal() click to toggle source

Opens an interactive terminal session.

# File lib/accu-encrypt.rb, line 430
def self.interactive_terminal()
        print "Welcome to interactive encode/decode.  "
        selection = ""
        @@mode = :normal
        verbose = true
        while selection != "q" do
                puts "What would you like to do? |E D I M N Q|"
                selection = gets.downcase
                selection.slice!(-1)
                if selection == "e" then
                        file = ""
                        ready = false
                        while not ready do
                                file = FileLibrary.select_file
                                if verbose then
                                        puts "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file.  Are you sure you would like to use this file? |Y N Q|"
                                        selection2 = gets.downcase
                                        selection2.slice!(-1)
                                        while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
                                                puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
                                                selection2 = gets.downcase
                                                selection2.slice!(-1)
                                        end
                                        if selection2 == "q" then
                                                ready = true
                                                file = ""
                                        elsif selection2 == "y" then
                                                ready = true
                                        end
                                else
                                        ready = true
                                end
                        end
                        if File.exists? file then
                                password = EncryptDecrypt.get_password
                                if verbose then
                                        puts "Finally, are you sure you would like to encrypt this file using this password? |Y N|"
                                        selection2 = gets.downcase
                                        selection2.slice!(-1)
                                        if selection2 == "y" then
                                                self.encrypt_file(file,password,@@mode)
                                                puts "Successfully encrypted file."
                                        end
                                else
                                        self.encrypt_file(file,password,@@mode)
                                        puts "Successfully encrypted file."
                                end
                        end
                elsif selection == "d" then
                        file = ""
                        ready = false
                        while not ready do
                                file = FileLibrary.select_file
                                if verbose then
                                        puts "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|"
                                        selection2 = gets.downcase
                                        selection2.slice!(-1)
                                        while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
                                                puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
                                                selection2 = gets.downcase
                                                selection2.slice!(-1)
                                        end
                                        if selection2 == "q" then
                                                ready = true
                                                file = ""
                                        elsif selection2 == "y" then
                                                ready = true
                                        end
                                else
                                        ready = true
                                end
                        end
                        if File.exists? file then
                                password = EncryptDecrypt.get_password
                                if verbose then
                                        puts "Finally, are you sure you would like to decrypt this file using this password? |Y N|"
                                        selection2 = gets.downcase
                                        selection2.slice!(-1)
                                        if selection2 == "y" then
                                                self.decrypt_file(file,password,@@mode)
                                                puts "Successfully decrypted file."
                                        end
                                else
                                        self.decrypt_file(file,password,@@mode)
                                        puts "Successfully decrypted file."
                                end
                        end
                elsif selection == "i" then
                        puts EncryptDecrypt.interactive_help
                elsif selection == "m" then
                        puts  "Select mode: Double - D Normal - N Verbose - V Unverbose - U"
                        selection2 = ""
                        selection2 = gets.downcase
                        selection2.slice!(-1)
                        while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do
                                puts "Invalid: |" + selection2 + "|"
                                selection2 = gets.downcase
                                selection2.slice!(-1)
                        end
                        if selection2 == "d" then
                                @@mode = :double
                        elsif selection2 == "v" then
                                verbose = true
                        elsif selection2 == "u" then
                                verbose = false
                        else
                                @@mode = :normal
                        end
                elsif selection == "n" then
                        puts "Port number: "
                        selection2 = ""
                        selection2 = gets.downcase
                        selection2.slice!(-1)
                        while (selection2.to_i == 0 or selection2.to_i == nil) do
                                puts "Invalid: |" + selection2 + "|"
                                selection2 = gets.downcase
                                selection2.slice!(-1)
                        end
                        port = selection2.to_i
                        puts "Would you like to open a client or a server? |C S|"
                        selection2 = ""
                        selection2 = gets.downcase
                        selection2.slice!(-1)
                        while (selection2 != "c" and selection2 != "s") do
                                puts "Invalid: |" + selection2 + "|"
                                selection2 = gets.downcase
                                selection2.slice!(-1)
                        end
                        if selection2 == "c" then
                                puts "IP: "
                                ip = ""
                                ip = gets.downcase
                                ip.slice!(-1)
                                begin
                                        server = TCPSocket.open( ip, port )
                                        EncryptedClient.new( server )
                                        server.close
                                rescue
                                        puts "ERROR\nConnection probably refused."
                                end
                        else
                                server = TCPServer.open port
                                EncryptedServer.new( server.accept )
                                server.close
                        end
                        puts "Returning to normal encryption tasks."
                elsif selection != "q" then
                        puts "Incorrect selection: |" + selection + "|"
                end
        end
end
interactive_windows() click to toggle source

Opens an interactive session with the WindowTerminal library.

# File lib/accu-encrypt.rb, line 600
def self.interactive_windows()
        raise "WindowTerminal library missing" if not defined? WindowTerminal
        manager = WindowTerminal::WindowManager.new
        text = self.windows(manager)
        selection = ""
        @@mode = :normal
        verbose = true
        while selection != "q" do
                text.set_text "What would you like to do? |E D I M N Q|"
                manager.show(1)
                manager.render()
                selection = WindowTerminal.getchr().downcase
                if selection == "e" then
                        file = ""
                        ready = false
                        while not ready do
                                file = FileLibrary.select_file_with_windows(manager)
                                manager.show
                                if verbose then
                                        text.set_text "---------\nWARNING!!!!!\n---------\nEncoding a file could un-retrievably destroy the contents of this file.  Are you sure you would like to use this file? |Y N Q|"
                                        manager.render()
                                        selection2 = WindowTerminal.getchr().downcase
                                        while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
                                                text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
                                                manager.render()
                                                selection2 = WindowTerminal.getchr().downcase
                                        end
                                        if selection2 == "q" then
                                                ready = true
                                                file = ""
                                        elsif selection2 == "y" then
                                                ready = true
                                        end
                                else
                                        ready = true
                                end
                        end
                        if File.exists? file then
                                text.set_text "Password: "
                                total = 0
                                manager.render()
                                password = WindowTerminal.getchrs{ |char,full|
                                        if char.ord != 127 then 
                                                text.set_text(text.text + "*")
                                                total += 1
                                        elsif total > 0 then
                                                text.set_text(text.text[0..(text.text.length-1)])
                                                total -= 1
                                        end
                                        manager.render()
                                }
                                
                                if verbose then
                                        text.set_text "Finally, are you sure you would like to encrypt this file using this password? |Y N|"
                                        manager.render()
                                        selection2 = WindowTerminal.getchr().downcase
                                        if selection2 == "y" then
                                                self.encrypt_file(file,password,@@mode)
                                                text.set_text "Successfully encrypted file."
                                                manager.render()
                                        end
                                else
                                        self.encrypt_file(file,password,@@mode)
                                        text.set_text "Successfully encrypted file."
                                        manager.render()
                                end
                                WindowTerminal.getchr()
                        end
                elsif selection == "d" then
                        file = ""
                        ready = false
                        while not ready do
                                file = FileLibrary.select_file_with_windows(manager)
                                manager.show
                                if verbose then
                                        text.set_text "---------\nWARNING!!!!!\n---------\nDecoding a file which has not been encrypted will destroy the contents.\nDecoding a file with the incorrect password will likely also destroy the file.\n---------\nAre you sure you would like to use this file? |Y N Q|"
                                        manager.render()
                                        selection2 = WindowTerminal.getchr().downcase
                                        while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
                                                text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
                                                manager.render()
                                                selection2 = WindowTerminal.getchr().downcase
                                        end
                                        if selection2 == "q" then
                                                ready = true
                                                file = ""
                                        elsif selection2 == "y" then
                                                ready = true
                                        end
                                else
                                        ready = true
                                end
                        end
                        if File.exists? file then
                                text.set_text "Password: "
                                total = 0
                                manager.render()
                                password = WindowTerminal.getchrs{ |char,full|
                                        if char.ord != 127 then 
                                                text.set_text(text.text + "*")
                                                total += 1
                                        elsif total > 0 then
                                                text.set_text(text.text[0..(text.text.length-1)])
                                                total -= 1
                                        end
                                        manager.render()
                                }
                                
                                if verbose then
                                        text.set_text "Finally, are you sure you would like to decrypt this file using this password? |Y N|"
                                        manager.render()
                                        selection2 = WindowTerminal.getchr().downcase
                                        if selection2 == "y" then
                                                self.decrypt_file(file,password,@@mode)
                                                text.set_text "Successfully decrypted file."
                                                manager.render()
                                        end
                                else
                                        self.decrypt_file(file,password,@@mode)
                                        text.set_text "Successfully decrypted file."
                                        manager.render()
                                end
                                WindowTerminal.getchr()
                        end
                elsif selection == "i" then
                        manager.show(2)
                        manager.render()
                        WindowTerminal.getchr()
                        manager.show(1)
                        manager.render()
                elsif selection == "m" then
                        text.set_text "Select mode: Double - D Normal - N Verbose - V Unverbose - U"
                        manager.render()
                        selection2 = ""
                        selection2 = WindowTerminal.getchr().downcase
                        while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do
                                text.set_text "Invalid: |" + selection2 + "|"
                                manager.render()
                                selection2 = WindowTerminal.getchr().downcase
                        end
                        if selection2 == "d" then
                                @@mode = :double
                        elsif selection2 == "v" then
                                verbose = true
                        elsif selection2 == "u" then
                                verbose = false
                        else
                                @@mode = :normal
                        end
                elsif selection == "n" then
                        if WindowTerminal.os == :linux then
                                print `clear`
                        end
                        puts "Dropping to command line for network communications.\n\n"
                        puts "Port number: "
                        selection2 = ""
                        selection2 = gets.downcase
                        selection2.slice!(-1)
                        while (selection2.to_i == 0 or selection2.to_i == nil) do
                                puts "Invalid: |" + selection2 + "|"
                                selection2 = gets.downcase
                                selection2.slice!(-1)
                        end
                        port = selection2.to_i
                        puts "Would you like to open a client or a server? |C S|"
                        selection2 = ""
                        selection2 = gets.downcase
                        selection2.slice!(-1)
                        while (selection2 != "c" and selection2 != "s") do
                                puts "Invalid: |" + selection2 + "|"
                                selection2 = gets.downcase
                                selection2.slice!(-1)
                        end
                        if selection2 == "c" then
                                puts "IP: "
                                ip = ""
                                ip = gets.downcase
                                ip.slice!(-1)
                                begin
                                        server = TCPSocket.open( ip, port )
                                        EncryptedClient.new( server )
                                        server.close
                                rescue
                                        puts "ERROR\nConnection probably refused."
                                end
                        else
                                server = TCPServer.open port
                                EncryptedServer.new( server.accept )
                                server.close
                        end
                        puts "Returning to normal encryption tasks."
                elsif selection != "q" then
                        text.set_text "Incorrect selection: |" + selection + "|"
                        manager.render()
                        WindowTerminal.getchr()
                end
        end
end
set_mode(arg) click to toggle source

Set current encryption mode.

# File lib/accu-encrypt.rb, line 817
def self.set_mode(arg)
        if arg then
                @@mode = arg
        end
end
windows(manager) click to toggle source

Declares the default windows on a manager object.

# File lib/accu-encrypt.rb, line 583
def self.windows(manager)
        #--
        # Page 1 - Simple window.
        page1_win1 = WindowTerminal::Templates.simple()[0]
        page1_win1.set_name "Lobby"
        manager.new_page(page1_win1)
        # Page 2 - Help
        page2_win1 = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Help")
        page2_text1 = WindowTerminal::ColoredWrappedText.new(WindowTerminal::Orientation.new,self.interactive_help.gsub(/-/,""),6)
        page2_win1.add_objects(page2_text1)
        manager.new_page(page2_win1)
        return page1_win1.objects[2]
        #++
end