rsaa 0.0.4 → 0.0.7
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.rb +6 -1
- 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 +25 -3
- data/lib/message_compile.rb +11 -0
- data/lib/rsa.rb +101 -43
- 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: 63ed80c92a5bb0329eece215f6cee97b467ab6468f079b6aa06d8a2a6524f409
|
|
4
|
+
data.tar.gz: 033437ef244a711343e2e861989f793c42558eee43ec07b6133bbf247de89ed1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b2ccc8ee489e257283d1f07c422b9b0df2ba1a041840d88f35fa7a5f1d8738e227598ca30b51f4c52feea611e7ba2357d13f12b762dbba490c8281871a82298d
|
|
7
|
+
data.tar.gz: 295c2abe92bb105e551399ea89dd57258f65ae3aa2d34cc02e8deadedb68e01f9130cd9854612bb39bb9b0a2e1f860583641f16b95d948c609f6ef9700487caf
|
data/lib/archive.rb
CHANGED
|
@@ -4,6 +4,7 @@ class Archive
|
|
|
4
4
|
ROOT_PATH_FILE = "./"
|
|
5
5
|
EXTENSION_FILE = ".txt"
|
|
6
6
|
MODE_WRITE = "w"
|
|
7
|
+
FILE_PATH_KEYS="keys"
|
|
7
8
|
|
|
8
9
|
attr_reader :name, :path
|
|
9
10
|
|
|
@@ -20,7 +21,11 @@ class Archive
|
|
|
20
21
|
end
|
|
21
22
|
|
|
22
23
|
def full_name
|
|
23
|
-
|
|
24
|
+
if FILE_PATH_KEYS
|
|
25
|
+
Dir.mkdir(FILE_PATH_KEYS) unless Dir.exist?(FILE_PATH_KEYS)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
"#{ FILE_PATH_KEYS + '/' if FILE_PATH_KEYS}#{@name || 'text'}#{EXTENSION_FILE}"
|
|
24
29
|
end
|
|
25
30
|
|
|
26
31
|
def encode(string)
|
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
|
|
|
@@ -21,4 +37,10 @@ class Mathematics
|
|
|
21
37
|
# (i * e) % @totiente_n == 1
|
|
22
38
|
mod(term1, term2) == 1
|
|
23
39
|
end
|
|
40
|
+
|
|
41
|
+
def self.func_totiente_n(p, q)
|
|
42
|
+
# função totiente em n
|
|
43
|
+
# 𝜑(𝑛) = (𝑝 − 1) ∗ (𝑞 − 1)
|
|
44
|
+
(p - 1) * (q - 1)
|
|
45
|
+
end
|
|
24
46
|
end
|
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,17 +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
|
|
27
75
|
end
|
|
28
76
|
|
|
29
|
-
def
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
puts @key_p
|
|
33
|
-
puts @key_q
|
|
34
|
-
puts @key_d
|
|
35
|
-
puts @totiente_n
|
|
36
|
-
puts "=== END PRIVATE KEYS ==="
|
|
77
|
+
def create_file(archive)
|
|
78
|
+
key_n = @key_p * @key_q
|
|
79
|
+
archive.write [key_n, @key_d]
|
|
37
80
|
end
|
|
38
81
|
|
|
39
82
|
private
|
|
@@ -44,9 +87,7 @@ module RSA
|
|
|
44
87
|
end
|
|
45
88
|
|
|
46
89
|
def generated_totiente_n
|
|
47
|
-
|
|
48
|
-
# 𝜑(𝑛) = (𝑝 − 1) ∗ (𝑞 − 1)
|
|
49
|
-
@totiente_n = (@key_p - 1) * (@key_q - 1)
|
|
90
|
+
@totiente_n = Mathematics.func_totiente_n(@key_p, @key_q)
|
|
50
91
|
end
|
|
51
92
|
|
|
52
93
|
def keys
|
|
@@ -65,15 +106,11 @@ module RSA
|
|
|
65
106
|
|
|
66
107
|
def create_file_of_keys
|
|
67
108
|
public_key_file = ArchivePublic.new
|
|
68
|
-
public_key_file.write
|
|
109
|
+
public_key_file.write keys
|
|
69
110
|
end
|
|
70
111
|
|
|
71
|
-
def
|
|
72
|
-
|
|
73
|
-
puts "==== PUBLIC KEYS ===="
|
|
74
|
-
puts @key_n
|
|
75
|
-
puts @e
|
|
76
|
-
puts "== END PUBLIC KEYS =="
|
|
112
|
+
def create_file(archive)
|
|
113
|
+
archive.write [@key_n, @e]
|
|
77
114
|
end
|
|
78
115
|
|
|
79
116
|
private
|
|
@@ -110,41 +147,62 @@ module RSA
|
|
|
110
147
|
private = RSA::Private.new
|
|
111
148
|
public = RSA::Public.new(private: private)
|
|
112
149
|
|
|
113
|
-
private.display_keys
|
|
114
150
|
private.create_file_of_keys
|
|
115
|
-
|
|
116
|
-
public.display_keys
|
|
117
151
|
public.create_file_of_keys
|
|
118
152
|
end
|
|
119
153
|
|
|
120
|
-
def self.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
file = JSON.parse(file)
|
|
154
|
+
def self.generated_open_keys
|
|
155
|
+
private = RSA::Private.new
|
|
156
|
+
public = RSA::Public.new(private: private)
|
|
124
157
|
|
|
125
|
-
|
|
126
|
-
|
|
158
|
+
private.create_file(ArchivePrivateOpen.new)
|
|
159
|
+
public.create_file(ArchivePublicOpen.new)
|
|
160
|
+
end
|
|
161
|
+
|
|
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}}/
|
|
127
177
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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)
|
|
131
182
|
}
|
|
132
|
-
menssage_encode
|
|
133
183
|
end
|
|
134
184
|
|
|
135
|
-
def self.decode(menssage_encode)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
185
|
+
def self.decode(menssage_encode)
|
|
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
|
|
142
198
|
|
|
143
199
|
menssage_decode = menssage_encode.map { |block|
|
|
144
|
-
|
|
145
|
-
|
|
200
|
+
original_chunk = block.to_i.mod_pow(d, n)
|
|
201
|
+
TextChunk.new(original_chunk).to_s
|
|
202
|
+
}.join
|
|
146
203
|
|
|
147
|
-
menssage_decode
|
|
148
|
-
menssage_decode.pack('C*').force_encoding('UTF-8')
|
|
204
|
+
MessageCompile.back_pre_compile(menssage_decode)
|
|
149
205
|
end
|
|
150
206
|
end
|
|
207
|
+
|
|
208
|
+
RSA.generated_open_keys
|
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.7
|
|
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-
|
|
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
|