accutronic 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +201 -0
- data/bin/accu-encrypt +4 -0
- data/lib/accu-encrypt.rb +823 -0
- data/lib/accu-file.rb +198 -0
- data/lib/accu-window.rb +756 -0
- metadata +93 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/bin/accu-encrypt
ADDED
data/lib/accu-encrypt.rb
ADDED
@@ -0,0 +1,823 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# This is a library which contains a single module
|
5
|
+
# which implements an encryption algorithm of my
|
6
|
+
# own design. It creates a combined password using
|
7
|
+
# an opening password added to the plaintext as it
|
8
|
+
# emerges. The ongoing password offsets the plaintext
|
9
|
+
# to create the output. The library contains two classes
|
10
|
+
# for encrypted network communications, several methods
|
11
|
+
# implementing the encryption previously mentioned,
|
12
|
+
# and two methods for an interactive encryption
|
13
|
+
# session.
|
14
|
+
#
|
15
|
+
#
|
16
|
+
# Author:: Luke Spangler
|
17
|
+
# Email:: m27frogy.roblox@gmail.com
|
18
|
+
|
19
|
+
require "abc"
|
20
|
+
require "rubygems"
|
21
|
+
require "highline/import"
|
22
|
+
require "socket"
|
23
|
+
require "accu-file"
|
24
|
+
require "accu-window"
|
25
|
+
|
26
|
+
# The primary module which packages the classes
|
27
|
+
# and global methods to be used for encryption.
|
28
|
+
module EncryptDecrypt
|
29
|
+
|
30
|
+
# A class which allows for the creation of an encrypted session
|
31
|
+
# over a server, with the client acting as the server.
|
32
|
+
#
|
33
|
+
# Codes:
|
34
|
+
# 0 - Connection refused, 1 - Connection accepted, 2 - Ready, 3 - Sending encrypted message, 4 - End communications, 5 - Verification message inbound
|
35
|
+
class EncryptedServer
|
36
|
+
# Initializes EncryptedServer object.
|
37
|
+
def initialize( server )
|
38
|
+
@server = server
|
39
|
+
print "Connection attempt from |" + @server.peeraddr[3] + "|. Would you like to connect? "
|
40
|
+
response = gets.chomp.downcase
|
41
|
+
if response == "y" then
|
42
|
+
puts "Connection accepted."
|
43
|
+
send_code(1)
|
44
|
+
@password = get_password "Please enter password for communications: "
|
45
|
+
@listen_thread, @interactive_thread = nil, nil
|
46
|
+
@mode = EncryptDecrypt.get_mode
|
47
|
+
if listen_code() == 2 then
|
48
|
+
send_code(2)
|
49
|
+
puts "Ready code received.\nSuccessfully connected!"
|
50
|
+
listen_encrypted()
|
51
|
+
interactive()
|
52
|
+
@listen_thread.join
|
53
|
+
@interactive_thread.join
|
54
|
+
puts "Ending connection."
|
55
|
+
else
|
56
|
+
puts "Connection refused."
|
57
|
+
end
|
58
|
+
else
|
59
|
+
send_code(0)
|
60
|
+
puts "Connection refused."
|
61
|
+
end
|
62
|
+
server.close
|
63
|
+
end
|
64
|
+
|
65
|
+
# Reads a password using the highline gem.
|
66
|
+
def get_password(prompt="Enter Password")
|
67
|
+
ask(prompt) {|q| q.echo = false}
|
68
|
+
end
|
69
|
+
|
70
|
+
# Listens for encrypted messages.
|
71
|
+
def listen_encrypted()
|
72
|
+
@listen_thread = Thread.new {
|
73
|
+
loop {
|
74
|
+
code = listen_code()
|
75
|
+
if code == 3 then
|
76
|
+
lines = ""
|
77
|
+
while line = @server.gets and line != "END\n" do
|
78
|
+
lines << line
|
79
|
+
end
|
80
|
+
secret,text = EncryptDecrypt.decrypt(@password,lines,@mode)
|
81
|
+
puts "Message received:\n\n" + text.chomp + "\n\nMessage end."
|
82
|
+
elsif code == 4 then
|
83
|
+
puts "Client closed connection."
|
84
|
+
@interactive_thread.exit
|
85
|
+
@listen_thread.exit
|
86
|
+
end
|
87
|
+
}
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
# Listens for a generic network communication
|
92
|
+
# and yields to a passed block (if any).
|
93
|
+
def listen()
|
94
|
+
msg = @server.gets.chomp
|
95
|
+
yield msg
|
96
|
+
end
|
97
|
+
|
98
|
+
# Listens for multiple lines, appends them together
|
99
|
+
# and stops at END.
|
100
|
+
def listen_lines()
|
101
|
+
lines = []
|
102
|
+
while line = @server.gets.chomp and line != "END" do
|
103
|
+
#puts "|" + line + "|"
|
104
|
+
lines << line
|
105
|
+
end
|
106
|
+
lines
|
107
|
+
end
|
108
|
+
|
109
|
+
# Listens for a network code.
|
110
|
+
def listen_code()
|
111
|
+
listen { |msg|
|
112
|
+
if msg.slice(0..1) == "C:" then
|
113
|
+
#puts "Code present."
|
114
|
+
return msg.slice(2..msg.length).to_i
|
115
|
+
else
|
116
|
+
#puts "Code not present."
|
117
|
+
return msg.slice(0..1)
|
118
|
+
end
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
# Sends a plaintext message.
|
123
|
+
def send(text)
|
124
|
+
@server.puts text
|
125
|
+
end
|
126
|
+
|
127
|
+
# Sends multiple lines and appends
|
128
|
+
# END to end the send.
|
129
|
+
def send_lines(lines)
|
130
|
+
array = lines.split("\n")
|
131
|
+
array.each do |line|
|
132
|
+
send(line)
|
133
|
+
end
|
134
|
+
send("END")
|
135
|
+
end
|
136
|
+
|
137
|
+
# Send over an encrypted multiline message.
|
138
|
+
def send_encrypted(lines)
|
139
|
+
secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
|
140
|
+
send_code(3)
|
141
|
+
send(text)
|
142
|
+
send("END")
|
143
|
+
end
|
144
|
+
|
145
|
+
# Sends a code.
|
146
|
+
def send_code(code)
|
147
|
+
send("C:#{code}")
|
148
|
+
end
|
149
|
+
|
150
|
+
# Opens an interactive session.
|
151
|
+
def interactive()
|
152
|
+
@interactive_thread = Thread.new {
|
153
|
+
loop {
|
154
|
+
print "What would you like to do? |S Q| "
|
155
|
+
response = gets.chomp.downcase
|
156
|
+
while response != "q" and response != "s" do
|
157
|
+
puts "Incorrect input: |" + response + "|"
|
158
|
+
print "What would you like to do? |S Q| "
|
159
|
+
response = gets.chomp.downcase
|
160
|
+
end
|
161
|
+
if response == "q" then
|
162
|
+
puts "Sending close message to client."
|
163
|
+
send_code(4)
|
164
|
+
@listen_thread.exit
|
165
|
+
@interactive_thread.exit
|
166
|
+
else
|
167
|
+
print "Type message and end with END:\n\n"
|
168
|
+
lines = ""
|
169
|
+
while line = gets and line.chomp != "END" do
|
170
|
+
#puts "|" + line + "|"
|
171
|
+
lines << line
|
172
|
+
end
|
173
|
+
lines = lines.chomp
|
174
|
+
puts "Message read.\nEncrypting now."
|
175
|
+
send_encrypted(lines)
|
176
|
+
puts "Message sent."
|
177
|
+
end
|
178
|
+
}
|
179
|
+
}
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# A class which allows for the creation of an encrypted session
|
184
|
+
# over a server, with the client acting as the client.
|
185
|
+
#
|
186
|
+
# Codes:
|
187
|
+
# 0 - Connection refused, 1 - Connection accepted, 2 - Ready, 3 - Sending encrypted message, 4 - End communications, 5 - Verification message inbound
|
188
|
+
class EncryptedClient
|
189
|
+
# Initializes EncryptedServer object.
|
190
|
+
def initialize( server )
|
191
|
+
@server = server
|
192
|
+
@listen_thread, @interactive_thread = nil, nil
|
193
|
+
@mode = EncryptDecrypt.get_mode
|
194
|
+
if listen_code() == 1 then
|
195
|
+
@password = get_password "Please enter password for communications: "
|
196
|
+
send_code(2)
|
197
|
+
if listen_code() == 2 then
|
198
|
+
puts "Ready code received.\nSuccessfully connected!"
|
199
|
+
listen_encrypted()
|
200
|
+
interactive()
|
201
|
+
@listen_thread.join
|
202
|
+
@interactive_thread.join
|
203
|
+
puts "Client exiting."
|
204
|
+
else
|
205
|
+
puts "Server unready for communications."
|
206
|
+
end
|
207
|
+
else
|
208
|
+
puts "Connection refused."
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# Reads a password using the highline gem.
|
213
|
+
def get_password(prompt="Enter Password")
|
214
|
+
ask(prompt) {|q| q.echo = false}
|
215
|
+
end
|
216
|
+
|
217
|
+
# Listens for encrypted messages.
|
218
|
+
def listen_encrypted()
|
219
|
+
@listen_thread = Thread.new {
|
220
|
+
loop {
|
221
|
+
code = listen_code()
|
222
|
+
if code == 3 then
|
223
|
+
lines = ""
|
224
|
+
while line = @server.gets and line != "END\n" do
|
225
|
+
lines << line
|
226
|
+
end
|
227
|
+
secret,text = EncryptDecrypt.decrypt(@password,lines,@mode)
|
228
|
+
puts "Message received:\n\n" + text + "\n\nMessage end."
|
229
|
+
elsif code == 4 then
|
230
|
+
puts "Server closed connection."
|
231
|
+
@interactive_thread.exit
|
232
|
+
@listen_thread.exit
|
233
|
+
end
|
234
|
+
}
|
235
|
+
}
|
236
|
+
end
|
237
|
+
|
238
|
+
# Listens for a generic network communication
|
239
|
+
# and yields to a passed block (if any).
|
240
|
+
def listen()
|
241
|
+
msg = @server.gets.chomp
|
242
|
+
yield msg
|
243
|
+
end
|
244
|
+
|
245
|
+
# Listens for multiple lines, appends them together
|
246
|
+
# and stops at END.
|
247
|
+
def listen_lines()
|
248
|
+
lines = []
|
249
|
+
while line = @server.gets.chomp and line != "END" do
|
250
|
+
#puts "|" + line + "|"
|
251
|
+
lines << line
|
252
|
+
end
|
253
|
+
lines
|
254
|
+
end
|
255
|
+
|
256
|
+
# Listens for a network code.
|
257
|
+
def listen_code()
|
258
|
+
listen { |msg|
|
259
|
+
if msg.slice(0..1) == "C:" then
|
260
|
+
#puts "Code present."
|
261
|
+
return msg.slice(2..msg.length).to_i
|
262
|
+
else
|
263
|
+
#puts "Code not present."
|
264
|
+
return msg.slice(0..1)
|
265
|
+
end
|
266
|
+
}
|
267
|
+
end
|
268
|
+
|
269
|
+
# Sends a plaintext message.
|
270
|
+
def send(text)
|
271
|
+
@server.puts text
|
272
|
+
end
|
273
|
+
|
274
|
+
# Sends multiple lines and appends
|
275
|
+
# END to end the send.
|
276
|
+
def send_lines(lines)
|
277
|
+
array = lines.split("\n")
|
278
|
+
array.each do |line|
|
279
|
+
send(line)
|
280
|
+
end
|
281
|
+
send("END")
|
282
|
+
end
|
283
|
+
|
284
|
+
# Send over an encrypted multiline message.
|
285
|
+
def send_encrypted(lines)
|
286
|
+
secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
|
287
|
+
send_code(3)
|
288
|
+
send(text)
|
289
|
+
send("END")
|
290
|
+
end
|
291
|
+
|
292
|
+
# Sends a code.
|
293
|
+
def send_code(code)
|
294
|
+
send("C:#{code}")
|
295
|
+
end
|
296
|
+
|
297
|
+
# Opens an interactive session.
|
298
|
+
def interactive()
|
299
|
+
@interactive_thread = Thread.new {
|
300
|
+
loop {
|
301
|
+
print "What would you like to do? |S Q| "
|
302
|
+
response = gets.chomp.downcase
|
303
|
+
while response != "q" and response != "s" do
|
304
|
+
puts "Incorrect input: |" + response + "|"
|
305
|
+
print "What would you like to do? |S Q| "
|
306
|
+
response = gets.chomp.downcase
|
307
|
+
end
|
308
|
+
if response == "q" then
|
309
|
+
puts "Sending close message to server."
|
310
|
+
send_code(4)
|
311
|
+
@listen_thread.exit
|
312
|
+
@interactive_thread.exit
|
313
|
+
else
|
314
|
+
print "Type message and end with END:\n\n"
|
315
|
+
lines = ""
|
316
|
+
while line = gets and line.chomp != "END" do
|
317
|
+
#puts "|" + line + "|"
|
318
|
+
lines << line
|
319
|
+
end
|
320
|
+
lines = lines.chomp
|
321
|
+
puts "Message read.\nEncrypting now."
|
322
|
+
send_encrypted(lines)
|
323
|
+
puts "Message sent."
|
324
|
+
end
|
325
|
+
}
|
326
|
+
}
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
# Returns interactive help in a string.
|
331
|
+
def self.InteractiveHelp()
|
332
|
+
"---------\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"
|
333
|
+
end
|
334
|
+
|
335
|
+
# Encrypts based on a continuing password
|
336
|
+
# offset algorithm.
|
337
|
+
def self.encrypt(secret,source,*args)
|
338
|
+
currentKey = secret.clone
|
339
|
+
output = ""
|
340
|
+
source.each_byte { |orig|
|
341
|
+
secondbyte = currentKey[0].ord
|
342
|
+
out = orig + secondbyte
|
343
|
+
while out > 255 do
|
344
|
+
out = out - 255
|
345
|
+
end
|
346
|
+
output[output.length] = out.chr
|
347
|
+
currentKey[currentKey.length] = orig.chr
|
348
|
+
currentKey.slice! 0
|
349
|
+
}
|
350
|
+
if args[0] == :continuing then
|
351
|
+
return secret,output,currentKey
|
352
|
+
elsif args[0] == :double then
|
353
|
+
return self.encrypt(secret,output)
|
354
|
+
else
|
355
|
+
return secret,output
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
# Decrypts based on a continuing password
|
360
|
+
# offset algorithm.
|
361
|
+
def self.decrypt(secret,plainText,*args)
|
362
|
+
currentKey = secret.clone
|
363
|
+
output = ""
|
364
|
+
plainText.each_byte { |plain|
|
365
|
+
secondbyte = currentKey[0].ord
|
366
|
+
out = plain - secondbyte
|
367
|
+
while out < 0 do
|
368
|
+
out = out + 255
|
369
|
+
end
|
370
|
+
output[output.length] = out.chr
|
371
|
+
currentKey[currentKey.length] = out.chr
|
372
|
+
currentKey.slice! 0
|
373
|
+
}
|
374
|
+
if args[0] == :continuing then
|
375
|
+
return secret,output,currentKey
|
376
|
+
elsif args[0] == :double then
|
377
|
+
return self.decrypt(secret,output)
|
378
|
+
else
|
379
|
+
return secret,output
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
# Encrypts a file.
|
384
|
+
def self.encrypt_file(filename,pass,*args)
|
385
|
+
if File.exists? filename then
|
386
|
+
if args[0] != :double then
|
387
|
+
file = File.open filename
|
388
|
+
pass,output = *self.encrypt(pass,file.read)
|
389
|
+
file.close
|
390
|
+
File.delete filename
|
391
|
+
File.open(filename,"w") do |file|
|
392
|
+
file << output
|
393
|
+
end
|
394
|
+
else
|
395
|
+
self.encrypt_file(filename,pass)
|
396
|
+
self.encrypt_file(filename,pass)
|
397
|
+
end
|
398
|
+
else
|
399
|
+
puts "ERROR"
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
# Decrypts a file.
|
404
|
+
def self.decrypt_file(filename,pass,*args)
|
405
|
+
if File.exists? filename then
|
406
|
+
if args[0] != :double then
|
407
|
+
file = File.open filename
|
408
|
+
pass,output = *self.decrypt(pass,file.read)
|
409
|
+
file.close
|
410
|
+
File.delete filename
|
411
|
+
File.open(filename,"w") do |file|
|
412
|
+
file << output
|
413
|
+
end
|
414
|
+
else
|
415
|
+
self.decrypt_file(filename,pass)
|
416
|
+
self.decrypt_file(filename,pass)
|
417
|
+
end
|
418
|
+
else
|
419
|
+
puts "ERROR"
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
# Opens an interactive terminal session.
|
424
|
+
def self.interactive_terminal()
|
425
|
+
print "Welcome to interactive encode/decode. "
|
426
|
+
selection = ""
|
427
|
+
@@mode = :normal
|
428
|
+
verbose = true
|
429
|
+
while selection != "q" do
|
430
|
+
puts "What would you like to do? |E D I M N Q|"
|
431
|
+
selection = gets.downcase
|
432
|
+
selection.slice!(-1)
|
433
|
+
if selection == "e" then
|
434
|
+
file = ""
|
435
|
+
ready = false
|
436
|
+
while not ready do
|
437
|
+
file = FileLibrary.SelectFile
|
438
|
+
if verbose then
|
439
|
+
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|"
|
440
|
+
selection2 = gets.downcase
|
441
|
+
selection2.slice!(-1)
|
442
|
+
while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
|
443
|
+
puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
|
444
|
+
selection2 = gets.downcase
|
445
|
+
selection2.slice!(-1)
|
446
|
+
end
|
447
|
+
if selection2 == "q" then
|
448
|
+
ready = true
|
449
|
+
file = ""
|
450
|
+
elsif selection2 == "y" then
|
451
|
+
ready = true
|
452
|
+
end
|
453
|
+
else
|
454
|
+
ready = true
|
455
|
+
end
|
456
|
+
end
|
457
|
+
if File.exists? file then
|
458
|
+
password = self.get_password("Password: ")
|
459
|
+
if verbose then
|
460
|
+
puts "Finally, are you sure you would like to encrypt this file using this password? |Y N|"
|
461
|
+
selection2 = gets.downcase
|
462
|
+
selection2.slice!(-1)
|
463
|
+
if selection2 == "y" then
|
464
|
+
self.encrypt_file(file,password,@@mode)
|
465
|
+
puts "Successfully encrypted file."
|
466
|
+
end
|
467
|
+
else
|
468
|
+
self.encrypt_file(file,password,@@mode)
|
469
|
+
puts "Successfully encrypted file."
|
470
|
+
end
|
471
|
+
end
|
472
|
+
elsif selection == "d" then
|
473
|
+
file = ""
|
474
|
+
ready = false
|
475
|
+
while not ready do
|
476
|
+
file = FileLibrary.SelectFile
|
477
|
+
if verbose then
|
478
|
+
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|"
|
479
|
+
selection2 = gets.downcase
|
480
|
+
selection2.slice!(-1)
|
481
|
+
while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
|
482
|
+
puts "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
|
483
|
+
selection2 = gets.downcase
|
484
|
+
selection2.slice!(-1)
|
485
|
+
end
|
486
|
+
if selection2 == "q" then
|
487
|
+
ready = true
|
488
|
+
file = ""
|
489
|
+
elsif selection2 == "y" then
|
490
|
+
ready = true
|
491
|
+
end
|
492
|
+
else
|
493
|
+
ready = true
|
494
|
+
end
|
495
|
+
end
|
496
|
+
if File.exists? file then
|
497
|
+
password = self.get_password("Password: ")
|
498
|
+
if verbose then
|
499
|
+
puts "Finally, are you sure you would like to decrypt this file using this password? |Y N|"
|
500
|
+
selection2 = gets.downcase
|
501
|
+
selection2.slice!(-1)
|
502
|
+
if selection2 == "y" then
|
503
|
+
self.decrypt_file(file,password,@@mode)
|
504
|
+
puts "Successfully decrypted file."
|
505
|
+
end
|
506
|
+
else
|
507
|
+
self.decrypt_file(file,password,@@mode)
|
508
|
+
puts "Successfully decrypted file."
|
509
|
+
end
|
510
|
+
end
|
511
|
+
elsif selection == "i" then
|
512
|
+
puts EncryptDecrypt.InteractiveHelp
|
513
|
+
elsif selection == "m" then
|
514
|
+
puts "Select mode: Double - D Normal - N Verbose - V Unverbose - U"
|
515
|
+
selection2 = ""
|
516
|
+
selection2 = gets.downcase
|
517
|
+
selection2.slice!(-1)
|
518
|
+
while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do
|
519
|
+
puts "Invalid: |" + selection2 + "|"
|
520
|
+
selection2 = gets.downcase
|
521
|
+
selection2.slice!(-1)
|
522
|
+
end
|
523
|
+
if selection2 == "d" then
|
524
|
+
@@mode = :double
|
525
|
+
elsif selection2 == "v" then
|
526
|
+
verbose = true
|
527
|
+
elsif selection2 == "u" then
|
528
|
+
verbose = false
|
529
|
+
else
|
530
|
+
@@mode = :normal
|
531
|
+
end
|
532
|
+
elsif selection == "n" then
|
533
|
+
puts "Port number: "
|
534
|
+
selection2 = ""
|
535
|
+
selection2 = gets.downcase
|
536
|
+
selection2.slice!(-1)
|
537
|
+
while (selection2.to_i == 0 or selection2.to_i == nil) do
|
538
|
+
puts "Invalid: |" + selection2 + "|"
|
539
|
+
selection2 = gets.downcase
|
540
|
+
selection2.slice!(-1)
|
541
|
+
end
|
542
|
+
port = selection2.to_i
|
543
|
+
puts "Would you like to open a client or a server? |C S|"
|
544
|
+
selection2 = ""
|
545
|
+
selection2 = gets.downcase
|
546
|
+
selection2.slice!(-1)
|
547
|
+
while (selection2 != "c" and selection2 != "s") do
|
548
|
+
puts "Invalid: |" + selection2 + "|"
|
549
|
+
selection2 = gets.downcase
|
550
|
+
selection2.slice!(-1)
|
551
|
+
end
|
552
|
+
if selection2 == "c" then
|
553
|
+
puts "IP: "
|
554
|
+
ip = ""
|
555
|
+
ip = gets.downcase
|
556
|
+
ip.slice!(-1)
|
557
|
+
begin
|
558
|
+
server = TCPSocket.open( ip, port )
|
559
|
+
EncryptedClient.new( server )
|
560
|
+
server.close
|
561
|
+
rescue
|
562
|
+
puts "ERROR\nConnection probably refused."
|
563
|
+
end
|
564
|
+
else
|
565
|
+
server = TCPServer.open port
|
566
|
+
EncryptedServer.new( server.accept )
|
567
|
+
server.close
|
568
|
+
end
|
569
|
+
puts "Returning to normal encryption tasks."
|
570
|
+
elsif selection != "q" then
|
571
|
+
puts "Incorrect selection: |" + selection + "|"
|
572
|
+
end
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
# Declares the default windows on a manager object.
|
577
|
+
def self.windows(manager)
|
578
|
+
#--
|
579
|
+
# Page 1 - Simple window.
|
580
|
+
page1_win1 = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Lobby")
|
581
|
+
page1_text1 = WindowTerminal::WrappedText.new(WindowTerminal::Orientation.new,"",8,:preserve)
|
582
|
+
page1_win1.add_object page1_text1
|
583
|
+
manager.new_page(page1_win1)
|
584
|
+
# Page 2 - Help
|
585
|
+
page2_win1 = WindowTerminal::Window.new(WindowTerminal::Orientation.new,"Help")
|
586
|
+
page2_text1 = WindowTerminal::ColoredWrappedText.new(WindowTerminal::Orientation.new,self.InteractiveHelp.gsub(/-/,""),6)
|
587
|
+
page2_win1.add_objects(page2_text1)
|
588
|
+
manager.new_page(page2_win1)
|
589
|
+
return page1_text1
|
590
|
+
#++
|
591
|
+
end
|
592
|
+
|
593
|
+
# Opens an interactive session with
|
594
|
+
# the WindowTerminal library.
|
595
|
+
def self.interactive_windows()
|
596
|
+
raise "WindowTerminal library missing" if not defined? WindowTerminal
|
597
|
+
manager = WindowTerminal::WindowManager.new
|
598
|
+
text = self.windows(manager)
|
599
|
+
selection = ""
|
600
|
+
@@mode = :normal
|
601
|
+
verbose = true
|
602
|
+
while selection != "q" do
|
603
|
+
text.set_text "What would you like to do? |E D I M N Q|"
|
604
|
+
manager.show(1)
|
605
|
+
manager.render()
|
606
|
+
selection = WindowTerminal.getchr().downcase
|
607
|
+
if selection == "e" then
|
608
|
+
file = ""
|
609
|
+
ready = false
|
610
|
+
while not ready do
|
611
|
+
file = FileLibrary.SelectFile_With_Windows(manager)
|
612
|
+
manager.show
|
613
|
+
if verbose then
|
614
|
+
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|"
|
615
|
+
manager.render()
|
616
|
+
selection2 = WindowTerminal.getchr().downcase
|
617
|
+
while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
|
618
|
+
text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
|
619
|
+
manager.render()
|
620
|
+
selection2 = WindowTerminal.getchr().downcase
|
621
|
+
end
|
622
|
+
if selection2 == "q" then
|
623
|
+
ready = true
|
624
|
+
file = ""
|
625
|
+
elsif selection2 == "y" then
|
626
|
+
ready = true
|
627
|
+
end
|
628
|
+
else
|
629
|
+
ready = true
|
630
|
+
end
|
631
|
+
end
|
632
|
+
if File.exists? file then
|
633
|
+
text.set_text "Password: "
|
634
|
+
total = 0
|
635
|
+
manager.render()
|
636
|
+
password = WindowTerminal.getchrs{ |char,full|
|
637
|
+
if char.ord != 127 then
|
638
|
+
text.set_text(text.text + "*")
|
639
|
+
total += 1
|
640
|
+
elsif total > 0 then
|
641
|
+
text.set_text(text.text[0..(text.text.length-1)])
|
642
|
+
total -= 1
|
643
|
+
end
|
644
|
+
manager.render()
|
645
|
+
}
|
646
|
+
|
647
|
+
if verbose then
|
648
|
+
text.set_text "Finally, are you sure you would like to encrypt this file using this password? |Y N|"
|
649
|
+
manager.render()
|
650
|
+
selection2 = WindowTerminal.getchr().downcase
|
651
|
+
if selection2 == "y" then
|
652
|
+
self.encrypt_file(file,password,@@mode)
|
653
|
+
text.set_text "Successfully encrypted file."
|
654
|
+
manager.render()
|
655
|
+
end
|
656
|
+
else
|
657
|
+
self.encrypt_file(file,password,@@mode)
|
658
|
+
text.set_text "Successfully encrypted file."
|
659
|
+
manager.render()
|
660
|
+
end
|
661
|
+
WindowTerminal.getchr()
|
662
|
+
end
|
663
|
+
elsif selection == "d" then
|
664
|
+
file = ""
|
665
|
+
ready = false
|
666
|
+
while not ready do
|
667
|
+
file = FileLibrary.SelectFile_With_Windows(manager)
|
668
|
+
manager.show
|
669
|
+
if verbose then
|
670
|
+
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|"
|
671
|
+
manager.render()
|
672
|
+
selection2 = WindowTerminal.getchr().downcase
|
673
|
+
while (selection2 != "y" and selection2 != "n" and selection2 != "q") do
|
674
|
+
text.set_text "Incorrect choice: |" + selection2 + "|\nAre you sure you would like to use this file? |Y N Q|"
|
675
|
+
manager.render()
|
676
|
+
selection2 = WindowTerminal.getchr().downcase
|
677
|
+
end
|
678
|
+
if selection2 == "q" then
|
679
|
+
ready = true
|
680
|
+
file = ""
|
681
|
+
elsif selection2 == "y" then
|
682
|
+
ready = true
|
683
|
+
end
|
684
|
+
else
|
685
|
+
ready = true
|
686
|
+
end
|
687
|
+
end
|
688
|
+
if File.exists? file then
|
689
|
+
text.set_text "Password: "
|
690
|
+
total = 0
|
691
|
+
manager.render()
|
692
|
+
password = WindowTerminal.getchrs{ |char,full|
|
693
|
+
if char.ord != 127 then
|
694
|
+
text.set_text(text.text + "*")
|
695
|
+
total += 1
|
696
|
+
elsif total > 0 then
|
697
|
+
text.set_text(text.text[0..(text.text.length-1)])
|
698
|
+
total -= 1
|
699
|
+
end
|
700
|
+
manager.render()
|
701
|
+
}
|
702
|
+
|
703
|
+
if verbose then
|
704
|
+
text.set_text "Finally, are you sure you would like to decrypt this file using this password? |Y N|"
|
705
|
+
manager.render()
|
706
|
+
selection2 = WindowTerminal.getchr().downcase
|
707
|
+
if selection2 == "y" then
|
708
|
+
self.decrypt_file(file,password,@@mode)
|
709
|
+
text.set_text "Successfully decrypted file."
|
710
|
+
manager.render()
|
711
|
+
end
|
712
|
+
else
|
713
|
+
self.decrypt_file(file,password,@@mode)
|
714
|
+
text.set_text "Successfully decrypted file."
|
715
|
+
manager.render()
|
716
|
+
end
|
717
|
+
WindowTerminal.getchr()
|
718
|
+
end
|
719
|
+
elsif selection == "i" then
|
720
|
+
manager.show(2)
|
721
|
+
manager.render()
|
722
|
+
WindowTerminal.getchr()
|
723
|
+
manager.show(1)
|
724
|
+
manager.render()
|
725
|
+
elsif selection == "m" then
|
726
|
+
text.set_text "Select mode: Double - D Normal - N Verbose - V Unverbose - U"
|
727
|
+
manager.render()
|
728
|
+
selection2 = ""
|
729
|
+
selection2 = WindowTerminal.getchr().downcase
|
730
|
+
while (selection2 != "d" and selection2 != "n" and selection2 != "v" and selection2 != "u") do
|
731
|
+
text.set_text "Invalid: |" + selection2 + "|"
|
732
|
+
manager.render()
|
733
|
+
selection2 = WindowTerminal.getchr().downcase
|
734
|
+
end
|
735
|
+
if selection2 == "d" then
|
736
|
+
@@mode = :double
|
737
|
+
elsif selection2 == "v" then
|
738
|
+
verbose = true
|
739
|
+
elsif selection2 == "u" then
|
740
|
+
verbose = false
|
741
|
+
else
|
742
|
+
@@mode = :normal
|
743
|
+
end
|
744
|
+
elsif selection == "n" then
|
745
|
+
if WindowTerminal.os == :linux then
|
746
|
+
print `clear`
|
747
|
+
end
|
748
|
+
puts "Dropping to command line for network communications.\n\n"
|
749
|
+
puts "Port number: "
|
750
|
+
selection2 = ""
|
751
|
+
selection2 = gets.downcase
|
752
|
+
selection2.slice!(-1)
|
753
|
+
while (selection2.to_i == 0 or selection2.to_i == nil) do
|
754
|
+
puts "Invalid: |" + selection2 + "|"
|
755
|
+
selection2 = gets.downcase
|
756
|
+
selection2.slice!(-1)
|
757
|
+
end
|
758
|
+
port = selection2.to_i
|
759
|
+
puts "Would you like to open a client or a server? |C S|"
|
760
|
+
selection2 = ""
|
761
|
+
selection2 = gets.downcase
|
762
|
+
selection2.slice!(-1)
|
763
|
+
while (selection2 != "c" and selection2 != "s") do
|
764
|
+
puts "Invalid: |" + selection2 + "|"
|
765
|
+
selection2 = gets.downcase
|
766
|
+
selection2.slice!(-1)
|
767
|
+
end
|
768
|
+
if selection2 == "c" then
|
769
|
+
puts "IP: "
|
770
|
+
ip = ""
|
771
|
+
ip = gets.downcase
|
772
|
+
ip.slice!(-1)
|
773
|
+
begin
|
774
|
+
server = TCPSocket.open( ip, port )
|
775
|
+
EncryptedClient.new( server )
|
776
|
+
server.close
|
777
|
+
rescue
|
778
|
+
puts "ERROR\nConnection probably refused."
|
779
|
+
end
|
780
|
+
else
|
781
|
+
server = TCPServer.open port
|
782
|
+
EncryptedServer.new( server.accept )
|
783
|
+
server.close
|
784
|
+
end
|
785
|
+
puts "Returning to normal encryption tasks."
|
786
|
+
elsif selection != "q" then
|
787
|
+
text.set_text "Incorrect selection: |" + selection + "|"
|
788
|
+
manager.render()
|
789
|
+
WindowTerminal.getchr()
|
790
|
+
end
|
791
|
+
end
|
792
|
+
end
|
793
|
+
|
794
|
+
# Chooses either interactive mode based
|
795
|
+
# on operating system.
|
796
|
+
def self.interactive()
|
797
|
+
if defined? WindowTerminal and WindowTerminal.os == :linux then
|
798
|
+
self.interactive_windows
|
799
|
+
else
|
800
|
+
self.interactive_terminal
|
801
|
+
end
|
802
|
+
end
|
803
|
+
|
804
|
+
# Returns current encryption
|
805
|
+
# mode.
|
806
|
+
def self.get_mode()
|
807
|
+
@@mode.dup
|
808
|
+
end
|
809
|
+
end
|
810
|
+
|
811
|
+
# Copyright 2014 Luke Spangler
|
812
|
+
#
|
813
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
814
|
+
# you may not use this file except in compliance with the License.
|
815
|
+
# You may obtain a copy of the License at
|
816
|
+
#
|
817
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
818
|
+
#
|
819
|
+
# Unless required by applicable law or agreed to in writing, software
|
820
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
821
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
822
|
+
# See the License for the specific language governing permissions and
|
823
|
+
# limitations under the License.
|