rsaa 0.0.5 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/archive_private.rb +2 -2
- data/lib/archive_private_open.rb +19 -0
- data/lib/archive_public.rb +2 -2
- data/lib/archive_public_open.rb +19 -0
- data/lib/mathematics.rb +19 -3
- data/lib/message_compile.rb +11 -0
- data/lib/rsa.rb +102 -23
- data/lib/text_chunk.rb +54 -0
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e99ade722cedeb27fb26cd8a7f1ded492c7a776a11d919690bf70777738b7cc6
|
|
4
|
+
data.tar.gz: 1400c2b19a22df7a0ecda612b85c7a03f24cf8b8b89ddf4b6cc219e7375f451d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ffc4afbc4675df5d9ed0952319a43e5d021e7e0aadd54235e1355c511bc14dc0ec5b73b14ea2e53301b57db85b39cd1165a7f1056e5baa3bee8050f635b1505e
|
|
7
|
+
data.tar.gz: 28666096be00c8eb0d6ef28dbc1678056937e01453ce7134893a086057fa6cdddbdf1938c0f05da3b929ef341fbffe978fca0e04fa678ee62dfc12e2107c4bfc
|
data/lib/archive_private.rb
CHANGED
|
@@ -8,12 +8,12 @@ class ArchivePrivate < Archive
|
|
|
8
8
|
|
|
9
9
|
def read
|
|
10
10
|
file = File.open full_name
|
|
11
|
-
decode
|
|
11
|
+
JSON.parse(decode(file.read), symbolize_names: true)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def write(text)
|
|
15
15
|
file = File.open(full_name, Archive::MODE_WRITE)
|
|
16
|
-
file.puts encode text
|
|
16
|
+
file.puts encode JSON.generate text
|
|
17
17
|
file.close
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require_relative './archive'
|
|
2
|
+
|
|
3
|
+
class ArchivePrivateOpen < Archive
|
|
4
|
+
FILE_NAME = 'private'
|
|
5
|
+
def initialize
|
|
6
|
+
set_name(FILE_NAME)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def read
|
|
10
|
+
file = File.open full_name
|
|
11
|
+
file
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def write(text)
|
|
15
|
+
file = File.open(full_name, Archive::MODE_WRITE)
|
|
16
|
+
file.puts text
|
|
17
|
+
file.close
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/archive_public.rb
CHANGED
|
@@ -8,12 +8,12 @@ class ArchivePublic < Archive
|
|
|
8
8
|
|
|
9
9
|
def read
|
|
10
10
|
file = File.open full_name
|
|
11
|
-
decode
|
|
11
|
+
JSON.parse(decode(file.read), symbolize_names: true)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def write(text)
|
|
15
15
|
file = File.open(full_name, Archive::MODE_WRITE)
|
|
16
|
-
file.puts encode text
|
|
16
|
+
file.puts encode JSON.generate text
|
|
17
17
|
file.close
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require_relative './archive'
|
|
2
|
+
|
|
3
|
+
class ArchivePublicOpen < Archive
|
|
4
|
+
FILE_NAME = 'public'
|
|
5
|
+
def initialize
|
|
6
|
+
set_name(FILE_NAME)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def read
|
|
10
|
+
file = File.open full_name
|
|
11
|
+
file
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def write(text)
|
|
15
|
+
file = File.open(full_name, Archive::MODE_WRITE)
|
|
16
|
+
file.puts text
|
|
17
|
+
file.close
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/mathematics.rb
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
require 'prime'
|
|
2
2
|
|
|
3
|
+
class Integer
|
|
4
|
+
def mod_pow(exp, mod)
|
|
5
|
+
result = 1
|
|
6
|
+
base = self
|
|
7
|
+
while exp > 0
|
|
8
|
+
if (exp & 1) == 1
|
|
9
|
+
result = (result * base) % mod
|
|
10
|
+
end
|
|
11
|
+
exp = exp >> 1
|
|
12
|
+
base = (base * base) % mod
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
result
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
3
19
|
class Mathematics
|
|
4
|
-
LIMIT =
|
|
5
|
-
@number_primes = Prime.
|
|
20
|
+
LIMIT = 10000
|
|
21
|
+
@number_primes = Prime.each(LIMIT).to_a
|
|
6
22
|
|
|
7
23
|
def self.random_prime
|
|
8
|
-
number_random = rand(
|
|
24
|
+
number_random = rand(@number_primes.length)
|
|
9
25
|
@number_primes[number_random]
|
|
10
26
|
end
|
|
11
27
|
|
data/lib/rsa.rb
CHANGED
|
@@ -1,9 +1,57 @@
|
|
|
1
1
|
require 'json'
|
|
2
2
|
require_relative 'mathematics'
|
|
3
|
+
require_relative './text_chunk'
|
|
3
4
|
require_relative './archive_private'
|
|
4
5
|
require_relative './archive_public'
|
|
6
|
+
require_relative './archive_private_open'
|
|
7
|
+
require_relative './archive_public_open'
|
|
8
|
+
require_relative './message_compile'
|
|
9
|
+
|
|
5
10
|
|
|
6
11
|
module RSA
|
|
12
|
+
module OPEN
|
|
13
|
+
|
|
14
|
+
class Private
|
|
15
|
+
attr_accessor :d, :n
|
|
16
|
+
|
|
17
|
+
def self.n=(n)
|
|
18
|
+
@n=n
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.d=(d)
|
|
22
|
+
@d=d
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.n
|
|
26
|
+
@n
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.d
|
|
30
|
+
@d
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class Public
|
|
35
|
+
attr_accessor :e, :n
|
|
36
|
+
|
|
37
|
+
def self.n=(n)
|
|
38
|
+
@n=n
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.e=(e)
|
|
42
|
+
@e=e
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.n
|
|
46
|
+
@n
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.e
|
|
50
|
+
@e
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
7
55
|
class Private
|
|
8
56
|
attr_reader :key_p, :key_q, :totiente_n, :key_d
|
|
9
57
|
|
|
@@ -23,7 +71,12 @@ module RSA
|
|
|
23
71
|
|
|
24
72
|
def create_file_of_keys
|
|
25
73
|
private_key_file = ArchivePrivate.new
|
|
26
|
-
private_key_file.write
|
|
74
|
+
private_key_file.write keys
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def create_file(archive)
|
|
78
|
+
key_n = @key_p * @key_q
|
|
79
|
+
archive.write [key_n, @key_d]
|
|
27
80
|
end
|
|
28
81
|
|
|
29
82
|
private
|
|
@@ -53,7 +106,11 @@ module RSA
|
|
|
53
106
|
|
|
54
107
|
def create_file_of_keys
|
|
55
108
|
public_key_file = ArchivePublic.new
|
|
56
|
-
public_key_file.write
|
|
109
|
+
public_key_file.write keys
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def create_file(archive)
|
|
113
|
+
archive.write [@key_n, @e]
|
|
57
114
|
end
|
|
58
115
|
|
|
59
116
|
private
|
|
@@ -94,34 +151,56 @@ module RSA
|
|
|
94
151
|
public.create_file_of_keys
|
|
95
152
|
end
|
|
96
153
|
|
|
97
|
-
def self.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
154
|
+
def self.generated_open_keys
|
|
155
|
+
private = RSA::Private.new
|
|
156
|
+
public = RSA::Public.new(private: private)
|
|
157
|
+
|
|
158
|
+
private.create_file(ArchivePrivateOpen.new)
|
|
159
|
+
public.create_file(ArchivePublicOpen.new)
|
|
160
|
+
end
|
|
101
161
|
|
|
102
|
-
|
|
103
|
-
|
|
162
|
+
def self.encode(menssage)
|
|
163
|
+
e = nil
|
|
164
|
+
n = nil
|
|
165
|
+
|
|
166
|
+
if RSA::OPEN::Public.e && RSA::OPEN::Public.n
|
|
167
|
+
e = RSA::OPEN::Public.e
|
|
168
|
+
n = RSA::OPEN::Public.n
|
|
169
|
+
else
|
|
170
|
+
archive_public = ArchivePublic.new
|
|
171
|
+
file = archive_public.read
|
|
172
|
+
e = file[:e].to_i
|
|
173
|
+
n = file[:key_n].to_i
|
|
174
|
+
end
|
|
175
|
+
chunk_size = TextChunk.block_size(n)
|
|
176
|
+
split_in_regex = /.{1,#{chunk_size}}/
|
|
104
177
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
178
|
+
pre_compile = MessageCompile.pre_compile(menssage)
|
|
179
|
+
array_chunk = pre_compile.scan(split_in_regex)
|
|
180
|
+
array_chunk.map { |chunk|
|
|
181
|
+
TextChunk.new(chunk).to_i.mod_pow(e, n)
|
|
108
182
|
}
|
|
109
|
-
menssage_encode
|
|
110
183
|
end
|
|
111
184
|
|
|
112
185
|
def self.decode(menssage_encode)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
186
|
+
d = nil
|
|
187
|
+
n = nil
|
|
188
|
+
|
|
189
|
+
if RSA::OPEN::Private.d && RSA::OPEN::Private.n
|
|
190
|
+
d = RSA::OPEN::Private.d
|
|
191
|
+
n = RSA::OPEN::Private.n
|
|
192
|
+
else
|
|
193
|
+
archive_private = ArchivePrivate.new
|
|
194
|
+
private_file = archive_private.read
|
|
195
|
+
d = private_file[:key_d].to_i
|
|
196
|
+
n = private_file[:key_p].to_i * private_file[:key_q].to_i
|
|
197
|
+
end
|
|
119
198
|
|
|
120
199
|
menssage_decode = menssage_encode.map { |block|
|
|
121
|
-
|
|
122
|
-
|
|
200
|
+
original_chunk = block.to_i.mod_pow(d, n)
|
|
201
|
+
TextChunk.new(original_chunk).to_s
|
|
202
|
+
}.join
|
|
123
203
|
|
|
124
|
-
menssage_decode
|
|
125
|
-
menssage_decode.pack('C*').force_encoding('UTF-8')
|
|
204
|
+
MessageCompile.back_pre_compile(menssage_decode)
|
|
126
205
|
end
|
|
127
|
-
end
|
|
206
|
+
end
|
data/lib/text_chunk.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Biblioteca disponibilizada pelo professor para a impletemetação
|
|
2
|
+
|
|
3
|
+
class TextChunk
|
|
4
|
+
def initialize(n)
|
|
5
|
+
case n
|
|
6
|
+
when String
|
|
7
|
+
@stringVal = n
|
|
8
|
+
when Numeric
|
|
9
|
+
@stringVal = numericToString(n)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def numericToString(n)
|
|
14
|
+
strVal = ""
|
|
15
|
+
if n == 0
|
|
16
|
+
strVal = "0"
|
|
17
|
+
else
|
|
18
|
+
while n > 0
|
|
19
|
+
ans = n.divmod(256)
|
|
20
|
+
charNum = ans[1]
|
|
21
|
+
strVal += charNum.chr
|
|
22
|
+
n = ans[0]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
return strVal
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_s
|
|
29
|
+
return @stringVal
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_i
|
|
33
|
+
result = 0
|
|
34
|
+
|
|
35
|
+
@stringVal.reverse.split('').each{ | c |
|
|
36
|
+
result = result * 256
|
|
37
|
+
result += c.ord
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return result
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.block_size(n)
|
|
44
|
+
block_size = 0
|
|
45
|
+
temp = n - 1
|
|
46
|
+
while temp > 1
|
|
47
|
+
temp = temp / 2
|
|
48
|
+
block_size += 1
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
return block_size / 8
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rsaa
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dayan Freitas
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-07-
|
|
11
|
+
date: 2022-07-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: ''
|
|
14
14
|
email: dayan.freitas.df@gmail.com.br
|
|
@@ -18,9 +18,13 @@ extra_rdoc_files: []
|
|
|
18
18
|
files:
|
|
19
19
|
- lib/archive.rb
|
|
20
20
|
- lib/archive_private.rb
|
|
21
|
+
- lib/archive_private_open.rb
|
|
21
22
|
- lib/archive_public.rb
|
|
23
|
+
- lib/archive_public_open.rb
|
|
22
24
|
- lib/mathematics.rb
|
|
25
|
+
- lib/message_compile.rb
|
|
23
26
|
- lib/rsa.rb
|
|
27
|
+
- lib/text_chunk.rb
|
|
24
28
|
homepage: https://github.com/Dayanfreitas/RSA
|
|
25
29
|
licenses:
|
|
26
30
|
- MIT
|