rsaa 0.0.3 → 0.0.6
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_public.rb +2 -2
- data/lib/mathematics.rb +6 -0
- data/lib/message_compile.rb +11 -0
- data/lib/rsa.rb +99 -47
- data/lib/text_chunk.rb +54 -0
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 70896d0c3fce5933fcbb234100e9d7ca6b2f6ba818fdbe3f0c7799c7bd2459a7
|
|
4
|
+
data.tar.gz: c5c5bc35dd136bbb6f6e62170624a7281ae52930e7fee8a724d29cc4291828b4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9700ea5f52225bb477b14541cf3c19747ecde26c22ed8acde7fb2b770748262271cbfae1725c83874da873b185ed52cc02bc2cefc3b6057b6a5f668ec548c29d
|
|
7
|
+
data.tar.gz: 830fd958b4b5fecbe3c4b043fcf9a14ebf046571faafe679d478bbe773815f08e61b617f0ea829875be3069923fcc019c02ccc15efb46c92008a4a54b2a688e1
|
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
|
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
|
data/lib/mathematics.rb
CHANGED
data/lib/rsa.rb
CHANGED
|
@@ -1,9 +1,70 @@
|
|
|
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 './message_compile'
|
|
7
|
+
|
|
8
|
+
class Integer
|
|
9
|
+
def mod_pow(exp, mod)
|
|
10
|
+
result = 1
|
|
11
|
+
base = self
|
|
12
|
+
while exp > 0
|
|
13
|
+
if (exp & 1) == 1
|
|
14
|
+
result = (result * base) % mod
|
|
15
|
+
end
|
|
16
|
+
exp = exp >> 1
|
|
17
|
+
base = (base * base) % mod
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
result
|
|
21
|
+
end
|
|
22
|
+
end
|
|
5
23
|
|
|
6
24
|
module RSA
|
|
25
|
+
module OPEN
|
|
26
|
+
|
|
27
|
+
class Private
|
|
28
|
+
attr_accessor :d, :n
|
|
29
|
+
|
|
30
|
+
def self.n=(n)
|
|
31
|
+
@n=n
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.d=(d)
|
|
35
|
+
@d=d
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.n
|
|
39
|
+
@n
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.d
|
|
43
|
+
@d
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Public
|
|
48
|
+
attr_accessor :e, :n
|
|
49
|
+
|
|
50
|
+
def self.n=(n)
|
|
51
|
+
@n=n
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.e=(e)
|
|
55
|
+
@e=e
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.n
|
|
59
|
+
@n
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.e
|
|
63
|
+
@e
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
7
68
|
class Private
|
|
8
69
|
attr_reader :key_p, :key_q, :totiente_n, :key_d
|
|
9
70
|
|
|
@@ -23,17 +84,7 @@ module RSA
|
|
|
23
84
|
|
|
24
85
|
def create_file_of_keys
|
|
25
86
|
private_key_file = ArchivePrivate.new
|
|
26
|
-
private_key_file.write
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def display_keys
|
|
30
|
-
puts ""
|
|
31
|
-
puts "==== PRIVATE KEYS ===="
|
|
32
|
-
puts @key_p
|
|
33
|
-
puts @key_q
|
|
34
|
-
puts @key_d
|
|
35
|
-
puts @totiente_n
|
|
36
|
-
puts "=== END PRIVATE KEYS ==="
|
|
87
|
+
private_key_file.write keys
|
|
37
88
|
end
|
|
38
89
|
|
|
39
90
|
private
|
|
@@ -44,9 +95,7 @@ module RSA
|
|
|
44
95
|
end
|
|
45
96
|
|
|
46
97
|
def generated_totiente_n
|
|
47
|
-
|
|
48
|
-
# 𝜑(𝑛) = (𝑝 − 1) ∗ (𝑞 − 1)
|
|
49
|
-
@totiente_n = (@key_p - 1) * (@key_q - 1)
|
|
98
|
+
@totiente_n = Mathematics.func_totiente_n(@key_p, @key_q)
|
|
50
99
|
end
|
|
51
100
|
|
|
52
101
|
def keys
|
|
@@ -65,15 +114,7 @@ module RSA
|
|
|
65
114
|
|
|
66
115
|
def create_file_of_keys
|
|
67
116
|
public_key_file = ArchivePublic.new
|
|
68
|
-
public_key_file.write
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def display_keys
|
|
72
|
-
puts ""
|
|
73
|
-
puts "==== PUBLIC KEYS ===="
|
|
74
|
-
puts @key_n
|
|
75
|
-
puts @e
|
|
76
|
-
puts "== END PUBLIC KEYS =="
|
|
117
|
+
public_key_file.write keys
|
|
77
118
|
end
|
|
78
119
|
|
|
79
120
|
private
|
|
@@ -110,41 +151,52 @@ module RSA
|
|
|
110
151
|
private = RSA::Private.new
|
|
111
152
|
public = RSA::Public.new(private: private)
|
|
112
153
|
|
|
113
|
-
private.display_keys
|
|
114
154
|
private.create_file_of_keys
|
|
115
|
-
|
|
116
|
-
public.display_keys
|
|
117
155
|
public.create_file_of_keys
|
|
118
156
|
end
|
|
119
157
|
|
|
120
158
|
def self.encode(menssage)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
159
|
+
e = nil
|
|
160
|
+
n = nil
|
|
161
|
+
|
|
162
|
+
if RSA::OPEN::Public.e && RSA::OPEN::Public.n
|
|
163
|
+
e = RSA::OPEN::Public.e
|
|
164
|
+
n = RSA::OPEN::Public.n
|
|
165
|
+
else
|
|
166
|
+
archive_public = ArchivePublic.new
|
|
167
|
+
file = archive_public.read
|
|
168
|
+
e = file[:e].to_i
|
|
169
|
+
n = file[:key_n].to_i
|
|
170
|
+
end
|
|
171
|
+
chunk_size = TextChunk.block_size(n)
|
|
172
|
+
split_in_regex = /.{1,#{chunk_size}}/
|
|
127
173
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
174
|
+
pre_compile = MessageCompile.pre_compile(menssage)
|
|
175
|
+
array_chunk = pre_compile.scan(split_in_regex)
|
|
176
|
+
array_chunk.map { |chunk|
|
|
177
|
+
TextChunk.new(chunk).to_i.mod_pow(e ,n)
|
|
131
178
|
}
|
|
132
|
-
menssage_encode
|
|
133
179
|
end
|
|
134
180
|
|
|
135
|
-
def self.decode(menssage_encode)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
181
|
+
def self.decode(menssage_encode)
|
|
182
|
+
d = nil
|
|
183
|
+
n = nil
|
|
184
|
+
|
|
185
|
+
if RSA::OPEN::Private.d && RSA::OPEN::Private.n
|
|
186
|
+
d = RSA::OPEN::Private.d
|
|
187
|
+
n = RSA::OPEN::Private.n
|
|
188
|
+
else
|
|
189
|
+
archive_private = ArchivePrivate.new
|
|
190
|
+
private_file = archive_private.read
|
|
191
|
+
d = private_file[:key_d].to_i
|
|
192
|
+
n = private_file[:key_p].to_i * private_file[:key_q].to_i
|
|
193
|
+
end
|
|
142
194
|
|
|
143
195
|
menssage_decode = menssage_encode.map { |block|
|
|
144
|
-
|
|
145
|
-
|
|
196
|
+
original_chunk = block.to_i.mod_pow(d, n)
|
|
197
|
+
TextChunk.new(original_chunk).to_s
|
|
198
|
+
}.join
|
|
146
199
|
|
|
147
|
-
menssage_decode
|
|
148
|
-
menssage_decode.pack('C*').force_encoding('UTF-8')
|
|
200
|
+
MessageCompile.back_pre_compile(menssage_decode)
|
|
149
201
|
end
|
|
150
202
|
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.6
|
|
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-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: ''
|
|
14
14
|
email: dayan.freitas.df@gmail.com.br
|
|
@@ -20,8 +20,10 @@ files:
|
|
|
20
20
|
- lib/archive_private.rb
|
|
21
21
|
- lib/archive_public.rb
|
|
22
22
|
- lib/mathematics.rb
|
|
23
|
+
- lib/message_compile.rb
|
|
23
24
|
- lib/rsa.rb
|
|
24
|
-
|
|
25
|
+
- lib/text_chunk.rb
|
|
26
|
+
homepage: https://github.com/Dayanfreitas/RSA
|
|
25
27
|
licenses:
|
|
26
28
|
- MIT
|
|
27
29
|
metadata: {}
|