rbencrypt2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/rubyencrypt +10 -0
  3. data/src/MyClass.rb +310 -0
  4. data/src/glade/MyClass.glade +254 -0
  5. metadata +75 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c99270c9bb1ad7155f5f189500e5febbc2a8bda
4
+ data.tar.gz: b821bfa10fbf973239ee81298bb3cdc67df8cb69
5
+ SHA512:
6
+ metadata.gz: ecaf84b9841e872b46757b696930ae75877eb4e4844609030207a51662f0f7cffe07795ba53c8294b16e28b60bfe702d7090a7e1f9946f40ff9aa75afaff0fe6
7
+ data.tar.gz: bf7b0afd0a21b2242ecd34224d263e07d132ad70e1ac5c0abf366afd8ef1bead2fb0a6f67cf8a33d566f2e3ec385d63bbbeb9b15aec4ef363f9b35ca6f465cf1
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require "vrlib"
4
+
5
+ # from require_all gem:
6
+ require_rel 'src/'
7
+
8
+
9
+ MyClass.new.show_glade()
10
+
@@ -0,0 +1,310 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'prime'
5
+
6
+ # RSA
7
+ # Step 1: Pick p and q (both prime)
8
+ # Step 2: n = p * q = modulus
9
+ # Step 3: phi(n) = (p-1) * (q-1) (amount of primes not coprime to p or q, up to the modulus)
10
+ # Step 4: d * e mod((p-1) * (q-1)) = 1
11
+ ## Criteria for e
12
+ ## 1. coprime with (phi(n), n)
13
+ ## 2. 1 < e < phi(n)
14
+ # Step 5: Choose d such that d * e mod (phi(n)) = 1 (d is secret, e is public)
15
+
16
+ # public info: (e, n)
17
+ # private info: (d, p, q)
18
+
19
+ # to encrypt: raise your number x ^ e, mod n;
20
+ # to decrypt: raise the encrypted message m ^ d, mod n
21
+
22
+ # To do crazy powers of a ** b % n
23
+ def raiseToModulus a, b, n
24
+ b < 0 and raise ArgumentError, "negative exponent"
25
+ prod = 1
26
+ a %= n
27
+ until b.zero? do
28
+ if b.odd?
29
+ prod = (prod * a) % n
30
+ end
31
+ b /= 2
32
+ a = (a ** 2) % n
33
+ end
34
+ prod
35
+ end
36
+
37
+ # Primality Test
38
+ def miller_rabin_prime?(n, g)
39
+ d = n - 1
40
+ s = 0
41
+ while d % 2 == 0 do
42
+ d /= 2
43
+ s += 1
44
+ end
45
+ g.times do
46
+ a = 2 + rand(n - 4)
47
+ x = raiseToModulus(a, d, n) # x = (a**d) % n
48
+ next if x == 1 || x == n - 1
49
+ for r in (1..s - 1)
50
+ x = raiseToModulus(x, 2, n) # x = (x**2) % n
51
+ return false if x == 1
52
+ break if x == n - 1
53
+ end
54
+ return false if x != n - 1
55
+ end
56
+ true # probably
57
+ end
58
+
59
+ # Actaully Testing primality
60
+ def isPrime? n
61
+ Prime.each(10000) do |num|
62
+ if ( raiseToModulus num, n-1, n ) != 1 or n % num == 0
63
+ return false
64
+ end
65
+ end
66
+ return miller_rabin_prime?(n, 10)
67
+ end
68
+
69
+ ## Determine the Mult. Inv.
70
+
71
+ # Euclidian Algorithim
72
+ def extended_gcd(a, b)
73
+ last_remainder, remainder = a.abs, b.abs
74
+ x, last_x, y, last_y = 0, 1, 1, 0
75
+ while remainder != 0
76
+ last_remainder, (quotient, remainder) = remainder, last_remainder.divmod(remainder)
77
+ x, last_x = last_x - quotient*x, x
78
+ y, last_y = last_y - quotient*y, y
79
+ end
80
+
81
+ return last_remainder, last_x * (a < 0 ? -1 : 1)
82
+ end
83
+
84
+ # Get multi. Inv. based on eculidian algorithim
85
+ def invmod(e, et)
86
+ g, x = extended_gcd(e, et)
87
+ if g != 1
88
+ raise 'The maths are broken!'
89
+ end
90
+ x % et
91
+ end
92
+
93
+ # genrate x bit number
94
+ def randomBitNum x
95
+ bitString = "1"
96
+ (1...x).each do
97
+ bitString += rand(2).to_s
98
+ end
99
+ return bitString.to_i(2)
100
+ end
101
+
102
+ def createKeyPair email
103
+ # converting from command line code to function
104
+ name = email
105
+
106
+ # Array to store p and q
107
+ randNums = []
108
+ (0..1).each do |i|
109
+ # Generate Random Num
110
+ value = randomBitNum 1024
111
+ # If even, make odd
112
+ if value % 2 == 0
113
+ value += 1
114
+ end
115
+ # Check if number generated is prime
116
+ # Add 2 if not, until prime
117
+ while not isPrime? value do
118
+ value += 2
119
+ end
120
+ randNums[i] = value
121
+ end
122
+
123
+ # n = p * q
124
+ n = randNums[0] * randNums[1]
125
+ # coprime = (p - 1) * (q - 1)
126
+ coprime = (randNums[0] - 1) * (randNums[1] - 1)
127
+ # Generate e randomly
128
+ e = randomBitNum 1024
129
+ # Keep incrementing until e is co prime with (p - 1) * (q - 1)
130
+ # and e is prime
131
+ until (coprime.gcd(e) == 1) and (isPrime? e )do
132
+ e += 1
133
+ end
134
+ # D is the mult. inv. of e and (p - 1) * (q - 1)s
135
+ d = invmod(e,coprime)
136
+
137
+ # Create a folder if not there
138
+ if not File.directory?(File.expand_path('~')+'/.bre')
139
+ Dir.mkdir File.expand_path('~')+'/.bre'
140
+ end
141
+
142
+ # Write to pub and sec keys
143
+ File.open(File.expand_path('~')+"/.bre/#{name[0...name.index("@")]}.pub", 'w') do |file|
144
+ file.write(name + "\n" + e.to_s + "\n" + n.to_s)
145
+ end
146
+ File.open(File.expand_path('~')+"/.bre/#{name[0...name.index("@")]}.sec", 'w') do |file|
147
+ file.write(name + "\n" + d.to_s + "\n" + n.to_s)
148
+ end
149
+
150
+ ## Add key to resgister
151
+
152
+ # Check if register exists
153
+ if File.file?(File.expand_path('~')+"/.bre/keys.json")
154
+ keysFile = File.read(File.expand_path('~')+"/.bre/keys.json")
155
+ key_hash = JSON.parse(keysFile)
156
+ else
157
+ key_hash = {}
158
+ end
159
+
160
+ # add key's hash value to register
161
+ key_hash[name] = { location: "#{name[0...name.index("@")]}.pub", secret: "#{name[0...name.index("@")]}.sec" }
162
+
163
+ # Write to the register
164
+ File.open(File.expand_path('~')+"/.bre/keys.json", 'w') do |file|
165
+ file.write(key_hash.to_json)
166
+ end
167
+ end
168
+
169
+ def addKey email
170
+ emailName = email.split("@").first
171
+ loc = File.expand_path("~")+"/Desktop/"+emailName+".pub"
172
+
173
+ # Copy to the folder with username as file name
174
+ system("mv #{loc} "+File.expand_path('~')+"/.bre/#{emailName}.pub")
175
+
176
+ # Open up keys.json to add key to register
177
+ if File.file?(File.expand_path('~')+"/.bre/keys.json")
178
+ keysFile = File.read(File.expand_path('~')+"/.bre/keys.json")
179
+ key_hash = JSON.parse(keysFile)
180
+ else
181
+ key_hash = {}
182
+ end
183
+
184
+ # Assign it a hash value
185
+ key_hash[email] = { location: "#{emailName}.pub" }
186
+
187
+ # Write to register
188
+ File.open(File.expand_path('~')+"/.bre/keys.json", 'w') do |file|
189
+ file.write(key_hash.to_json)
190
+ end
191
+ end
192
+
193
+ # Method to encrypt
194
+ def encrypt text, email
195
+ dataToEncrypt = text
196
+ charArray = dataToEncrypt.split("")
197
+ toEncrypt = ""
198
+ charArray.each do |char|
199
+ if char.ord.to_s(8).length == 2
200
+ toEncrypt += "0" + char.ord.to_s(8)
201
+ else
202
+ toEncrypt += char.ord.to_s(8)
203
+ end
204
+ end
205
+ emailName = email.split("@").first
206
+ keyLocation = File.expand_path("~")+"/.bre/"+emailName+".pub"
207
+ eAndN = File.read(keyLocation)
208
+ e = eAndN[(eAndN.index("\n") + 1)..-1]
209
+ e = e[0...e.index("\n")]
210
+ n = eAndN[(eAndN.index("\n") + 1)..-1]
211
+ n = n[(n.index("\n") + 1)..-1]
212
+ toEncrypt = toEncrypt.scan(/.{1,300}/)
213
+ encrypted = ""
214
+ toEncrypt.each do |encrypt|
215
+ pre = ""
216
+ while encrypt[0] == "0" do
217
+ pre += "0|"
218
+ encrypt = encrypt[1..-1]
219
+ end
220
+ encrypted += pre + raiseToModulus(encrypt.to_i, e.to_i, n.to_i).to_s + "|"
221
+ end
222
+ return encrypted
223
+ end
224
+
225
+ # Method to decrypt
226
+ def decrypt text, email
227
+ emailName = email.split("@").first
228
+ keyLocation = File.expand_path("~")+"/.bre/"+emailName+".sec"
229
+ # get numbers for encryption
230
+ dAndN = File.read(keyLocation)
231
+ d = dAndN[(dAndN.index("\n") + 1)..-1]
232
+ d = d[0...d.index("\n")]
233
+ n = dAndN[(dAndN.index("\n") + 1)..-1]
234
+ n = n[(n.index("\n") + 1)..-1]
235
+
236
+ # Get encryptedText
237
+ encryptedText = text
238
+ encryptedText = encryptedText.split("|")
239
+ # Convert Back
240
+ decryptedText = ""
241
+ encryptedText.each do |encrypt|
242
+ decryptedText += raiseToModulus(encrypt.to_i, d.to_i, n.to_i).to_s
243
+ end
244
+ #decryptedText= decryptedText.to_i
245
+ # Cut string up into thirds
246
+ #decryptedText = decryptedText.to_s(8)
247
+ decryptedText = decryptedText.scan(/.{1,3}/)
248
+ # Loop through to turn each back into letter
249
+ text = ""
250
+ decryptedText.each do |letter|
251
+ text += letter.to_i(8).chr
252
+ end
253
+ return text
254
+ end
255
+
256
+ class MyClass #(change name)
257
+
258
+ include GladeGUI
259
+
260
+ def create__clicked(*args)
261
+ email = @builder["input"].text
262
+ if email.length >= 1
263
+ alert "Click OK to go. This may take some time."
264
+ createKeyPair(email)
265
+ alert "Finished! Check ~/.bre to find the keychain."
266
+ @builder["create"].label = "Created!"
267
+ else
268
+ alert "Invalid email!"
269
+ @builder["create"].label = "Create"
270
+ end
271
+ end
272
+
273
+ def add__clicked(*args)
274
+ keyLoc = @builder["input"].text
275
+ if keyLoc.length >= 1
276
+ addKey(keyLoc)
277
+ alert "Key added! Check ~/.bre to find the keychain."
278
+ else
279
+ alert "Invalid location!"
280
+ end
281
+ end
282
+
283
+ def crypt__clicked(*args)
284
+ email = @builder["input"].text
285
+ if email.length >= 1
286
+ @builder["window2"].show
287
+ else
288
+ alert "Invalid email!"
289
+ end
290
+ end
291
+
292
+ def encrypt__clicked(*args)
293
+ filePath = @builder["input"].text
294
+ toEncrypt = @builder["textinput"].text
295
+ encryptedText = encrypt(toEncrypt, filePath)
296
+ @builder["textinput"].text = encryptedText
297
+ end
298
+
299
+ def decrypt__clicked(*args)
300
+ filePath = @builder["input"].text
301
+ toDecrypt = @builder["textinput"].text
302
+ decryptedText = decrypt(toDecrypt, filePath)
303
+ @builder["textinput"].text = decryptedText
304
+ end
305
+
306
+ def help__clicked(*args)
307
+ alert "Add: must be used to add a .pub file which is on the desktop, \nthe name of it being 'local-part-of-email.pub'. For more \ninformation, visit https://github.com/ryandrew14/RubyEncrypt."
308
+ end
309
+ end
310
+
@@ -0,0 +1,254 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!-- Generated with glade 3.20.2 -->
3
+ <interface>
4
+ <requires lib="gtk+" version="3.0"/>
5
+ <object class="GtkWindow" id="window1">
6
+ <property name="can_focus">False</property>
7
+ <property name="window_position">center</property>
8
+ <child>
9
+ <object class="GtkBox" id="main">
10
+ <property name="visible">True</property>
11
+ <property name="can_focus">False</property>
12
+ <property name="orientation">vertical</property>
13
+ <child>
14
+ <object class="GtkLabel" id="header">
15
+ <property name="visible">True</property>
16
+ <property name="can_focus">False</property>
17
+ <property name="margin_top">10</property>
18
+ <property name="margin_bottom">10</property>
19
+ <property name="label" translatable="yes">Welcome to RubyEncrypt!</property>
20
+ <attributes>
21
+ <attribute name="weight" value="semibold"/>
22
+ <attribute name="scale" value="2"/>
23
+ </attributes>
24
+ </object>
25
+ <packing>
26
+ <property name="expand">False</property>
27
+ <property name="fill">True</property>
28
+ <property name="position">0</property>
29
+ </packing>
30
+ </child>
31
+ <child>
32
+ <object class="GtkGrid" id="buttons">
33
+ <property name="visible">True</property>
34
+ <property name="can_focus">False</property>
35
+ <child>
36
+ <object class="GtkButton" id="create">
37
+ <property name="label" translatable="yes">Create</property>
38
+ <property name="visible">True</property>
39
+ <property name="can_focus">True</property>
40
+ <property name="receives_default">True</property>
41
+ </object>
42
+ <packing>
43
+ <property name="left_attach">1</property>
44
+ <property name="top_attach">0</property>
45
+ </packing>
46
+ </child>
47
+ <child>
48
+ <object class="GtkButton" id="add">
49
+ <property name="label" translatable="yes">Add</property>
50
+ <property name="visible">True</property>
51
+ <property name="can_focus">True</property>
52
+ <property name="receives_default">True</property>
53
+ </object>
54
+ <packing>
55
+ <property name="left_attach">1</property>
56
+ <property name="top_attach">1</property>
57
+ </packing>
58
+ </child>
59
+ <child>
60
+ <object class="GtkButton" id="crypt">
61
+ <property name="label" translatable="yes">Encrypt</property>
62
+ <property name="visible">True</property>
63
+ <property name="can_focus">True</property>
64
+ <property name="receives_default">True</property>
65
+ </object>
66
+ <packing>
67
+ <property name="left_attach">1</property>
68
+ <property name="top_attach">2</property>
69
+ </packing>
70
+ </child>
71
+ <child>
72
+ <object class="GtkLabel" id="createlabel">
73
+ <property name="visible">True</property>
74
+ <property name="can_focus">False</property>
75
+ <property name="margin_left">10</property>
76
+ <property name="margin_right">10</property>
77
+ <property name="label" translatable="yes">Create a new keypair with the given email, storing it in ~/.bre/</property>
78
+ </object>
79
+ <packing>
80
+ <property name="left_attach">0</property>
81
+ <property name="top_attach">0</property>
82
+ </packing>
83
+ </child>
84
+ <child>
85
+ <object class="GtkLabel" id="addlabel">
86
+ <property name="visible">True</property>
87
+ <property name="can_focus">False</property>
88
+ <property name="label" translatable="yes">Add a key file to the keychain*</property>
89
+ </object>
90
+ <packing>
91
+ <property name="left_attach">0</property>
92
+ <property name="top_attach">1</property>
93
+ </packing>
94
+ </child>
95
+ <child>
96
+ <object class="GtkLabel" id="cryptlabel">
97
+ <property name="visible">True</property>
98
+ <property name="can_focus">False</property>
99
+ <property name="label" translatable="yes">Open the encryption/decryption window</property>
100
+ </object>
101
+ <packing>
102
+ <property name="left_attach">0</property>
103
+ <property name="top_attach">2</property>
104
+ </packing>
105
+ </child>
106
+ </object>
107
+ <packing>
108
+ <property name="expand">False</property>
109
+ <property name="fill">True</property>
110
+ <property name="position">1</property>
111
+ </packing>
112
+ </child>
113
+ <child>
114
+ <object class="GtkEntry" id="input">
115
+ <property name="visible">True</property>
116
+ <property name="can_focus">True</property>
117
+ </object>
118
+ <packing>
119
+ <property name="expand">False</property>
120
+ <property name="fill">True</property>
121
+ <property name="position">2</property>
122
+ </packing>
123
+ </child>
124
+ <child>
125
+ <object class="GtkGrid">
126
+ <property name="visible">True</property>
127
+ <property name="can_focus">False</property>
128
+ <child>
129
+ <object class="GtkLabel" id="footer">
130
+ <property name="visible">True</property>
131
+ <property name="can_focus">False</property>
132
+ <property name="margin_left">10</property>
133
+ <property name="margin_top">10</property>
134
+ <property name="margin_bottom">10</property>
135
+ <property name="label" translatable="yes">Type your email in the above box to connect to your key.</property>
136
+ <attributes>
137
+ <attribute name="scale" value="1.25"/>
138
+ </attributes>
139
+ </object>
140
+ <packing>
141
+ <property name="left_attach">0</property>
142
+ <property name="top_attach">0</property>
143
+ </packing>
144
+ </child>
145
+ <child>
146
+ <object class="GtkButton" id="help">
147
+ <property name="label" translatable="yes">Help</property>
148
+ <property name="visible">True</property>
149
+ <property name="can_focus">True</property>
150
+ <property name="receives_default">True</property>
151
+ <property name="margin_left">5</property>
152
+ </object>
153
+ <packing>
154
+ <property name="left_attach">1</property>
155
+ <property name="top_attach">0</property>
156
+ </packing>
157
+ </child>
158
+ </object>
159
+ <packing>
160
+ <property name="expand">False</property>
161
+ <property name="fill">True</property>
162
+ <property name="position">3</property>
163
+ </packing>
164
+ </child>
165
+ </object>
166
+ </child>
167
+ <child type="titlebar">
168
+ <placeholder/>
169
+ </child>
170
+ </object>
171
+ <object class="GtkWindow" id="window2">
172
+ <property name="can_focus">False</property>
173
+ <child>
174
+ <object class="GtkBox" id="main2">
175
+ <property name="visible">True</property>
176
+ <property name="can_focus">False</property>
177
+ <property name="orientation">vertical</property>
178
+ <child>
179
+ <object class="GtkLabel" id="cryptwindowlabel">
180
+ <property name="visible">True</property>
181
+ <property name="can_focus">False</property>
182
+ <property name="margin_left">10</property>
183
+ <property name="margin_right">10</property>
184
+ <property name="margin_top">10</property>
185
+ <property name="margin_bottom">10</property>
186
+ <property name="label" translatable="yes">Welcome to the Encryption Window!</property>
187
+ <attributes>
188
+ <attribute name="scale" value="1.5"/>
189
+ </attributes>
190
+ </object>
191
+ <packing>
192
+ <property name="expand">False</property>
193
+ <property name="fill">True</property>
194
+ <property name="position">0</property>
195
+ </packing>
196
+ </child>
197
+ <child>
198
+ <object class="GtkButtonBox" id="cryptbuttons">
199
+ <property name="visible">True</property>
200
+ <property name="can_focus">False</property>
201
+ <property name="margin_top">10</property>
202
+ <property name="margin_bottom">10</property>
203
+ <property name="layout_style">spread</property>
204
+ <child>
205
+ <object class="GtkButton" id="encrypt">
206
+ <property name="label" translatable="yes">Encrypt</property>
207
+ <property name="visible">True</property>
208
+ <property name="can_focus">True</property>
209
+ <property name="receives_default">True</property>
210
+ </object>
211
+ <packing>
212
+ <property name="expand">True</property>
213
+ <property name="fill">True</property>
214
+ <property name="position">0</property>
215
+ </packing>
216
+ </child>
217
+ <child>
218
+ <object class="GtkButton" id="decrypt">
219
+ <property name="label" translatable="yes">Decrypt</property>
220
+ <property name="visible">True</property>
221
+ <property name="can_focus">True</property>
222
+ <property name="receives_default">True</property>
223
+ </object>
224
+ <packing>
225
+ <property name="expand">True</property>
226
+ <property name="fill">True</property>
227
+ <property name="position">1</property>
228
+ </packing>
229
+ </child>
230
+ </object>
231
+ <packing>
232
+ <property name="expand">False</property>
233
+ <property name="fill">True</property>
234
+ <property name="position">1</property>
235
+ </packing>
236
+ </child>
237
+ <child>
238
+ <object class="GtkEntry" id="textinput">
239
+ <property name="visible">True</property>
240
+ <property name="can_focus">True</property>
241
+ </object>
242
+ <packing>
243
+ <property name="expand">False</property>
244
+ <property name="fill">True</property>
245
+ <property name="position">2</property>
246
+ </packing>
247
+ </child>
248
+ </object>
249
+ </child>
250
+ <child type="titlebar">
251
+ <placeholder/>
252
+ </child>
253
+ </object>
254
+ </interface>
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbencrypt2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Your Name
8
+ autorequire:
9
+ bindir:
10
+ - "."
11
+ cert_chain: []
12
+ date: 2018-01-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: visualruby
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.0.18
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.18
28
+ - !ruby/object:Gem::Dependency
29
+ name: require_all
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.2.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 1.2.0
42
+ description: Full description here
43
+ email: you@yoursite.com
44
+ executables:
45
+ - rubyencrypt
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - "./rubyencrypt"
50
+ - src/MyClass.rb
51
+ - src/glade/MyClass.glade
52
+ homepage: http://www.yoursite.org/
53
+ licenses: []
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: nowarning
71
+ rubygems_version: 2.6.11
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Short Description Here.
75
+ test_files: []