rsaa 0.0.5 → 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_private.rb +2 -2
- data/lib/archive_public.rb +2 -2
- data/lib/message_compile.rb +11 -0
- data/lib/rsa.rb +97 -22
- data/lib/text_chunk.rb +54 -0
- metadata +4 -2
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_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/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,7 +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
|
|
87
|
+
private_key_file.write keys
|
|
27
88
|
end
|
|
28
89
|
|
|
29
90
|
private
|
|
@@ -53,7 +114,7 @@ module RSA
|
|
|
53
114
|
|
|
54
115
|
def create_file_of_keys
|
|
55
116
|
public_key_file = ArchivePublic.new
|
|
56
|
-
public_key_file.write
|
|
117
|
+
public_key_file.write keys
|
|
57
118
|
end
|
|
58
119
|
|
|
59
120
|
private
|
|
@@ -95,33 +156,47 @@ module RSA
|
|
|
95
156
|
end
|
|
96
157
|
|
|
97
158
|
def self.encode(menssage)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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}}/
|
|
104
173
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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)
|
|
108
178
|
}
|
|
109
|
-
menssage_encode
|
|
110
179
|
end
|
|
111
180
|
|
|
112
181
|
def self.decode(menssage_encode)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
194
|
|
|
120
195
|
menssage_decode = menssage_encode.map { |block|
|
|
121
|
-
|
|
122
|
-
|
|
196
|
+
original_chunk = block.to_i.mod_pow(d, n)
|
|
197
|
+
TextChunk.new(original_chunk).to_s
|
|
198
|
+
}.join
|
|
123
199
|
|
|
124
|
-
menssage_decode
|
|
125
|
-
menssage_decode.pack('C*').force_encoding('UTF-8')
|
|
200
|
+
MessageCompile.back_pre_compile(menssage_decode)
|
|
126
201
|
end
|
|
127
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-07-
|
|
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,7 +20,9 @@ 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
|
|
25
|
+
- lib/text_chunk.rb
|
|
24
26
|
homepage: https://github.com/Dayanfreitas/RSA
|
|
25
27
|
licenses:
|
|
26
28
|
- MIT
|