-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoder
More file actions
executable file
·103 lines (83 loc) · 2.69 KB
/
coder
File metadata and controls
executable file
·103 lines (83 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/ruby
ALPHABET = [*("a".."z"), *("1".."9"), " "]
STRICT_ALPHABET = [*("a".."z"), *("A".."Z"), *("1".."9"), " ", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "{", "}", "[", "]", "|", "\\", '<', '>', '.', ',', '?', '/']
if File.file?(File.join(File.dirname(__FILE__), 'encryption.rb'))
require_relative 'encryption.rb'
else
require '/usr/share/ruby/vendor_ruby/coder/encryption.rb'
end
require 'optparse'
class String
def clean_newlines sub = '[newline]'
replace gsub "\n", sub
end
end
m = :encrypt # method - encrypt or decrypt
s = false # strict? - true or false
t = "example text" # text - string
k = "examplekey" # key - string
f, o = nil # file input and output - strings
OptionParser.new do |parser|
parser.banner = "Usage: coder [options]"
parser.on("-h", "--help", "Show this help message") do
puts parser
exit
end
parser.on("-e", "--encrypt", "Encrypts text (default)") do
end
parser.on("-d", "--decrypt", "Decrypts text") do
m = :decrypt
end
parser.on("-t", "--text TEXT", "The text to encrypt or decrypt") do |v|
t = v
end
parser.on("-k", "--key KEY", "Specify a key to use") do |v|
k = v
end
parser.on("-s", "--strict", "Keeps capital letters and encrypts punctuation") do
s = true
end
parser.on("-f", "--file PATH", "Encrypt or decrypt a file instead") do |v|
unless File.file? v
puts "The path you entered does not exist or is not a file."
exit
end
f = v
t = File.read f
end
parser.on("-o", "--output PATH", "Specify a file to export the result of the encryption or decryption to") do |v|
o = v
end
end.parse!
def check_key k, a
k.chars.each do |char|
return false unless a.include? char
end
true
end
def key_error a
puts "The key that you entered is invalid! Please use only the following characters: "
puts a.map {|char| "'#{char}'"}.join(", ") + "."
puts "NOTE: If you are trying to use capitals or punctuation, you may want to try strict mode (--strict or -s)."
exit
end
t_display = f ? f : t
e = m == :encrypt
a = s ? STRICT_ALPHABET : ALPHABET
key_error a unless check_key k, a
m_text = e ? "encrypted" : "decrypted"
e_text = t.encrypt k, a, s, !e
e_text_display = o ? o : e_text
if e_text_display.length + t_display.length < 50
t_display.clean_newlines
e_text_display.clean_newlines
line = "-" * (t_display.length+2) + "=" * (e_text_display.length+1)
puts line
puts "#{t_display} ➜ #{e_text_display}"
puts line + "\n"
puts (f ? "The contents of " : "The text ") + "\"#{t_display}\" was #{m_text} to \"#{e_text_display}\" using key: \"#{k}\""
else
puts "\n#{t_display}\n\n"
puts e_text_display + "\n"
end
File.new(o, "w").write e_text if o